o
    [h#r                     @   s.  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 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eZG dd deZG dd d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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)d* Z'dHd+d,Z(G d-d. d.eZ)d/d0 Z*dHd1d2Z+d3d4 Z,d5d6 Z-d7d8 Z.dHd9d:Z/d;d< Z0G d=d> d>eZ1e1d?Z2d@dA Z3G dBdC dCeZ4G dDdE dEe4Z5dFdG Z6dS )IzCollection classes and helpers.    )absolute_importN   )binary_types)collections_abc)itertools_filterfalse)py2k)string_types)	threadingc                   @   s   e Zd ZdZdd ZdS )AbstractKeyedTuple c                 C   
   t | jS )zReturn a list of string key names for this :class:`.KeyedTuple`.

        .. seealso::

            :attr:`.KeyedTuple._fields`

        )list_fieldsselfr   r   /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/sqlalchemy/util/_collections.pykeys   s   
	zAbstractKeyedTuple.keysN)__name__
__module____qualname__	__slots__r   r   r   r   r   r
      s    r
   c                   @   s6   e Zd ZdZdddZedd Zdd Zd	d
 ZdS )
KeyedTuplea  ``tuple`` subclass that adds labeled names.

    E.g.::

        >>> k = KeyedTuple([1, 2, 3], labels=["one", "two", "three"])
        >>> k.one
        1
        >>> k.two
        2

    Result rows returned by :class:`_query.Query` that contain multiple
    ORM entities and/or column expressions make use of this
    class to return rows.

    The :class:`.KeyedTuple` exhibits similar behavior to the
    ``collections.namedtuple()`` construct provided in the Python
    standard library, however is architected very differently.
    Unlike ``collections.namedtuple()``, :class:`.KeyedTuple` is
    does not rely on creation of custom subtypes in order to represent
    a new series of keys, instead each :class:`.KeyedTuple` instance
    receives its list of keys in place.   The subtype approach
    of ``collections.namedtuple()`` introduces significant complexity
    and performance overhead, which is not necessary for the
    :class:`_query.Query` object's use case.

    .. seealso::

        :ref:`ormtutorial_querying`

    Nc                 C   s6   t | |}|r|jt|| ng }||jd< |S )N_labels)tuple__new____dict__updatezip)clsvalslabelstr   r   r   r   J   s   
zKeyedTuple.__new__c                 C   s   t dd | jD S )zReturn a tuple of string key names for this :class:`.KeyedTuple`.

        This method provides compatibility with ``collections.namedtuple()``.

        .. seealso::

            :meth:`.KeyedTuple.keys`

        c                 S      g | ]}|d ur|qS Nr   ).0lr   r   r   
<listcomp>^       z&KeyedTuple._fields.<locals>.<listcomp>)r   r   r   r   r   r   r   S   s   zKeyedTuple._fieldsc                 C   s   t d| )NzCan't set attribute: %s)AttributeErrorr   keyvaluer   r   r   __setattr__`      zKeyedTuple.__setattr__c                    s    fdd   D S )zReturn the contents of this :class:`.KeyedTuple` as a dictionary.

        This method provides compatibility with ``collections.namedtuple()``,
        with the exception that the dictionary returned is **not** ordered.

        c                    s   i | ]}| j | qS r   )r   r$   r*   r   r   r   
<dictcomp>j   r'   z&KeyedTuple._asdict.<locals>.<dictcomp>)r   r   r   r   r   _asdictc   s   zKeyedTuple._asdictr#   )	r   r   r   __doc__r   propertyr   r,   r0   r   r   r   r   r   *   s    
	
r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	_LWr   c                 C   s   t | |S r#   )r   r   )r   r   r   r   r   r   p   r-   z_LW.__new__c                 C   s   t t| | jffS r#   )r   r   _real_fieldsr   r   r   r   
__reduce__s   s   z_LW.__reduce__c                 C   s    t t| j| }|dd |S )zAReturn the contents of this :class:`.KeyedTuple` as a dictionary.N)dictr   r4   popr   dr   r   r   r0   y   s   z_LW._asdictN)r   r   r   r   r   r5   r0   r   r   r   r   r3   m   s
    r3   c                   @   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__r   r   argkwr   r   r   
_immutable      zImmutableContainer._immutableN)r   r   r   r@   __delitem____setitem__r,   r   r   r   r   r:      s    r:   c                   @   sJ   e 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 )immutabledictc                 G   s    t | }t j|g|R   |S r#   )r6   r   __init__)r   argsnewr   r   r   r      s   
zimmutabledict.__new__c                 G   s   d S r#   r   )r   rF   r   r   r   rE      s   zimmutabledict.__init__c                 C   s   t t| ffS r#   )rD   r6   r   r   r   r   r5         zimmutabledict.__reduce__c                 C   s:   |s| S | st |tr|S t|S t| }t|| |S r#   )
isinstancerD   r6   r   )r   r9   Zd2r   r   r   union   s   
zimmutabledict.unionc                 C   s   dt |  S )Nzimmutabledict(%s))r6   __repr__r   r   r   r   rK      rH   zimmutabledict.__repr__N)r   r   r   r:   r@   clearr7   popitem
setdefaultr   r   rE   r5   rJ   rK   r   r   r   r   rD      s    rD   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 NrQ   objectr,   )r   datar   r   r   rE         zProperties.__init__c                 C   r   r#   )lenrQ   r   r   r   r   __len__      
zProperties.__len__c                 C   s   t t| j S r#   )iterr   rQ   valuesr   r   r   r   __iter__   rV   zProperties.__iter__c                    s"   t tt| dd | j D  S )Nc                 S   s   g | ]}t |qS r   )str)r$   kr   r   r   r&      s    z&Properties.__dir__.<locals>.<listcomp>)dirsuperrO   rQ   r   r   r<   r   r   __dir__   s   zProperties.__dir__c                 C   s   t | t | S r#   r   r   otherr   r   r   __add__   rA   zProperties.__add__c                 C      || j |< d S r#   rP   r   r*   objr   r   r   rC      rH   zProperties.__setitem__c                 C   
   | j | S r#   rP   r   r*   r   r   r   __getitem__   rY   zProperties.__getitem__c                 C   s   | j |= d S r#   rP   rk   r   r   r   rB      r-   zProperties.__delitem__c                 C   rg   r#   rP   rh   r   r   r   r,      rH   zProperties.__setattr__c                 C   s
   d| j iS rR   rP   r   r   r   r   __getstate__   rY   zProperties.__getstate__c                 C   s   t | d|d  d S rR   rS   )r   stater   r   r   __setstate__      zProperties.__setstate__c                 C   s$   z| j | W S  ty   t|w r#   )rQ   KeyErrorr(   rk   r   r   r   __getattr__   s
   zProperties.__getattr__c                 C   
   || j v S r#   rP   rk   r   r   r   __contains__   rY   zProperties.__contains__c                 C   r   )z8Return an immutable proxy for this :class:`.Properties`.)ImmutablePropertiesrQ   r   r   r   r   as_immutable   s   
zProperties.as_immutablec                 C   s   | j | d S r#   )rQ   r   r   r+   r   r   r   r      rA   zProperties.updateNc                 C   s   || v r| | S |S r#   r   )r   r*   defaultr   r   r   get   s   zProperties.getc                 C   r   r#   )r   rQ   r   r   r   r   r      rY   zProperties.keysc                 C      t | j S r#   )r   rQ   r[   r   r   r   r   r[      rH   zProperties.valuesc                 C   rz   r#   )r   rQ   itemsr   r   r   r   r{      rH   zProperties.itemsc                 C   rs   r#   rP   rk   r   r   r   has_key   rY   zProperties.has_keyc                 C      | j   d S r#   )rQ   rL   r   r   r   r   rL      rH   zProperties.clearr#   )r   r   r   r1   r   rE   rX   r\   rb   rf   rC   rl   rB   r,   rm   ro   rr   rt   rv   r   ry   r   r[   r{   r|   rL   __classcell__r   r   ra   r   rO      s0    
rO   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#   )rO   rE   OrderedDictr   r   r   r   rE      rV   zOrderedProperties.__init__N)r   r   r   r1   r   rE   r   r   r   r   r      s    r   c                   @   s   e Zd ZdZdZdS )ru   zDProvide immutable dict/object attribute to an underlying dictionary.r   N)r   r   r   r1   r   r   r   r   r   ru     s    ru   c                   @   s   e Zd ZdZdZdd Zd*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erHdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) ZdS )+r   zCA dict that returns keys/values/items in the order they were added._listc                 C   s   t |  ffS r#   )r   r{   r   r   r   r   r5     rH   zOrderedDict.__reduce__Nc                 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   rE     s   zOrderedDict.__init__c                 C   s   g | _ t|  d S r#   )r   r6   rL   r   r   r   r   rL        zOrderedDict.clearc                 C   s   |   S r#   )__copy__r   r   r   r   copy     zOrderedDict.copyc                 C      t | S r#   )r   r   r   r   r   r     r   zOrderedDict.__copy__c                 O   s   | j j|i | d S r#   )r   sortr=   r   r   r   r   !  rp   zOrderedDict.sortc                 K   s^   |d ur$t |dr| D ]
}| |||  qn|D ]\}}|| |< q|r-| | d S d S )Nr   )hasattrr   rC   r   )r   r   r   r*   r+   r   r   r   r   $  s   

zOrderedDict.updatec                 C   s"   || vr|  || |S | |S r#   )rC   rl   r)   r   r   r   rN   /  s   
zOrderedDict.setdefaultc                 C   r   r#   rZ   r   r   r   r   r   r\   6  rY   zOrderedDict.__iter__c                 C   r   r#   rc   r   r   r   r   r   9  r   zOrderedDict.keysc                        fdd j D S )Nc                    s   g | ]} | qS r   r   r.   r   r   r   r&   =      z&OrderedDict.values.<locals>.<listcomp>r   r   r   r   r   r[   <     zOrderedDict.valuesc                    r   )Nc                    s   g | ]}| | fqS r   r   r.   r   r   r   r&   @  r'   z%OrderedDict.items.<locals>.<listcomp>r   r   r   r   r   r{   ?  r   zOrderedDict.itemsc                 C      t |  S r#   )rZ   r[   r   r   r   r   
itervaluesD  r-   zOrderedDict.itervaluesc                 C   r   r#   )rZ   r   r   r   r   iterkeysG  r   zOrderedDict.iterkeysc                 C   r   r#   )rZ   r{   r   r   r   r   	iteritemsJ  r-   zOrderedDict.iteritemsc                 C   sF   || vrz| j | W n ty   |g| _ Y nw t| || d S r#   )r   appendr(   r6   rC   rh   r   r   r   rC   M  s   zOrderedDict.__setitem__c                 C   s   t | | | j| d S r#   )r6   rB   r   removerk   r   r   r   rB   W     zOrderedDict.__delitem__c                 G   s0   || v }t j| |g|R  }|r| j| |S r#   )r6   r7   r   r   )r   r*   rx   Zpresentr+   r   r   r   r7   [  s
   zOrderedDict.popc                 C   s   t | }| j|d  |S Nr   )r6   rM   r   r   r   itemr   r   r   rM   b  s   
zOrderedDict.popitemr#   )r   r   r   r1   r   r5   rE   rL   r   r   r   r   rN   r\   r   r[   r{   r   r   r   r   rC   rB   r7   rM   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   s>   t |  g | _|d urt|| _t | | j d S g | _d S r#   )setrE   r   unique_listr   r8   r   r   r   rE   i  s   


zOrderedSet.__init__c                 C   s$   || vr
| j | t| | d S r#   r   r   r   addr   elementr   r   r   r   r  s   zOrderedSet.addc                 C   s   t | | | j| d S r#   )r   r   r   r   r   r   r   r   w  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   rL   r   r   r   r   r   rL     s   

zOrderedSet.clearc                 C   rj   r#   r   rk   r   r   r   rl     rY   zOrderedSet.__getitem__c                 C   r   r#   r   r   r   r   r   r\     rY   zOrderedSet.__iter__c                 C   s
   |  |S r#   )rJ   rd   r   r   r   rf     rY   zOrderedSet.__add__c                 C   s   d| j j| jf S Nz%s(%r))r<   r   r   r   r   r   r   rK     rV   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   re   resultr   r   r   rJ     s   

zOrderedSet.unionc                        t   |  fdd| D S )Nc                 3   s    | ]	}| v r|V  qd S r#   r   r$   are   r   r   	<genexpr>      z*OrderedSet.intersection.<locals>.<genexpr>r   r<   rd   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   )re   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   rd   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   rd   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   rd   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   rd   r   r   r   r     s   zOrderedSet.difference_updater#   )r   r   r   rE   r   r   r   r   rL   rl   r\   rf   rK   __str__r   __ior__rJ   __or__r   __and__r   __xor__r   __sub__r   __iand__r   __ixor__r   __isub__r   r   r   r   r   h  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#   )r6   _membersr   r   r   r   r   r   rE     s   zIdentitySet.__init__c                 C   s   || j t|< d S r#   r   idrw   r   r   r   r     rV   zIdentitySet.addc                 C   s   t || jv S r#   )r   r   rw   r   r   r   rt     rH   zIdentitySet.__contains__c                 C   s   | j t|= d S r#   r   rw   r   r   r   r     rA   zIdentitySet.removec                 C   s&   z|  | W d S  ty   Y d S w r#   )r   rq   rw   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   rM   rq   )r   pairr   r   r   r7     s   

zIdentitySet.popc                 C   r}   r#   )r   rL   r   r   r   r   rL     rH   zIdentitySet.clearc                 C      t d)Nzcannot compare sets using cmp()r;   rd   r   r   r   __cmp__  r   zIdentitySet.__cmp__c                 C   s   t |tr| j|jkS dS )NFrI   r   r   rd   r   r   r   __eq__     
zIdentitySet.__eq__c                 C   s   t |tr| j|jkS dS NTr   rd   r   r   r   __ne__  r   zIdentitySet.__ne__c                 C   sD   |  |}t| t|krdS t|jjt| j D ]} dS dS NFTr<   rW   r   r   rt   rZ   r   r   r   re   mr   r   r   issubset  s   
zIdentitySet.issubsetc                 C      t |tstS | |S r#   )rI   r   NotImplementedr   rd   r   r   r   __le__     

zIdentitySet.__le__c                 C   s(   t |tstS t| t|k o| |S r#   )rI   r   r   rW   r   rd   r   r   r   __lt__     
zIdentitySet.__lt__c                 C   sD   |  |}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#   )rI   r   r   r   rd   r   r   r   __ge__)  r   zIdentitySet.__ge__c                 C   s(   t |tstS t| t|ko| |S r#   )rI   r   r   rW   r   rd   r   r   r   __gt__.  r   zIdentitySet.__gt__c                 C   s4   |   }| j}|j| |jdd |D  |S )Nc                 s       | ]	}t ||fV  qd S r#   r   r$   ri   r   r   r   r   7  r   z$IdentitySet.union.<locals>.<genexpr>)r<   r   r   r   r   r   membersr   r   r   rJ   3  s
   zIdentitySet.unionc                 C   r   r#   )rI   r   r   rJ   rd   r   r   r   r   :  r   zIdentitySet.__or__c                 C   s   | j dd |D  d S )Nc                 s   r   r#   r   r   r   r   r   r   @  r   z%IdentitySet.update.<locals>.<genexpr>)r   r   r   r   r   r   r   ?     zIdentitySet.updatec                 C      t |tstS | | | S r#   )rI   r   r   r   rd   r   r   r   r   B     

zIdentitySet.__ior__c                    >   |   }| j}dd |D  |j fdd| D  |S )Nc                 S      h | ]}t |qS r   r   r   r   r   r   	<setcomp>K  r   z)IdentitySet.difference.<locals>.<setcomp>c                 3   $    | ]\}}| vr||fV  qd S r#   r   r$   r^   vr   r   r   r   M     " z)IdentitySet.difference.<locals>.<genexpr>r<   r   r   r{   r   r   r   r   r   H  s   zIdentitySet.differencec                 C   r   r#   )rI   r   r   r   rd   r   r   r   r   Q  r   zIdentitySet.__sub__c                 C      |  |j| _d S r#   )r   r   r   r   r   r   r   V  rV   zIdentitySet.difference_updatec                 C   r   r#   )rI   r   r   r   rd   r   r   r   r   Y  r   zIdentitySet.__isub__c                    r   )Nc                 S   r   r   r   r   r   r   r   r   b  r   z+IdentitySet.intersection.<locals>.<setcomp>c                 3   s$    | ]\}}| v r||fV  qd S r#   r   r   r   r   r   r   c  s    z+IdentitySet.intersection.<locals>.<genexpr>r   r   r   r   r   r   _  s   zIdentitySet.intersectionc                 C   r   r#   )rI   r   r   r   rd   r   r   r   r   h  r   zIdentitySet.__and__c                 C   r   r#   )r   r   r   r   r   r   r   m  rV   zIdentitySet.intersection_updatec                 C   r   r#   )rI   r   r   r   rd   r   r   r   r   p  r   zIdentitySet.__iand__c                    s\   |   }| j 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   r/   y      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   re   r   r   v  s   z IdentitySet.symmetric_differencec                 C   r   r#   rI   r   r   r   rd   r   r   r   r     r   zIdentitySet.__xor__c                 C   r   r#   )r   r   r   r   r   r   r     rV   z'IdentitySet.symmetric_difference_updatec                 C   r   r#   r   rd   r   r   r   r     r   zIdentitySet.__ixor__c                 C   s   t | t| j S r#   )typerZ   r   r[   r   r   r   r   r     rp   zIdentitySet.copyc                 C   r   r#   )rW   r   r   r   r   r   rX     rY   zIdentitySet.__len__c                 C   rz   r#   )rZ   r   r[   r   r   r   r   r\     rH   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   r   r   r[   r   r   r   r   rK     s   zIdentitySet.__repr__r#   )*r   r   r   r1   rE   r   rt   r   r   r7   rL   r   r   r   r   r   r   r   r   r   rJ   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rX   r\   r   rK   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)r$   r   r   r   r   r&     s    z)WeakSequence.__init__.<locals>.<listcomp>)r   r   r   r   )r   Z_WeakSequence__elementsr   r   r   rE     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   r   r#   )rW   r   r   r   r   r   rX     rY   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   )r$   r   r   r   r   r     s    )r   r   r   r   r   r\     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   indexri   r   r   r   rl     s   zWeakSequence.__getitem__N)r   )r   r   r   rE   r   rX   r\   rl   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   rE   r   r   r   )r   r   or   r   r   rE     s   
zOrderedIdentitySet.__init__r#   )r   r   r   rE   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   rE     rY   zPopulateDict.__init__c                 C   s   |  | | |< }|S r#   r   r   r*   valr   r   r   __missing__  s   zPopulateDict.__missing__Nr   r   r   r1   rE   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   rE     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#   )r2   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   r$   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   )rU   _uniquegetattr_data_appenderr   r   r   )r   rU   Zviar   r   r   rE     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   r   r#   )rZ   rU   r   r   r   r   r\   !  rY   zUniqueAppender.__iter__r#   )r   r   r   r1   rE   r   r\   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   )rW   rI   typesGeneratorTyper   )r>   r   r   r   coerce_generator_arg%  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#   )rI   r   Iterabler   r   r   )r  rx   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   r$   ir   r   r   r&   A  r   z$has_intersection.<locals>.<listcomp>)boolr   )Zset_r   r   r   r   has_intersection9  s   r!  c                 C   (   | d u rt  S t| t st t| S | S r#   )r   rI   r  r  r   r   r   to_setD  
   
r$  c                 C   r"  r#   )
column_setrI   r  r#  r   r   r   to_column_setM  r%  r'  c                 K   s*   |   } |r| | | jdi | | S )z5Copy the given dict and update with the given values.Nr   )r   r   )r9   _newr?   r   r   r   update_copyV  s
   
r)  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.

    r\   N)rI   r]   r   flatten_iterator)r  elemyr   r   r   r*  `  s   r*  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   )r.  r/  r0  r1  r	   Lockr2  )r   r.  r/  r0  r   r   r   rE   y  s
   zLRUCache.__init__c                 C   s   |  j d7  _ | j S Nr   )r1  r   r   r   r   _inc_counter  s   zLRUCache._inc_counterc                 C   s.   t | ||}||ur|  |d< |d S |S N   r   )r6   ry   r7  )r   r*   rx   r   r   r   r   ry     s
   zLRUCache.getc                 C   s    t | |}|  |d< |d S r8  )r6   rl   r7  )r   r*   r   r   r   r   rl     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>)r6   r[   r   r   r   r   r[     r   zLRUCache.valuesc                 C   s   || v r| | S || |< |S r#   r   r)   r   r   r   rN     s   zLRUCache.setdefaultc                 C   sF   t | |}|d u r|||  g}t | || n||d< |   d S r6  )r6   ry   r7  rC   _manage_size)r   r*   r+   r   r   r   r   rC     s   zLRUCache.__setitem__c                 C   s   | j | j | j  S r#   )r.  r/  r   r   r   r   size_threshold  s   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 )NFr9  T)r*   reverser   )r2  acquirer   r0  rW   r.  r/  sortedr6   r[   r
  r  rq   release)r   r0  Z
by_counterr   r   r   r   r:    s*   

zLRUCache._manage_size)r3  r4  Nr#   )r   r   r   r1   r   rE   r7  ry   rl   r[   rN   rC   r2   r;  r:  r   r   r   r   r-  m  s    	

	
r-  r3  c                 C   sl   | ft | }t|}|r|S t| tftdd t|D dg }||_t dd |D |_|t|< |S )Nc                 S   s$   g | ]\}}|d ur|t | fqS r#   )_property_getters)r$   r  fieldr   r   r   r&     s
    
z+lightweight_named_tuple.<locals>.<listcomp>)r   r   c                 S   r"   r#   r   )r$   fr   r   r   r&     r'   )	r   
_lw_tuplesry   r   r3   r6   	enumerater4   r   )namefieldshash_Ztp_clsr   r   r   lightweight_named_tuple  s&   
rH  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   rE     s   
zScopedRegistry.__init__c                 C   s:   |   }z| j| W S  ty   | j||   Y S w r#   )rL  rM  rq   rN   rK  rk   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   ri   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   rL   
  s
   zScopedRegistry.clearN	r   r   r   r1   rE   rN  rO  r   rL   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   rE     r   zThreadLocalRegistry.__init__c                 C   s2   z| j jW S  ty   |   }| j _| Y S w r#   )rM  r+   r(   rK  )r   r  r   r   r   rN    s   
zThreadLocalRegistry.__call__c                 C   s   t | jdS )Nr+   )r   rM  r   r   r   r   rO  $  r-   zThreadLocalRegistry.hasc                 C   s   || j _d S r#   )rM  r+   rP  r   r   r   r   '  r-   zThreadLocalRegistry.setc                 C   s"   z| j `W d S  ty   Y d S w r#   )rM  r+   r(   r   r   r   r   rL   *  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_dupes1  s   
rW  r#   )7r1   
__future__r   r
  r  r   compatr   r   r   r   r   r	   	frozenset	EMPTY_SETr   r
   r   r3   rT   r:   r6   rD   rO   r   ru   r   r   r   r   r   r   r   r  r&  Zcolumn_dictZordered_column_setr  Z_gettersr@  r   r  r  r  r!  r$  r'  r)  r*  r-  rC  rH  rJ  rR  rW  r   r   r   r   <module>   sd   C O
ak P"	


	
	
Q8