o
    Rcv                     @   sp  d Z ddlmZ ddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddlm
Z
 dd	lmZ dd
lmZ ddlmZ e ZG dd deZdd ZzddlmZ eje W n eyk   e Zdd ZY nw dd Ze ZG dd deeZG dd deZG dd deZG dd deeZdKddZ ereZ!e Z"ndKdd 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#Z*eZ+e$Z,e(ej-Z.e(d/d0 Z/dKd1d2Z0G d3d4 d4eZ1d5d6 Z2dKd7d8Z3d9d: Z4d;d< Z5d=d> Z6dKd?d@Z7dAdB Z8G dCdD dDeZ9G dEdF dFeZ:G dGdH dHe:Z;dIdJ Z<dS )LzCollection classes and helpers.    )absolute_importN   )binary_types)collections_abc)itertools_filterfalse)py2k)py37)string_types)	threadingc                   @   s    e Zd Zdd Ze Z ZZdS )ImmutableContainerc                 O   s   t d| jj )Nz%s object is immutable)	TypeError	__class____name__)selfargkw r   >D:\Flask\env\Lib\site-packages\sqlalchemy/util/_collections.py
_immutable      zImmutableContainer._immutableN)r   
__module____qualname__r   __delitem____setitem____setattr__r   r   r   r   r      s    r   c                  C   s   G dd dt t} | S )Nc                   @   s^   e Zd Zej Z Z Z ZZ	dd Z
dd Zdd Zddd	Zdd
dZdd Zdd ZdS )z1_immutabledict_py_fallback.<locals>.immutabledictc                 W   s    t | }t j|g|R   |S N)dict__new____init__clsargsnewr   r   r   r   *   s   
z9_immutabledict_py_fallback.<locals>.immutabledict.__new__c                 W   s   d S r   r   )r   r!   r   r   r   r   /   s   z:_immutabledict_py_fallback.<locals>.immutabledict.__init__c                 S      t t| ffS r   )_immutabledict_reconstructorr   r   r   r   r   
__reduce__2      z<_immutabledict_py_fallback.<locals>.immutabledict.__reduce__Nc                 S   s0   |s| S t | j}t ||  t || |S r   r   r   r   r   update)r   _immutabledict__dr"   r   r   r   union5   s   z7_immutabledict_py_fallback.<locals>.immutabledict.unionc                 [   sD   |s|s| S t | j}t ||  |rt || t || |S r   r(   )r   r*   r   r"   r   r   r   _union_w_kw>   s   z=_immutabledict_py_fallback.<locals>.immutabledict._union_w_kwc                 W   sN   d }|D ]}|r|d u rt | j}t ||  t || q|d u r%| S |S r   r(   )r   Zdictsr"   dr   r   r   
merge_withJ   s   z<_immutabledict_py_fallback.<locals>.immutabledict.merge_withc                 S      dt |  S )Nzimmutabledict(%s)r   __repr__r%   r   r   r   r1   W   r'   z:_immutabledict_py_fallback.<locals>.immutabledict.__repr__r   )r   r   r   r   r   clearpoppopitem
setdefaultr)   r   r   r&   r+   r,   r.   r1   r   r   r   r   immutabledict$   s    

	r6   )r   r   r6   r   r   r   _immutabledict_py_fallback#   s   6r8   r7   c                  G   s   t |  S )zdo the pickle dancer7   r   r   r   r   r$   e   s   r$   c                 C   s   | st S t| tr| S t| S r   )
EMPTY_DICT
isinstancer6   r-   r   r   r   coerce_to_immutabledictj   s
   
r=   c                   @   sN   e Zd ZdZej Z Z Z Z	Z
dd Zdd Zdd Zdd	 Zd
d ZdS )
FacadeDictz*A dictionary that is not publicly mutable.c                 G   s   t | }|S r   )r   r   r   r   r   r   r   {   s   
zFacadeDict.__new__c                 C      t d)Nz\an immutabledict shouldn't need to be copied.  use dict(d) if you need a mutable dictionary.)NotImplementedErrorr%   r   r   r   copy   s   zFacadeDict.copyc                 C   r#   r   )r>   r   r%   r   r   r   r&      r'   zFacadeDict.__reduce__c                 C   s   t | || dS )z,insert an item into the dictionary directly.N)r   r   r   keyvaluer   r   r   _insert_item      zFacadeDict._insert_itemc                 C   r/   )NzFacadeDict(%s)r0   r%   r   r   r   r1      r'   zFacadeDict.__repr__N)r   r   r   __doc__r   r   r2   r3   r4   r5   r)   r   rA   r&   rE   r1   r   r   r   r   r>   v   s    r>   c                       s   e Zd ZdZdZdd Zdd Zdd Z f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 Zdd Zdd  Zd.d"d#Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Z  ZS )/
Propertiesz8Provide a __getattr__/__setattr__ interface over a dict._datac                 C   s   t | d| d S NrJ   objectr   )r   datar   r   r   r         zProperties.__init__c                 C   
   t | jS r   )lenrJ   r%   r   r   r   __len__      
zProperties.__len__c                 C   s   t t| j S r   )iterlistrJ   valuesr%   r   r   r   __iter__   rO   zProperties.__iter__c                    s"   t tt| dd | j D  S )Nc                 S   s   g | ]}t |qS r   )str.0kr   r   r   
<listcomp>   s    z&Properties.__dir__.<locals>.<listcomp>)dirsuperrH   rJ   keysr%   r   r   r   __dir__   s   zProperties.__dir__c                 C   s   t | t | S r   rU   r   otherr   r   r   __add__   r   zProperties.__add__c                 C      || j |< d S r   rI   r   rC   objr   r   r   r      r'   zProperties.__setitem__c                 C   
   | j | S r   rI   r   rC   r   r   r   __getitem__   rS   zProperties.__getitem__c                 C   s   | j |= d S r   rI   rj   r   r   r   r         zProperties.__delitem__c                 C   rf   r   rI   rg   r   r   r   r      r'   zProperties.__setattr__c                 C   s
   d| j iS rK   rI   r%   r   r   r   __getstate__   rS   zProperties.__getstate__c                 C   s   t | d|d  d S rK   rL   )r   stater   r   r   __setstate__      zProperties.__setstate__c                 C   s$   z| j | W S  ty   t|w r   )rJ   KeyErrorAttributeErrorrj   r   r   r   __getattr__   s
   zProperties.__getattr__c                 C   
   || j v S r   rI   rj   r   r   r   __contains__   rS   zProperties.__contains__c                 C   rP   )z8Return an immutable proxy for this :class:`.Properties`.)ImmutablePropertiesrJ   r%   r   r   r   as_immutable   s   
zProperties.as_immutablec                 C   s   | j | d S r   )rJ   r)   r   rD   r   r   r   r)      r   zProperties.updateNc                 C   s   || v r| | S |S r   r   )r   rC   defaultr   r   r   get   s   zProperties.getc                 C   rP   r   )rU   rJ   r%   r   r   r   r_      rS   zProperties.keysc                 C      t | j S r   )rU   rJ   rV   r%   r   r   r   rV      r'   zProperties.valuesc                 C   r{   r   )rU   rJ   itemsr%   r   r   r   r|      r'   zProperties.itemsc                 C   rt   r   rI   rj   r   r   r   has_key   rS   zProperties.has_keyc                 C      | j   d S r   )rJ   r2   r%   r   r   r   r2      r'   zProperties.clearr   )r   r   r   rG   	__slots__r   rR   rW   ra   re   r   rk   r   r   rm   ro   rs   ru   rw   r)   rz   r_   rV   r|   r}   r2   __classcell__r   r   r`   r   rH      s0    
rH   c                   @   s   e Zd ZdZdZdd ZdS )OrderedPropertieszUProvide a __getattr__/__setattr__ interface with an OrderedDict
    as backing store.r   c                 C   s   t | t  d S r   )rH   r   OrderedDictr%   r   r   r   r      rO   zOrderedProperties.__init__N)r   r   r   rG   r   r   r   r   r   r   r      s    r   c                   @   s   e Zd ZdZdZdS )rv   zDProvide immutable dict/object attribute to an underlying dictionary.r   N)r   r   r   rG   r   r   r   r   r   rv      s    rv   c                    s0    fddt  |dD }    | dS )zSort an OrderedDict in-place.c                       g | ]}| | fqS r   r   rY   r<   r   r   r\          z,_ordered_dictionary_sort.<locals>.<listcomp>rC   N)sortedr2   r)   )r-   rC   r|   r   r<   r   _ordered_dictionary_sort   s   r   c                 C   s   | j |d dS )zSort an OrderedDict in place.r   Nr   )r-   rC   r   r   r   sort_dictionary  s   r   c                   @   s   e Zd ZdZdZd*ddZdd Zd*dd	Zd
d Zdd Z	dd Z
d*ddZdd Zdd Zdd Zdd Zdd ZerIdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) ZdS )+r   zhDictionary that maintains insertion order.

        Superseded by Python dict as of Python 3.7

        _listNc                 C   s   t | |d d S )Nr   r   rj   r   r   r   r     r   z$OrderedDict._ordered_dictionary_sortc                 C   s   t |  ffS r   )r   r|   r%   r   r   r   r&     r'   zOrderedDict.__reduce__c                 K   s@   g | _ |d u r|r| jdi | d S d S | j|fi | d S )Nr   )r   r)   )r   _OrderedDict____sequencekwargsr   r   r   r     s   zOrderedDict.__init__c                 C   s   g | _ t|  d S r   )r   r   r2   r%   r   r   r   r2        zOrderedDict.clearc                 C   s   |   S r   )__copy__r%   r   r   r   rA   !     zOrderedDict.copyc                 C      t | S r   )r   r%   r   r   r   r   $  r   zOrderedDict.__copy__c                 K   s^   |d ur$t |dr| D ]
}| |||  qn|D ]\}}|| |< q|r-| | d S d S )Nr_   )hasattrr_   r   r)   )r   r   r   rC   rD   r   r   r   r)   '  s   

zOrderedDict.updatec                 C   s"   || vr|  || |S | |S r   )r   rk   rB   r   r   r   r5   2  s   
zOrderedDict.setdefaultc                 C   rP   r   rT   r   r%   r   r   r   rW   9  rS   zOrderedDict.__iter__c                 C   r   r   rb   r%   r   r   r   r_   <  r   zOrderedDict.keysc                        fdd j D S )Nc                    s   g | ]} | qS r   r   rZ   rC   r%   r   r   r\   @      z&OrderedDict.values.<locals>.<listcomp>r   r%   r   r%   r   rV   ?     zOrderedDict.valuesc                    r   )Nc                    r   r   r   r   r%   r   r   r\   C  r   z%OrderedDict.items.<locals>.<listcomp>r   r%   r   r%   r   r|   B  r   zOrderedDict.itemsc                 C      t |  S r   )rT   rV   r%   r   r   r   
itervaluesG  rl   zOrderedDict.itervaluesc                 C   r   r   )rT   r%   r   r   r   iterkeysJ  r   zOrderedDict.iterkeysc                 C   r   r   )rT   r|   r%   r   r   r   	iteritemsM  rl   zOrderedDict.iteritemsc                 C   sF   || vrz| j | W n ty   |g| _ Y nw t| || d S r   )r   appendrr   r   r   rg   r   r   r   r   P  s   zOrderedDict.__setitem__c                 C   s   t | | | j| d S r   )r   r   r   removerj   r   r   r   r   Z     zOrderedDict.__delitem__c                 G   s0   || v }t j| |g|R  }|r| j| |S r   )r   r3   r   r   )r   rC   ry   ZpresentrD   r   r   r   r3   ^  s
   zOrderedDict.popc                 C   s   t | }| j|d  |S Nr   )r   r4   r   r   r   itemr   r   r   r4   e  s   
zOrderedDict.popitemr   )r   r   r   rG   r   r   r&   r   r2   rA   r   r)   r5   rW   r_   rV   r|   r   r   r   r   r   r   r3   r4   r   r   r   r   r     s.    



r   c                   @   s   e Zd 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 ZeZdd ZeZdd ZeZdd ZeZdd ZeZdd ZeZd d! ZeZd"d# ZeZd$d% ZeZdS )'
OrderedSetNc                 C   s8   t |  |d urt|| _t | | j d S g | _d S r   )setr   unique_listr   r)   )r   r-   r   r   r   r   l  s
   


zOrderedSet.__init__c                 C   s$   || vr
| j | t| | d S r   r   r   r   addr   elementr   r   r   r   t  s   zOrderedSet.addc                 C   s   t | | | j| d S r   )r   r   r   r   r   r   r   r   y  r   zOrderedSet.removec                 C   s&   || vr| j || t| | d S r   )r   insertr   r   )r   posr   r   r   r   r   }  s   zOrderedSet.insertc                 C   s(   || v r| j | t| | d S d S r   )r   r   r   r   r   r   r   discard  s   zOrderedSet.discardc                 C   s   t |  g | _d S r   )r   r2   r   r%   r   r   r   r2     s   

zOrderedSet.clearc                 C   ri   r   r   rj   r   r   r   rk     rS   zOrderedSet.__getitem__c                 C   rP   r   r   r%   r   r   r   rW     rS   zOrderedSet.__iter__c                 C   s
   |  |S r   )r+   rc   r   r   r   re     rS   zOrderedSet.__add__c                 C   s   d| j j| jf S Nz%s(%r))r   r   r   r%   r   r   r   r1     rO   zOrderedSet.__repr__c                 C   s.   |D ]}|| vr| j | t| | q| S r   r   )r   iterableer   r   r   r)     s   zOrderedSet.updatec                 C   s   |  | }|| |S r   )r   r)   r   rd   resultr   r   r   r+     s   

zOrderedSet.unionc                        t   |  fdd| D S )Nc                 3   s    | ]	}| v r|V  qd S r   r   rZ   ard   r   r   	<genexpr>      z*OrderedSet.intersection.<locals>.<genexpr>r   r   rc   r   r   r   intersection     zOrderedSet.intersectionc                    s<   t    fddD }|fdd D  |S )Nc                 3       | ]	}| vr|V  qd S r   r   r   r   r   r   r     r   z2OrderedSet.symmetric_difference.<locals>.<genexpr>c                 3   r   r   r   r   r%   r   r   r     r   )r   r   r)   r   r   )rd   r   r   symmetric_difference  s   zOrderedSet.symmetric_differencec                    r   )Nc                 3   r   r   r   r   r   r   r   r     r   z(OrderedSet.difference.<locals>.<genexpr>r   rc   r   r   r   
difference  r   zOrderedSet.differencec                    s.   t   t |    fdd| jD | _| S )Nc                       g | ]}| v r|qS r   r   r   r   r   r   r\     r   z2OrderedSet.intersection_update.<locals>.<listcomp>)r   intersection_updater   rc   r   r   r   r     s   zOrderedSet.intersection_updatec                    sD   t  |  fdd jD  _  j fdd|jD 7  _ S )Nc                    r   r   r   r   r%   r   r   r\     r   z:OrderedSet.symmetric_difference_update.<locals>.<listcomp>c                    r   r   r   r   r%   r   r   r\     r   )r   symmetric_difference_updater   rc   r   r%   r   r     s   z&OrderedSet.symmetric_difference_updatec                    s&   t  |  fdd jD  _ S )Nc                    r   r   r   r   r%   r   r   r\     r   z0OrderedSet.difference_update.<locals>.<listcomp>)r   difference_updater   rc   r   r%   r   r     s   zOrderedSet.difference_updater   )r   r   r   r   r   r   r   r   r2   rk   rW   re   r1   __str__r)   __ior__r+   __or__r   __and__r   __xor__r   __sub__r   __iand__r   __ixor__r   __isub__r   r   r   r   r   k  s8    
r   c                   @   s>  e Zd ZdZdMd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 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( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#dCdD Z$e$Z%dEdF Z&dGdH Z'dIdJ Z(dKdL Z)dS )NIdentitySetzA set that considers only object id() for uniqueness.

    This strategy has edge cases for builtin types- it's possible to have
    two 'foo' strings in one of these sets, for example.  Use sparingly.

    Nc                 C   s   t  | _|r| | d S d S r   )r   _membersr)   r   r   r   r   r   r     s   zIdentitySet.__init__c                 C   s   || j t|< d S r   r   idrx   r   r   r   r     rO   zIdentitySet.addc                 C   s   t || jv S r   )r   r   rx   r   r   r   ru     r'   zIdentitySet.__contains__c                 C   s   | j t|= d S r   r   rx   r   r   r   r     r   zIdentitySet.removec                 C   s&   z|  | W d S  ty   Y d S w r   )r   rq   rx   r   r   r   r     s
   zIdentitySet.discardc                 C   s,   z
| j  }|d W S  ty   tdw )Nr   zpop from an empty set)r   r4   rq   )r   pairr   r   r   r3     s   

zIdentitySet.popc                 C   r~   r   )r   r2   r%   r   r   r   r2     r'   zIdentitySet.clearc                 C   r?   )Nzcannot compare sets using cmp()r   rc   r   r   r   __cmp__  r   zIdentitySet.__cmp__c                 C   s   t |tr| j|jkS dS )NFr;   r   r   rc   r   r   r   __eq__     
zIdentitySet.__eq__c                 C   s   t |tr| j|jkS dS NTr   rc   r   r   r   __ne__  r   zIdentitySet.__ne__c                 C   sV   t || jr	|}n| |}t| t|krdS t|jjt| j D ]} dS dS NFTr;   r   rQ   r   r   ru   rT   r_   r   r   rd   mr   r   r   issubset
  s   
zIdentitySet.issubsetc                 C      t |tstS | |S r   )r;   r   NotImplementedr   rc   r   r   r   __le__     

zIdentitySet.__le__c                 C   s(   t |tstS t| t|k o| |S r   )r;   r   r   rQ   r   rc   r   r   r   __lt__     
zIdentitySet.__lt__c                 C   sV   t || jr	|}n| |}t| t|k rdS t| jjt|j D ]} dS dS r   r   r   r   r   r   
issuperset"  s   
zIdentitySet.issupersetc                 C   r   r   )r;   r   r   r   rc   r   r   r   __ge__1  r   zIdentitySet.__ge__c                 C   s(   t |tstS t| t|ko| |S r   )r;   r   r   rQ   r   rc   r   r   r   __gt__6  r   zIdentitySet.__gt__c                 C   s4   |   }| j}|j| |jdd |D  |S )Nc                 s       | ]	}t ||fV  qd S r   r   rZ   rh   r   r   r   r   ?  r   z$IdentitySet.union.<locals>.<genexpr>)r   r   r)   r   r   r   membersr   r   r   r+   ;  s
   zIdentitySet.unionc                 C   r   r   )r;   r   r   r+   rc   r   r   r   r   B  r   zIdentitySet.__or__c                 C   s   | j dd |D  d S )Nc                 s   r   r   r   r   r   r   r   r   H  r   z%IdentitySet.update.<locals>.<genexpr>)r   r)   r   r   r   r   r)   G     zIdentitySet.updatec                 C      t |tstS | | | S r   )r;   r   r   r)   rc   r   r   r   r   J     

zIdentitySet.__ior__c                    Z   |   }| j}t|| j rt|j  ndd |D  |j fdd| D  |S )Nc                 S      h | ]}t |qS r   r   r   r   r   r   	<setcomp>V  r   z)IdentitySet.difference.<locals>.<setcomp>c                 3   $    | ]\}}| vr||fV  qd S r   r   rZ   r[   vr   r   r   r   X     " z)IdentitySet.difference.<locals>.<genexpr>r   r   r;   r   r_   r)   r|   r   r   r   r   r   P  s   zIdentitySet.differencec                 C   r   r   )r;   r   r   r   rc   r   r   r   r   \  r   zIdentitySet.__sub__c                 C      |  |j| _d S r   )r   r   r   r   r   r   r   a  rO   zIdentitySet.difference_updatec                 C   r   r   )r;   r   r   r   rc   r   r   r   r   d  r   zIdentitySet.__isub__c                    r   )Nc                 S   r   r   r   r   r   r   r   r   p  r   z+IdentitySet.intersection.<locals>.<setcomp>c                 3   s$    | ]\}}| v r||fV  qd S r   r   r   r   r   r   r   q  s    
z+IdentitySet.intersection.<locals>.<genexpr>r   r   r   r   r   r   j  s   zIdentitySet.intersectionc                 C   r   r   )r;   r   r   r   rc   r   r   r   r   v  r   zIdentitySet.__and__c                 C   r   r   )r   r   r   r   r   r   r   {  rO   zIdentitySet.intersection_updatec                 C   r   r   )r;   r   r   r   rc   r   r   r   r   ~  r   zIdentitySet.__iand__c                    sp   |   }| j t|| j r|jndd |D |jfdd  D  |j fdd D  |S )Nc                 S   s   i | ]}t ||qS r   r   r   r   r   r   
<dictcomp>      z4IdentitySet.symmetric_difference.<locals>.<dictcomp>c                 3   r   r   r   r   r   r   r   r     r   z3IdentitySet.symmetric_difference.<locals>.<genexpr>c                 3   r   r   r   r   )r   r   r   r     r   )r   r   r;   r)   r|   )r   r   r   r   )r   rd   r   r     s   z IdentitySet.symmetric_differencec                 C   r   r   r;   r   r   r   rc   r   r   r   r     r   zIdentitySet.__xor__c                 C   r   r   )r   r   r   r   r   r   r     rO   z'IdentitySet.symmetric_difference_updatec                 C   r   r   r   rc   r   r   r   r     r   zIdentitySet.__ixor__c                 C   s   t | t| j S r   )typerT   r   rV   r%   r   r   r   rA     rp   zIdentitySet.copyc                 C   rP   r   )rQ   r   r%   r   r   r   rR     rS   zIdentitySet.__len__c                 C   r{   r   )rT   r   rV   r%   r   r   r   rW     r'   zIdentitySet.__iter__c                 C   r?   )Nzset objects are unhashabler   r%   r   r   r   __hash__  r   zIdentitySet.__hash__c                 C   s   dt | jt| j f S r   )r   r   rU   r   rV   r%   r   r   r   r1     s   zIdentitySet.__repr__r   )*r   r   r   rG   r   r   ru   r   r   r3   r2   r   r   r   r   r   r   r   r   r   r+   r   r)   r   r   r   r   r   r   r   r   r   r   r   r   r   rA   r   rR   rW   r   r1   r   r   r   r   r     sP    
r   c                   @   s6   e Zd ZdddZdd Zdd Zdd	 Zd
d ZdS )WeakSequencer   c                    s0   t | fdd  | _ fdd|D | _d S )Nc                 S   s"   | }|d ur|j |  d S d S r   )_storager   )r   selfrefr   r   r   r   _remove  s   z&WeakSequence.__init__.<locals>._removec                    s   g | ]}t | qS r   )weakrefref)rZ   r   r   r   r   r\     s    z)WeakSequence.__init__.<locals>.<listcomp>)r   r   r   r   )r   Z_WeakSequence__elementsr   r  r   r     s
   
zWeakSequence.__init__c                 C   s   | j t|| j d S r   )r   r   r   r   r   r   r   r   r   r     r   zWeakSequence.appendc                 C   rP   r   )rQ   r   r%   r   r   r   rR     rS   zWeakSequence.__len__c                 C   s   dd dd | j D D S )Nc                 s   s    | ]	}|d ur|V  qd S r   r   r   r   r   r   r     s    
z(WeakSequence.__iter__.<locals>.<genexpr>c                 s   s    | ]}| V  qd S r   r   )rZ   r   r   r   r   r     s    )r   r%   r   r   r   rW     s   zWeakSequence.__iter__c                 C   s.   z	| j | }W | S  ty   td| w )NzIndex %s out of range)r   rq   
IndexError)r   indexrh   r   r   r   rk     s   zWeakSequence.__getitem__N)r   )r   r   r   r   r   rR   rW   rk   r   r   r   r   r     s    
r   c                   @   s   e Zd ZdddZdS )OrderedIdentitySetNc                 C   s2   t |  t | _|r|D ]	}| | qd S d S r   )r   r   r   r   r   )r   r   or   r   r   r     s   
zOrderedIdentitySet.__init__r   )r   r   r   r   r   r   r   r   r    s    r  c                   @       e Zd ZdZdd Zdd ZdS )PopulateDictzA dict which populates missing values via a creation function.

    Note the creation function takes a key, unlike
    collections.defaultdict.

    c                 C   s
   || _ d S r   creator)r   r	  r   r   r   r     rS   zPopulateDict.__init__c                 C   s   |  | | |< }|S r   r  r   rC   valr   r   r   __missing__  s   zPopulateDict.__missing__Nr   r   r   rG   r   r  r   r   r   r   r    s    r  c                   @   r  )WeakPopulateDictzaLike PopulateDict, but assumes a self + a method and does not create
    a reference cycle.

    c                 C   s   |j | _|j}t|| _d S r   )__func__r	  __self__r   r   weakself)r   Zcreator_methodr  r   r   r   r     s   zWeakPopulateDict.__init__c                 C   s   |  |  | | |< }|S r   )r	  r  r
  r   r   r   r    s   zWeakPopulateDict.__missing__Nr  r   r   r   r   r    s    r  c                 C   s   t t| S r   )propertyoperator
itemgetter)idxr   r   r   <lambda>
  s    r  c                    s:   t  j sfdd| D S  fdd| D S )Nc                    s    g | ]}| vr|s|qS r   r   rZ   x)seenseen_addr   r   r\     s     zunique_list.<locals>.<listcomp>c                    s(   g | ]} |vr |s|qS r   r   r  hashfuncr  r  r   r   r\     s    

)r   r   )seqr  r   r  r   r     s   r   c                   @   s*   e Zd ZdZd	ddZdd Zdd ZdS )
UniqueAppenderzAppends items to a collection ensuring uniqueness.

    Additional appends() of the same object are ignored.  Membership is
    determined by identity (``is a``) not equality (``==``).
    Nc                 C   sP   || _ i | _|rt||| _d S t|dr|j| _d S t|dr&|j| _d S d S )Nr   r   )rN   _uniquegetattr_data_appenderr   r   r   )r   rN   Zviar   r   r   r   "  s   

zUniqueAppender.__init__c                 C   s.   t |}|| jvr| | d| j|< d S d S r   )r   r  r!  )r   r   Zid_r   r   r   r   ,  s
   

zUniqueAppender.appendc                 C   rP   r   )rT   rN   r%   r   r   r   rW   2  rS   zUniqueAppender.__iter__r   )r   r   r   rG   r   r   rW   r   r   r   r   r    s
    

r  c                 C   s,   t | dkrt| d tjrt| d S | S )Nr   r   )rQ   r;   typesGeneratorTyperU   r9   r   r   r   coerce_generator_arg6  s   r$  c                 C   sB   | d u r|S t | tjrt | tt r| gS t | tr| S t| S r   )r;   r   Iterabler	   r   rU   )r  ry   r   r   r   to_list=  s   
r&  c                 C   s   t | dd |D S )zreturn True if any items of set\_ are present in iterable.

    Goes through special effort to ensure __hash__ is not called
    on items in iterable that don't support it.

    c                 S   s   g | ]}|j r|qS r   )r   rZ   ir   r   r   r\   R  r   z$has_intersection.<locals>.<listcomp>)boolr   )set_r   r   r   r   has_intersectionJ  s   r+  c                 C   (   | d u rt  S t| t st t| S | S r   )r   r;   r&  r  r   r   r   to_setU  
   
r.  c                 C   r,  r   )
column_setr;   r&  r-  r   r   r   to_column_set^  r/  r1  c                 K   s*   |   } |r| | | jdi | | S )z5Copy the given dict and update with the given values.Nr   )rA   r)   )r-   _newr   r   r   r   update_copyg  s
   
r3  c                 c   s@    | D ]}t |tst|drt|D ]}|V  qq|V  qdS )zGiven an iterator of which further sub-elements may also be
    iterators, flatten the sub-elements into a single iterator.

    rW   N)r;   rX   r   flatten_iterator)r  elemyr   r   r   r4  q  s   r4  c                   @   sd   e Zd ZdZdZdddZdd	 Zdd
dZdd Zdd Z	dd Z
dd Zedd Zdd ZdS )LRUCachezDictionary with 'squishy' removal of least
    recently used items.

    Note that either get() or [] should be used here, but
    generally its not safe to do an "in" check first as the dictionary
    can change subsequent to that call.

    )capacity	threshold
size_alert_counter_mutexd         ?Nc                 C   s&   || _ || _|| _d| _t | _d S r   )r8  r9  r:  r;  r
   Lockr<  )r   r8  r9  r:  r   r   r   r     s
   zLRUCache.__init__c                 C   s   |  j d7  _ | j S Nr   )r;  r%   r   r   r   _inc_counter  s   zLRUCache._inc_counterc                 C   s.   t | ||}||ur|  |d< |d S |S N   r   )r   rz   rA  )r   rC   ry   r   r   r   r   rz     s
   zLRUCache.getc                 C   s    t | |}|  |d< |d S rB  )r   rk   rA  )r   rC   r   r   r   r   rk     s   zLRUCache.__getitem__c                 C   s   dd t | D S )Nc                 S   s   g | ]}|d  qS )r   r   r'  r   r   r   r\     r   z#LRUCache.values.<locals>.<listcomp>)r   rV   r%   r   r   r   rV     r   zLRUCache.valuesc                 C   s   || v r| | S || |< |S r   r   rB   r   r   r   r5     s   zLRUCache.setdefaultc                 C   sF   t | |}|d u r|||  g}t | || n||d< |   d S r@  )r   rz   rA  r   _manage_size)r   rC   rD   r   r   r   r   r     s   zLRUCache.__setitem__c                 C   s   | j | j | j  S r   )r8  r9  r%   r   r   r   size_threshold  rF   zLRUCache.size_thresholdc              	   C   s   | j dsd S z_t| j}t| | j| j| j  kr`|r$d}| |  tt	| t
ddd}|| jd  D ]}z| |d = W q8 tyJ   Y q8w t| | j| j| j  ksW | j   d S W | j   d S | j   w )NFrC  T)rC   reverser   )r<  acquirer)  r:  rQ   r8  r9  r   r   rV   r  r  rq   release)r   r:  Z
by_counterr   r   r   r   rD    s*   

zLRUCache._manage_size)r=  r>  Nr   )r   r   r   rG   r   r   rA  rz   rk   rV   r5   r   r  rE  rD  r   r   r   r   r7  ~  s    	

	
r7  c                   @   8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )ScopedRegistrya  A Registry that can store one or multiple instances of a single
    class on the basis of a "scope" function.

    The object implements ``__call__`` as the "getter", so by
    calling ``myregistry()`` the contained object is returned
    for the current scope.

    :param createfunc:
      a callable that returns a new object to be placed in the registry

    :param scopefunc:
      a callable that will return a key to store/retrieve an object.
    c                 C   s   || _ || _i | _dS )aV  Construct a new :class:`.ScopedRegistry`.

        :param createfunc:  A creation function that will generate
          a new value for the current scope, if none is present.

        :param scopefunc:  A function that returns a hashable
          token representing the current scope (such as, current
          thread identifier).

        N)
createfunc	scopefuncregistry)r   rK  rL  r   r   r   r     s   
zScopedRegistry.__init__c                 C   s:   |   }z| j| W S  ty   | j||   Y S w r   )rL  rM  rq   r5   rK  rj   r   r   r   __call__  s   zScopedRegistry.__call__c                 C   s   |   | jv S )z9Return True if an object is present in the current scope.)rL  rM  r%   r   r   r   has  s   zScopedRegistry.hasc                 C   s   || j |  < dS )z$Set the value for the current scope.N)rM  rL  r   rh   r   r   r   r     s   zScopedRegistry.setc                 C   s(   z	| j |  = W dS  ty   Y dS w )z Clear the current scope, if any.N)rM  rL  rq   r%   r   r   r   r2     s
   zScopedRegistry.clearN	r   r   r   rG   r   rN  rO  r   r2   r   r   r   r   rJ    s    rJ  c                   @   rI  )ThreadLocalRegistryz\A :class:`.ScopedRegistry` that uses a ``threading.local()``
    variable for storage.

    c                 C   s   || _ t | _d S r   )rK  r
   localrM  )r   rK  r   r   r   r     r   zThreadLocalRegistry.__init__c                 C   s2   z| j jW S  ty   |   }| j _| Y S w r   )rM  rD   rr   rK  )r   r  r   r   r   rN    s   
zThreadLocalRegistry.__call__c                 C   s   t | jdS )NrD   )r   rM  r%   r   r   r   rO    rl   zThreadLocalRegistry.hasc                 C   s   || j _d S r   )rM  rD   rP  r   r   r   r     rl   zThreadLocalRegistry.setc                 C   s"   z| j `W d S  ty   Y d S w r   )rM  rD   rr   r%   r   r   r   r2     s
   zThreadLocalRegistry.clearNrQ  r   r   r   r   rR    s    rR  c                 C   s0   d}| D ]}||u r|d7 }|dkr dS qdS )zrGiven a sequence and search object, return True if there's more
    than one, False if zero or one of them.


    r   r   TFr   )sequencetargetcr   r   r   r   	has_dupes%  s   
rW  r   )=rG   
__future__r   r  r"  r   compatr   r   r   r   r   r	   r
   	frozenset	EMPTY_SETrM   r   r8   Zsqlalchemy.cimmutabledictr6   MappingregisterImportErrorr$   r=   r:   r   r>   rH   r   rv   r   r   r   r   r   r   r   r  r  r  r0  Zcolumn_dictZordered_column_setr  _gettersZ_property_gettersr   r  r$  r&  r+  r.  r1  r3  r4  r7  rJ  rR  rW  r   r   r   r   <module>   sz   :	O



ej _"	


	
	
Q8