a
    |=icU                     @   s  d Z dZdZddlZddlZddlZddlZddlZddlZddl	m
Z
 G dd dZG d	d
 d
ejjZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZe
jdfddZe
jdfddZdS )z0Extensible memoizing collections and decorators.)
Cache	FIFOCacheLFUCacheLRUCacheMRUCacheRRCache	TLRUCacheTTLCachecachedcachedmethodz5.2.0    N   )keysc                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	_DefaultSize c                 C   s   dS Nr   r   self_r   r   d/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/cachetools/__init__.py__getitem__    s    z_DefaultSize.__getitem__c                 C   s   |dksJ d S r   r   )r   r   valuer   r   r   __setitem__#   s    z_DefaultSize.__setitem__c                 C   s   dS r   r   r   r   r   r   pop&   s    z_DefaultSize.popN)__name__
__module____qualname__	__slots__r   r   r   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZe Ze Zd!ddZdd Z	dd Z
d	d
 Zdd Zdd Zdd Zdd Zdd Zd"ddZefddZd#ddZedd Zedd Zedd  ZdS )$r   z?Mutable mapping to serve as a simple cache or cache base class.Nc                 C   s6   |r
|| _ | j tj urt | _t | _d| _|| _d S Nr   )	getsizeofr   dict_Cache__size_Cache__data_Cache__currsize_Cache__maxsizer   maxsizer   r   r   r   __init__1   s    zCache.__init__c                 C   s   d| j jt| j| j| jf S )Nz%s(%s, maxsize=%r, currsize=%r))	__class__r   reprr!   r#   r"   r   r   r   r   __repr__:   s    zCache.__repr__c                 C   s.   z| j | W S  ty(   | | Y S 0 d S N)r!   KeyError__missing__r   keyr   r   r   r   B   s    zCache.__getitem__c                 C   s   | j }| |}||kr td|| jvs8| j| |k rP| j| |krP|   q8|| jv rj|| j|  }n|}|| j|< || j|< |  j|7  _d S )Nzvalue too large)r#   r   
ValueErrorr!   r    r"   popitem)r   r/   r   r%   sizeZdiffsizer   r   r   r   H   s    




zCache.__setitem__c                 C   s&   | j |}| j|= |  j|8  _d S r+   )r    r   r!   r"   )r   r/   r2   r   r   r   __delitem__X   s    zCache.__delitem__c                 C   s
   || j v S r+   )r!   r.   r   r   r   __contains__]   s    zCache.__contains__c                 C   s   t |d S r+   )r,   r.   r   r   r   r-   `   s    zCache.__missing__c                 C   s
   t | jS r+   )iterr!   r)   r   r   r   __iter__c   s    zCache.__iter__c                 C   s
   t | jS r+   )lenr!   r)   r   r   r   __len__f   s    zCache.__len__c                 C   s   || v r| | S |S d S r+   r   )r   r/   defaultr   r   r   geti   s    z	Cache.getc                 C   s4   || v r| | }| |= n|| j u r,t|n|}|S r+   )_Cache__markerr,   r   r/   r9   r   r   r   r   r   o   s    

z	Cache.popc                 C   s"   || v r| | }n| | |< }|S r+   r   r<   r   r   r   
setdefaulty   s    
zCache.setdefaultc                 C   s   | j S )zThe maximum size of the cache.)r#   r)   r   r   r   r%      s    zCache.maxsizec                 C   s   | j S )zThe current size of the cache.)r"   r)   r   r   r   currsize   s    zCache.currsizec                 C   s   dS )z+Return the size of a cache element's value.r   r   )r   r   r   r   r      s    zCache.getsizeof)N)N)N)r   r   r   __doc__objectr;   r   r    r&   r*   r   r   r3   r4   r-   r6   r8   r:   r   r=   propertyr%   r>   staticmethodr   r   r   r   r   r   *   s*   
	




r   c                   @   s>   e Zd ZdZdddZejfddZejfddZd	d
 ZdS )r   z/First In First Out (FIFO) cache implementation.Nc                 C   s   t | || t | _d S r+   )r   r&   collectionsOrderedDict_FIFOCache__orderr$   r   r   r   r&      s    zFIFOCache.__init__c                 C   s>   || || z| j | W n ty8   d | j |< Y n0 d S r+   )rE   move_to_endr,   r   r/   r   cache_setitemr   r   r   r      s
    zFIFOCache.__setitem__c                 C   s   || | | j |= d S r+   )rE   r   r/   cache_delitemr   r   r   r3      s    
zFIFOCache.__delitem__c                 C   sL   zt t| j}W n& ty8   tdt| j dY n0 || |fS dS )z9Remove and return the `(key, value)` pair first inserted.%s is emptyN)nextr5   rE   StopIterationr,   typer   r   r.   r   r   r   r1      s
    zFIFOCache.popitem)N)	r   r   r   r?   r&   r   r   r3   r1   r   r   r   r   r      s
   
r   c                   @   sL   e Zd ZdZdddZejfddZejfddZejfd	d
Zdd Z	dS )r   z1Least Frequently Used (LFU) cache implementation.Nc                 C   s   t | || t | _d S r+   )r   r&   rC   Counter_LFUCache__counterr$   r   r   r   r&      s    zLFUCache.__init__c                 C   s(   || |}|| v r$| j |  d8  < |S r   rP   r   r/   cache_getitemr   r   r   r   r      s    
zLFUCache.__getitem__c                 C   s"   || || | j |  d8  < d S r   rQ   rG   r   r   r   r      s    zLFUCache.__setitem__c                 C   s   || | | j |= d S r+   rQ   rI   r   r   r   r3      s    
zLFUCache.__delitem__c                 C   sP   z| j d\\}}W n& ty<   tdt| j dY n0 || |fS dS )z@Remove and return the `(key, value)` pair least frequently used.r   rK   N)rP   most_commonr0   r,   rN   r   r   )r   r/   r   r   r   r   r1      s
    zLFUCache.popitem)N)
r   r   r   r?   r&   r   r   r   r3   r1   r   r   r   r   r      s   
r   c                   @   sT   e Zd ZdZdddZejfddZejfddZejfd	d
Zdd Z	dd Z
dS )r   z/Least Recently Used (LRU) cache implementation.Nc                 C   s   t | || t | _d S r+   )r   r&   rC   rD   _LRUCache__orderr$   r   r   r   r&      s    zLRUCache.__init__c                 C   s    || |}|| v r|  | |S r+   _LRUCache__updaterR   r   r   r   r      s    

zLRUCache.__getitem__c                 C   s   || || |  | d S r+   rV   rG   r   r   r   r      s    zLRUCache.__setitem__c                 C   s   || | | j |= d S r+   )rU   rI   r   r   r   r3      s    
zLRUCache.__delitem__c                 C   sL   zt t| j}W n& ty8   tdt| j dY n0 || |fS dS )z>Remove and return the `(key, value)` pair least recently used.rK   N)rL   r5   rU   rM   r,   rN   r   r   r.   r   r   r   r1      s
    zLRUCache.popitemc                 C   s2   z| j | W n ty,   d | j |< Y n0 d S r+   )rU   rF   r,   r.   r   r   r   __update   s    zLRUCache.__update)N)r   r   r   r?   r&   r   r   r   r3   r1   rW   r   r   r   r   r      s   
	r   c                   @   sT   e Zd ZdZdddZejfddZejfddZejfd	d
Zdd Z	dd Z
dS )r   z.Most Recently Used (MRU) cache implementation.Nc                 C   s   t | || t | _d S r+   )r   r&   rC   rD   _MRUCache__orderr$   r   r   r   r&      s    zMRUCache.__init__c                 C   s    || |}|| v r|  | |S r+   _MRUCache__updaterR   r   r   r   r      s    

zMRUCache.__getitem__c                 C   s   || || |  | d S r+   rZ   rG   r   r   r   r      s    zMRUCache.__setitem__c                 C   s   || | | j |= d S r+   )rY   rI   r   r   r   r3     s    
zMRUCache.__delitem__c                 C   sL   zt t| j}W n& ty8   tdt| j dY n0 || |fS dS )z=Remove and return the `(key, value)` pair most recently used.rK   N)rL   r5   rY   rM   r,   rN   r   r   r.   r   r   r   r1     s
    zMRUCache.popitemc                 C   s6   z| j j|dd W n ty0   d | j |< Y n0 d S )NF)last)rY   rF   r,   r.   r   r   r   rX     s    zMRUCache.__update)N)r   r   r   r?   r&   r   r   r   r3   r1   r[   r   r   r   r   r      s   
	r   c                   @   s4   e Zd ZdZejdfddZedd Zdd ZdS )	r   z-Random Replacement (RR) cache implementation.Nc                 C   s   t | || || _d S r+   )r   r&   _RRCache__choice)r   r%   choicer   r   r   r   r&     s    zRRCache.__init__c                 C   s   | j S )z(The `choice` function used by the cache.)r]   r)   r   r   r   r^     s    zRRCache.choicec                 C   sL   z|  t| }W n& ty8   tdt| j dY n0 || |fS dS )z/Remove and return a random `(key, value)` pair.rK   N)r]   list
IndexErrorr,   rN   r   r   r.   r   r   r   r1   !  s
    zRRCache.popitem)	r   r   r   r?   randomr^   r&   rA   r1   r   r   r   r   r     s
   
r   c                       s   e Zd ZdZG dd dZejdfddZej	fddZ	ej
fd	d
Z
e fddZedd Zdd Zdd Zdd Zdd Z  ZS )_TimedCachez0Base class for time aware cache implementations.c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )z_TimedCache._Timerc                 C   s   || _ d| _d S r   )_Timer__timer_Timer__nesting)r   timerr   r   r   r&   /  s    z_TimedCache._Timer.__init__c                 C   s   | j dkr|  S | jS d S r   rd   rc   Z_Timer__timer)   r   r   r   __call__3  s    
z_TimedCache._Timer.__call__c                 C   s2   | j dkr|   | _}n| j}|  j d7  _ |S )Nr   r   rf   r   timer   r   r   	__enter__9  s
    
z_TimedCache._Timer.__enter__c                 G   s   |  j d8  _ d S r   )rd   )r   excr   r   r   __exit__A  s    z_TimedCache._Timer.__exit__c                 C   s   t j| jffS r+   )rb   _Timerrc   r)   r   r   r   
__reduce__D  s    z_TimedCache._Timer.__reduce__c                 C   s   t | j|S r+   )getattrrc   )r   namer   r   r   __getattr__G  s    z_TimedCache._Timer.__getattr__N)	r   r   r   r&   rg   rj   rl   rn   rq   r   r   r   r   rm   .  s   rm   Nc                 C   s   t | || t|| _d S r+   )r   r&   rb   rm   _TimedCache__timer)r   r%   re   r   r   r   r   r&   J  s    z_TimedCache.__init__c                 C   s<   | j "}| | || W  d    S 1 s.0    Y  d S r+   rr   expire)r   Z
cache_reprri   r   r   r   r*   N  s    
z_TimedCache.__repr__c                 C   s<   | j "}| | || W  d    S 1 s.0    Y  d S r+   rs   )r   Z	cache_lenri   r   r   r   r8   S  s    
z_TimedCache.__len__c                    s<   | j "}| | t jW  d    S 1 s.0    Y  d S r+   )rr   rt   superr>   rh   r'   r   r   r>   X  s    
z_TimedCache.currsizec                 C   s   | j S )z%The timer function used by the cache.)rr   r)   r   r   r   re   ^  s    z_TimedCache.timerc                 C   s>   | j $}| | t|  W d    n1 s00    Y  d S r+   )rr   rt   r   clearrh   r   r   r   rw   c  s    
z_TimedCache.clearc                 O   sB   | j ( tj| g|R i |W  d    S 1 s40    Y  d S r+   )rr   r   r:   r   argskwargsr   r   r   r:   h  s    z_TimedCache.getc                 O   sB   | j ( tj| g|R i |W  d    S 1 s40    Y  d S r+   )rr   r   r   rx   r   r   r   r   l  s    z_TimedCache.popc                 O   sB   | j ( tj| g|R i |W  d    S 1 s40    Y  d S r+   )rr   r   r=   rx   r   r   r   r=   p  s    z_TimedCache.setdefault)r   r   r   r?   rm   ri   	monotonicr&   r   r*   r8   rA   r>   re   rw   r:   r   r=   __classcell__r   r   rv   r   rb   +  s   
rb   c                   @   s   e Zd ZdZG dd dZejdfddZdd Ze	j
fd	d
Z
e	jfddZe	jfddZdd Zdd Zedd ZdddZdd Zdd ZdS )r   z@LRU Cache implementation with per-item time-to-live (TTL) value.c                   @   s*   e Zd ZdZd	ddZdd Zdd ZdS )
zTTLCache._Link)r/   expiresrL   prevNc                 C   s   || _ || _d S r+   )r/   r}   r   r/   r}   r   r   r   r&   |  s    zTTLCache._Link.__init__c                 C   s   t j| j| jffS r+   )r   _Linkr/   r}   r)   r   r   r   rn     s    zTTLCache._Link.__reduce__c                 C   s   | j }| j}||_ ||_d S r+   )rL   r~   )r   rL   r~   r   r   r   unlink  s    zTTLCache._Link.unlink)NN)r   r   r   r   r&   rn   r   r   r   r   r   r   x  s   
r   Nc                 C   s>   t | ||| t  | _}| |_|_t | _	|| _
d S r+   )rb   r&   r   r   _TTLCache__rootr~   rL   rC   rD   _TTLCache__links_TTLCache__ttl)r   r%   ttlre   r   rootr   r   r   r&     s
    
zTTLCache.__init__c                 C   s6   z| j | }W n ty"   Y dS 0 |  |jk S d S NF)r   r,   re   r}   )r   r/   linkr   r   r   r4     s
    zTTLCache.__contains__c                 C   sR   z|  |}W n ty$   d}Y n0 |  |jk  }|rD| |S || |S d S r   )_TTLCache__getlinkr,   re   r}   r-   )r   r/   rS   r   expiredr   r   r   r     s    

zTTLCache.__getitem__c                 C   s   | j &}| | || || W d    n1 s20    Y  z| |}W n& typ   t| | j|< }Y n
0 |  || j |_	| j
 |_}|j |_}| |_|_d S r+   )re   rt   r   r,   r   r   r   r   r   r}   r   rL   r~   )r   r/   r   rH   ri   r   r   r~   r   r   r   r     s    
*zTTLCache.__setitem__c                 C   s8   || | | j |}|  |  |jk s4t|d S r+   )r   r   r   re   r}   r,   )r   r/   rJ   r   r   r   r   r3     s
    
zTTLCache.__delitem__c                 c   sX   | j }|j}||urT| j"}||jk r.|jV  W d    n1 sB0    Y  |j}qd S r+   )r   rL   re   r}   r/   )r   r   currri   r   r   r   r6     s    
&zTTLCache.__iter__c                 C   sj   | j | | j}| |_|_t| j dd dD ]"}||_|j |_}| |_|_q4| | 	  d S )Nc                 S   s   | j S r+   r}   )objr   r   r   <lambda>      z'TTLCache.__setstate__.<locals>.<lambda>)r/   )
__dict__updater   r~   rL   sortedr   valuesrt   re   )r   stater   r   r~   r   r   r   __setstate__  s    zTTLCache.__setstate__c                 C   s   | j S )z,The time-to-live value of the cache's items.)r   r)   r   r   r   r     s    zTTLCache.ttlc                 C   sf   |du r|   }| j}|j}| j}tj}||urb||jk sb|| |j ||j= |j}|  |}q(dS )$Remove expired items from the cache.N)	re   r   rL   r   r   r3   r}   r/   r   )r   ri   r   r   linksrJ   rL   r   r   r   rt     s    zTTLCache.expirec              	   C   s   | j p}| | ztt| j}W n& tyJ   tdt| j dY n0 || 	|fW  d   S W d   n1 s|0    Y  dS zmRemove and return the `(key, value)` pair least recently used that
        has not already expired.

        rK   N)
re   rt   rL   r5   r   rM   r,   rN   r   r   r   ri   r/   r   r   r   r1     s    
zTTLCache.popitemc                 C   s   | j | }| j | |S r+   )r   rF   r   r/   r   r   r   r   Z	__getlink  s    
zTTLCache.__getlink)N)r   r   r   r?   r   ri   r{   r&   r4   r   r   r   r3   r6   r   rA   r   rt   r1   r   r   r   r   r   r   u  s   



r   c                   @   s   e Zd ZdZejG dd dZejdfddZ	dd Z
ejfd	d
ZejfddZejfddZdd Zedd ZdddZdd Zdd ZdS )r   z;Time aware Least Recently Used (TLRU) cache implementation.c                   @   s"   e Zd ZdZdddZdd ZdS )zTLRUCache._Itemr/   r}   removedNc                 C   s   || _ || _d| _d S r   r   r   r   r   r   r&     s    zTLRUCache._Item.__init__c                 C   s   | j |j k S r+   r   )r   otherr   r   r   __lt__  s    zTLRUCache._Item.__lt__)NN)r   r   r   r   r&   r   r   r   r   r   _Item  s   
r   Nc                 C   s*   t | ||| t | _g | _|| _d S r+   )rb   r&   rC   rD   _TLRUCache__items_TLRUCache__order_TLRUCache__ttu)r   r%   tture   r   r   r   r   r&     s    
zTLRUCache.__init__c                 C   s6   z| j | }W n ty"   Y dS 0 |  |jk S d S r   )r   r,   re   r}   )r   r/   itemr   r   r   r4     s
    zTLRUCache.__contains__c                 C   sR   z|  |}W n ty$   d}Y n0 |  |jk  }|rD| |S || |S d S r   )_TLRUCache__getitemr,   re   r}   r-   )r   r/   rS   r   r   r   r   r   r     s    

zTLRUCache.__getitem__c                 C   s   | j L}| |||}||k s.W d    d S | | || || W d    n1 sX0    Y  zd| |_W n ty   Y n0 t|| | j|< }t	
| j| d S NT)re   r   rt   r   r   r,   r   r   r   heapqheappushr   )r   r/   r   rH   ri   r}   r   r   r   r   r      s    
*zTLRUCache.__setitem__c                 C   sX   | j }|| | W d    n1 s&0    Y  | j|}d|_||jk sTt|d S r   )re   r   r   r   r}   r,   )r   r/   rJ   ri   r   r   r   r   r3   0  s    (
zTLRUCache.__delitem__c              	   c   sN   | j D ]B}| j(}||jk r*|js*|jV  W d    q1 s>0    Y  qd S r+   )r   re   r}   r   r/   )r   r   ri   r   r   r   r6   9  s    
zTLRUCache.__iter__c                 C   s   | j S )z1The local time-to-use function used by the cache.)r   r)   r   r   r   r   @  s    zTLRUCache.ttuc                 C   s   |du r|   }| j}| j}t|t|d krNdd |D  | _}t| tj}|r|d jsp||d j	k st
|}|jsT|| |j ||j= qTdS )r   N   c                 S   s   g | ]}|j s|qS r   )r   ).0r   r   r   r   
<listcomp>M  r   z$TLRUCache.expire.<locals>.<listcomp>r   )re   r   r   r7   r   heapifyr   r3   r   r}   heappopr/   )r   ri   itemsorderrJ   r   r   r   r   rt   E  s    

zTLRUCache.expirec              	   C   s   | j n}| | ztt| j}W n$ tyH   td| jj dY n0 || 	|fW  d   S W d   n1 sz0    Y  dS r   )
re   rt   rL   r5   r   rM   r,   r'   r   r   r   r   r   r   r1   V  s    
zTLRUCache.popitemc                 C   s   | j | }| j | |S r+   )r   rF   r   r   r   r   Z	__getitemd  s    
zTLRUCache.__getitem)N)r   r   r   r?   	functoolstotal_orderingr   ri   r{   r&   r4   r   r   r   r3   r6   rA   r   rt   r1   r   r   r   r   r   r     s   	

r   c                    s    fdd}|S )z_Decorator to wrap a function with a memoizing callable that saves
    results in a cache.

    c                    s   d u r fdd}dd }nFd u rD fdd}fdd}n  fdd}fdd}|_ |_|_||_t| S )	Nc                     s    | i |S r+   r   )ry   rz   funcr   r   wrappers  s    z*cached.<locals>.decorator.<locals>.wrapperc                   S   s   d S r+   r   r   r   r   r   rw   v  s    z(cached.<locals>.decorator.<locals>.clearc                     s^   | i |}z
 | W S  t y*   Y n0 | i |}z| |< W n tyX   Y n0 |S r+   r,   r0   ry   rz   kv)cacher   r/   r   r   r   {  s    
c                      s       d S r+   rw   r   r   r   r   rw     s    c                     s   | i |}z2  | W  d    W S 1 s40    Y  W n t yR   Y n0 | i |}z6  ||W  d    W S 1 s0    Y  W n ty   | Y S 0 d S r+   r,   r=   r0   r   )r   r   r/   lockr   r   r     s    ,0c                      s0        W d    n1 s"0    Y  d S r+   r   r   r   r   r   r   rw     s    r   	cache_keyZ
cache_lockcache_clearr   update_wrapper)r   r   rw   r   r/   r   r   r   	decoratorp  s    
zcached.<locals>.decoratorr   r   r/   r   r   r   r   r   r	   j  s    7r	   c                    s    fdd}|S )zoDecorator to wrap a class or instance method with a memoizing
    callable that saves results in a cache.

    c                    sj   d u r& fdd}fdd}n  fdd}fdd}|_ |_|_||_t| S )Nc                    s    | }|d u r&| g|R i |S | g|R i |}z
|| W S  t yX   Y n0 | g|R i |}z|||< W n ty   Y n0 |S r+   r   r   ry   rz   cr   r   )r   r/   methodr   r   r     s    
z0cachedmethod.<locals>.decorator.<locals>.wrapperc                    s    | }|d ur|   d S r+   r   r   r   r   r   r   rw     s    z.cachedmethod.<locals>.decorator.<locals>.clearc                    s    | }|d u r&| g|R i |S | g|R i |}z6|  || W  d    W S 1 sf0    Y  W n t y   Y n0 | g|R i |}z:|  |||W  d    W S 1 s0    Y  W n t y   | Y S 0 d S r+   r   r   )r   r/   r   r   r   r   r     s    
,
0c                    sD    | }|d ur@|  |   W d    n1 s60    Y  d S r+   r   r   r   r   r   rw     s    
r   )r   r   rw   r   )r   r   r     s    zcachedmethod.<locals>.decoratorr   r   r   r   r   r
     s    9r
   )r?   __all____version__rC   collections.abcr   r   ra   ri    r   r   abcMutableMappingr   r   r   r   r   r   rb   r   r   Zhashkeyr	   Z	methodkeyr
   r   r   r   r   <module>   s,   f%%J t@