
    \j                     z    d Z ddlZddlmZ ddlZddlmZmZ 	 ddlm	Z	  G d de	      Zy# e
$ r	 ddlm	Z	 Y w xY w)a  
Dictionary with auto-expiring values for caching purposes.

Expiration happens on any access, object is locked during cleanup from expired
values. Can not store more than max_len elements - the oldest will be deleted.

>>> ExpiringDict(max_len=100, max_age_seconds=10)

The values stored in the following way:
{
    key1: (value1, created_time1),
    key2: (value2, created_time2)
}

NOTE: iteration over dict and also keys() do not remove expired values!
    N)RLock)AnyUnion)OrderedDictc                       e Zd ZddZd ZddZddZddZd ZddZ	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zed        Zed        Zed        Zed        Zed        Zd Zd Zd Zy)ExpiringDictNc                      j                  |      s j                  ||       t        j                          | _        | _        t                _        t        j                  dk\  r fd _
        n j                   _
        |v j                  |      r j                  |||       y  j                  |      r j                  |       y  j                  |      r j!                  |       y t#        d      y )N)      c                  6    t         j                               S N)listkeysselfs   ?/root/env/lib/python3.12/site-packages/expiringdict/__init__.py<lambda>z'ExpiringDict.__init__.<locals>.<lambda>+   s    d499;&7     zcan not unpack items)+_ExpiringDict__is_instance_of_expiring_dict_ExpiringDict__assertionsr   __init__max_lenmax_ager   locksysversion_info
_safe_keysr   !_ExpiringDict__copy_expiring_dict"_ExpiringDict__is_instance_of_dict_ExpiringDict__copy_dict _ExpiringDict__is_reduced_result"_ExpiringDict__copy_reduced_result
ValueError)r   r   max_age_secondsitemss   `   r   r   zExpiringDict.__init__   s     2259g7T"&G	v%7DO"iiDO2259))'?EJ++E2  '))%0**51 !!788 r   c                     	 | j                   5  t        j                  | |      }t        j                         |d   z
  | j                  k  r
	 ddd       y| |= 	 ddd       y# 1 sw Y   yxY w# t
        $ r Y yw xY w)z7 Return True if the dict has a key, else return False.    NTFr   r   __getitem__timer   KeyError)r   keyitems      r   __contains__zExpiringDict.__contains__:   s    	 ""..tS999;a(4<<7" "
 S	" "   		s:   A/ ?A#A/ A#A/ #A,(A/ ,A/ /	A;:A;c                    | j                   5  t        j                  | |      }t        j                         |d   z
  }|| j                  k  r |r|d   |fcddd       S |d   cddd       S | |= t        |      # 1 sw Y   yxY w)z[ Return the item of the dict.

        Raises a KeyError if key is not in the map.
        r'   r   Nr(   )r   r,   with_ager-   item_ages        r   r)   zExpiringDict.__getitem__G   s    
 YY 
	$**45Dyy{T!W,H$,,&7H,
	$ 
	$  7
	$ 
	$ Ism#
	$ 
	$s   AA;A;-A;;Bc                 (   | j                   5  t        |       | j                  k(  r|| v r| |= n	 | j                  d       |t        j
                         }t        j                  | |||f       ddd       y# t        $ r Y Cw xY w# 1 sw Y   yxY w)z Set d[key] to value. F)lastN)r   lenr   popitemr+   r*   r   __setitem__)r   r,   valueset_times       r   r6   zExpiringDict.__setitem__X   s    YY 	B4yDLL($;S	%0 99;##D#x/@A	B 	B $ 	B 	Bs.   !BA9/B9	BBBBBc                     | j                   5  	 t        j                  | |      }| |= |d   cddd       S # t        $ r |cY cddd       S w xY w# 1 sw Y   yxY w)z{ Get item from the dict and remove it.

        Return default if expired or does not exist. Never raise KeyError.
        r   N)r   r   r)   r+   )r   r,   defaultr-   s       r   popzExpiringDict.popg   sj    
 YY 	"..tS9IAw		 	
  	 	
	 	s&   A6AAAAAc                 `    | j                  |d      \  }}|r| j                  |z
  }|dkD  r|S y)zg Return TTL of the `key` (in seconds).

        Returns None for non-existent or expired keys.
        T)r0   r   N)getr   )r   r,   	key_valuekey_agekey_ttls        r   ttlzExpiringDict.ttlt   s;    
 "XXcDX9	7llW,G{r   c                 Z    	 | j                  ||      S # t        $ r |r|dfcY S |cY S w xY w)zE Return the value for key if key is in the dictionary, else default. N)r)   r+   )r   r,   r:   r0   s       r   r=   zExpiringDict.get   s;    	##C22 	}$		s    ***c                     g }| j                         D ]  }	 |j                  || |   f        |S # t        $ r Y )w xY w)z? Return a copy of the dictionary's list of (key, value) pairs. r   appendr+   r   rr,   s      r   r%   zExpiringDict.items   sR    ??$ 	C#tCy)*	
   s   1	==c                     g }| j                         D ]*  }	 |j                  |t        j                  | |      f       , |S # t        $ r Y :w xY w)zL Return a copy of the dictionary's list of (key, value, timestamp) triples. )r   rE   r   r)   r+   rF   s      r   items_with_timestampz!ExpiringDict.items_with_timestamp   s[    ??$ 	C#{66tSABC	
   s   'A	AAc                 |    g }| j                         D ]  }	 |j                  | |           |S # t        $ r Y 'w xY w)zZ Return a copy of the dictionary's list of values.
        See the note for dict.items(). rD   rF   s      r   valueszExpiringDict.values   sO     ??$ 	Cc#	
   s   /	;;c                     t               )zE Create a new dictionary with keys from seq and values set to value. NotImplementedErrorr   s    r   fromkeyszExpiringDict.fromkeys       !##r   c                     t               )z> Return an iterator over the dictionary's (key, value) pairs. rM   r   s    r   	iteritemszExpiringDict.iteritems   rP   r   c                     t               )z2 Return an iterator over the dictionary's values. rM   r   s    r   
itervalueszExpiringDict.itervalues   rP   r   c                     t               )zC Return a new view of the dictionary's items ((key, value) pairs). rM   r   s    r   	viewitemszExpiringDict.viewitems   rP   r   c                     t               )z- Return a new view of the dictionary's keys. rM   r   s    r   viewkeyszExpiringDict.viewkeys   rP   r   c                     t               )z/ Return a new view of the dictionary's values. rM   r   s    r   
viewvalueszExpiringDict.viewvalues   rP   r   c                 p    | j                   | j                  | j                  d| j                         fff}|S )Nreduce_result)	__class__r   r   rI   )r   reduceds     r   
__reduce__zExpiringDict.__reduce__   s3    ..4<<QUQjQjQl?m"nnr   c                 H    | j                  |       | j                  |       y r   )_ExpiringDict__assert_max_len%_ExpiringDict__assert_max_age_seconds)r   r   r$   s      r   __assertionszExpiringDict.__assertions   s    g&%%o6r   c                     | dk\  sJ y Nr'    )r   s    r   __assert_max_lenzExpiringDict.__assert_max_len   s    !||r   c                     | dk\  sJ y )Nr   rf   )r$   s    r   __assert_max_age_secondsz%ExpiringDict.__assert_max_age_seconds   s    !###r   c                 2    t        |       dk(  r	| d   dk(  ryy)N   r   r\   TF)r4   r%   s    r   __is_reduced_resultz ExpiringDict.__is_reduced_result   s    u:?uQx?:r   c                 *    | t        | t              ryyNTF)
isinstancer   rl   s    r   __is_instance_of_expiring_dictz+ExpiringDict.__is_instance_of_expiring_dict   s    %.r   c                 &    t        | t              ryyro   )rp   dictrl   s    r   __is_instance_of_dictz"ExpiringDict.__is_instance_of_dict   s    eT"r   c           
      4   || j                  |       || _        n|j                  | _        || j                  |       || _        n|j                  | _        |j	                         D cg c]  \  }\  }}| j                  |||       c}}} y c c}}}w r   )ra   r   rb   r   rI   r6   )r   r   r$   r%   r,   r7   r8   s          r   __copy_expiring_dictz!ExpiringDict.__copy_expiring_dict   s    !!'*"DL ==DL&))/:*DL ==DLNSNhNhNjkk4JC9J%		#uh	/kks   - Bc                 v    |j                         D cg c]  \  }}| j                  ||       c}} y c c}}w r   )r%   r6   )r   r%   r,   r7   s       r   __copy_dictzExpiringDict.__copy_dict   s*    8=F*#u		#u	%FFs   5c           
      n    |d   D cg c]  \  }\  }}| j                  |||       c}}} y c c}}}w re   )r6   )r   r%   r,   r7   r8   s        r   __copy_reduced_resultz"ExpiringDict.__copy_reduced_result   s4    NSTUhWW4JC9J%		#uh	/WWs    0r   )F)NF)__name__
__module____qualname__r   r.   r)   r6   r;   rA   r=   r%   rI   rK   rO   rR   rT   rV   rX   rZ   r_   r   staticmethodra   rb   r!   r   r   r   r    r"   rf   r   r   r   r      s    96$"B
	$$$$$$7   $ $  
    
l GXr   r   )__doc__r*   	threadingr   r   typingr   r   collectionsr   ImportErrorordereddictr   rf   r   r   <module>r      sD   "   
 ('[X; [X  ('(s   , ::