B
    ¥0H[ù(  ã               @   s&   d Z ddlmZ eG dd„ dƒƒZdS )zÉ
This module contains a base type which provides list-style mutations
without specific data storage methods.

See also http://static.aryehleib.com/oldsite/MutableLists.html

Author: Aryeh Leib Taurog.
é    )Útotal_orderingc                   s  e Zd ZdZdZ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„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd@d'd(„Zd)d*„ Zd+d,„ ZdAd.d/„Zd0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Zd:d;„ Z d<d=„ Z!d>d?„ Z"‡  Z#S )BÚ	ListMixinay  
    A base class which provides complete list interface.
    Derived classes must call ListMixin's __init__() function
    and implement the following:

    function _get_single_external(self, i):
        Return single item with index i for general use.
        The index i will always satisfy 0 <= i < len(self).

    function _get_single_internal(self, i):
        Same as above, but for use within the class [Optional]
        Note that if _get_single_internal and _get_single_internal return
        different types of objects, _set_list must distinguish
        between the two and handle each appropriately.

    function _set_list(self, length, items):
        Recreate the entire object.

        NOTE: items may be a generator which calls _get_single_internal.
        Therefore, it is necessary to cache the values in a temporary:
            temp = list(items)
        before clobbering the original storage.

    function _set_single(self, i, value):
        Set the single item at index i to value [Optional]
        If left undefined, all mutations will result in rebuilding
        the object using _set_list.

    function __len__(self):
        Return the length

    int _minlength:
        The minimum legal length [Optional]

    int _maxlength:
        The maximum legal length [Optional]

    type or tuple _allowed:
        A type or tuple of allowed item types [Optional]
    r   Nc                s>   t | dƒs| j| _t | dƒs,| j| _| j| _tƒ j||Ž d S )NÚ_get_single_internalÚ_set_single)	ÚhasattrÚ_get_single_externalr   Ú_set_single_rebuildr   Ú_assign_extended_slice_rebuildÚ_assign_extended_sliceÚsuperÚ__init__)ÚselfÚargsÚkwargs)Ú	__class__© úhC:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\django\django\contrib\gis\geos\mutable_list.pyr   >   s    

zListMixin.__init__c                sB   t |tƒr*‡ fdd„t| tˆ ƒ¡Ž D ƒS ˆ  |¡}ˆ  |¡S dS )z-Get the item(s) at the specified index/slice.c                s   g | ]}ˆ   |¡‘qS r   )r   )Ú.0Úi)r   r   r   ú
<listcomp>K   s    z)ListMixin.__getitem__.<locals>.<listcomp>N)Ú
isinstanceÚsliceÚrangeÚindicesÚlenÚ_checkindexr   )r   Úindexr   )r   r   Ú__getitem__H   s    
 
zListMixin.__getitem__c                s€   t |ttfƒstd| ƒ‚tˆƒ}t |tƒr>ˆ |¡}|g‰ nt| |¡Ž ‰ |tˆ ƒ }‡ ‡fdd„t|ƒD ƒ}ˆ ||¡ dS )z0Delete the item(s) at the specified index/slice.z%s is not a legal indexc             3   s    | ]}|ˆ krˆ  |¡V  qd S )N)r   )r   r   )Ú
indexRanger   r   r   ú	<genexpr>^   s   z(ListMixin.__delitem__.<locals>.<genexpr>N)	r   Úintr   Ú	TypeErrorr   r   r   r   Ú_rebuild)r   r   ÚorigLenÚnewLenÚnewItemsr   )r   r   r   Ú__delitem__P   s    

zListMixin.__delitem__c             C   s>   t |tƒr|  ||¡ n"|  |¡}|  |f¡ |  ||¡ dS )z-Set the item(s) at the specified index/slice.N)r   r   Ú
_set_slicer   Ú_check_allowedr   )r   r   Úvalr   r   r   Ú__setitem__d   s
    

zListMixin.__setitem__c             C   s   |   t| ƒt|ƒ ¡S )zadd another list-like object)r   Úlist)r   Úotherr   r   r   Ú__add__n   s    zListMixin.__add__c             C   s   |  t|ƒt| ƒ ¡S )zadd to another list-like object)r   r+   )r   r,   r   r   r   Ú__radd__r   s    zListMixin.__radd__c             C   s   |   t|ƒ¡ | S )z$add another list-like object to self)Úextendr+   )r   r,   r   r   r   Ú__iadd__v   s    zListMixin.__iadd__c             C   s   |   t| ƒ| ¡S )Úmultiply)r   r+   )r   Únr   r   r   Ú__mul__{   s    zListMixin.__mul__c             C   s   |   t| ƒ| ¡S )r1   )r   r+   )r   r2   r   r   r   Ú__rmul__   s    zListMixin.__rmul__c             C   s@   |dkr| dd…= n(t | ƒ}xt|d ƒD ]}|  |¡ q*W | S )r1   r   Né   )r+   r   r/   )r   r2   Úcacher   r   r   r   Ú__imul__ƒ   s    zListMixin.__imul__c          	   C   sX   t |ƒ}xBt|ƒD ]6}y| | || k}W n tk
r>   dS X |sdS qW t | ƒ|kS )NF)r   r   Ú
IndexError)r   r,   Úolenr   Úcr   r   r   Ú__eq__�   s    zListMixin.__eq__c          	   C   sl   t |ƒ}xVt|ƒD ]J}y| | || k }W n tk
r>   dS X |rH|S || | | k rdS qW t | ƒ|k S )NTF)r   r   r8   )r   r,   r9   r   r:   r   r   r   Ú__lt__™   s    zListMixin.__lt__c             C   s&   d}x| D ]}||kr
|d7 }q
W |S )zStandard list count methodr   r5   r   )r   r)   Úcountr   r   r   r   r=   ©   s
    
zListMixin.countc             C   s8   x&t dt| ƒƒD ]}| | |kr|S qW td| ƒ‚dS )zStandard list index methodr   z%s not found in objectN)r   r   Ú
ValueError)r   r)   r   r   r   r   r   ±   s    zListMixin.indexc             C   s   |g| t | ƒd…< dS )zStandard list append methodN)r   )r   r)   r   r   r   Úappend¹   s    zListMixin.appendc             C   s   || t | ƒd…< dS )zStandard list extend methodN)r   )r   Úvalsr   r   r   r/   ½   s    zListMixin.extendc             C   s(   t |tƒstd| ƒ‚|g| ||…< dS )zStandard list insert methodz%s is not a legal indexN)r   r    r!   )r   r   r)   r   r   r   ÚinsertÁ   s    
zListMixin.insertéÿÿÿÿc             C   s   | | }| |= |S )zStandard list pop methodr   )r   r   Úresultr   r   r   ÚpopÇ   s    zListMixin.popc             C   s   | |   |¡= dS )zStandard list remove methodN)r   )r   r)   r   r   r   ÚremoveÍ   s    zListMixin.removec             C   s   | ddd… | dd…< dS )zStandard list reverse methodrB   Nr   )r   r   r   r   ÚreverseÑ   s    zListMixin.reverseFc                s|   ˆ r@‡ fdd„| D ƒ}|j dd„ |d� dd„ |D ƒ| dd…< n8t| ƒ}|dk	r`|j ||d� n|j |d	� || dd…< dS )
zStandard list sort methodc                s   g | ]}ˆ |ƒ|f‘qS r   r   )r   Úv)Úkeyr   r   r   Ø   s    z"ListMixin.sort.<locals>.<listcomp>c             S   s   | d S )Nr   r   )Úxr   r   r   Ú<lambda>Ù   s    z ListMixin.sort.<locals>.<lambda>)rH   rF   c             S   s   g | ]}|d  ‘qS )r5   r   )r   rG   r   r   r   r   Ú   s    N)ÚcmprF   )rF   )Úsortr+   )r   rK   rH   rF   Útempr   )rH   r   rL   Õ   s    zListMixin.sortc             C   sN   |r|| j k rtd| j  ƒ‚| jd k	r>|| jkr>td| j ƒ‚|  ||¡ d S )NzMust have at least %d itemszCannot have more than %d items)Ú
_minlengthr>   Ú
_maxlengthZ	_set_list)r   r$   r%   r   r   r   r"   ä   s
    zListMixin._rebuildc             C   s   |   t||d dƒ|g¡ d S )Nr5   )r'   r   )r   r   Úvaluer   r   r   r   ì   s    zListMixin._set_single_rebuildc             C   sV   t | ƒ}d|  kr|k r$n n|S | |  kr:dk rFn n|| S td| ƒ‚d S )Nr   zinvalid index: %s)r   r8   )r   r   Úlengthr   r   r   r   ï   s    zListMixin._checkindexc                s,   t ˆ dƒr(d‡ fdd„|D ƒkr(tdƒ‚d S )NÚ_allowedFc                s   g | ]}t |ˆ jƒ‘qS r   )r   rR   )r   r)   )r   r   r   r   ù   s    z,ListMixin._check_allowed.<locals>.<listcomp>z*Invalid type encountered in the arguments.)r   r!   )r   Úitemsr   )r   r   r(   ÷   s    
zListMixin._check_allowedc             C   sz   yt |ƒ}W n tk
r(   tdƒ‚Y nX |  |¡ t| ƒ}| |¡\}}}|jdkrf|  |||¡ n|  ||||¡ dS )z&Assign values to a slice of the objectz&can only assign an iterable to a sliceN)r+   r!   r(   r   r   ÚstepÚ_assign_simple_slicer
   )r   r   ÚvaluesÚ	valueListr#   ÚstartÚstoprT   r   r   r   r'   ü   s    

zListMixin._set_slicec                sl   t |||ƒ}t|ƒt|ƒkr4tdt|ƒt|ƒf ƒ‚tˆƒ‰ tt||ƒƒ‰‡ ‡‡fdd„}ˆ ˆ |ƒ ¡ dS )z2Assign an extended slice by rebuilding entire listzBattempt to assign sequence of size %d to extended slice of size %dc              3   s6   x0t ˆ ƒD ]$} | ˆkr"ˆ|  V  q
ˆ | ¡V  q
W d S )N)r   r   )r   )r$   ÚnewValsr   r   r   r%     s    z:ListMixin._assign_extended_slice_rebuild.<locals>.newItemsN)r   r   r>   ÚdictÚzipr"   )r   rX   rY   rT   rW   Ú	indexListr%   r   )r$   rZ   r   r   r	     s    z(ListMixin._assign_extended_slice_rebuildc             C   s\   t |||ƒ}t|ƒt|ƒkr4tdt|ƒt|ƒf ƒ‚x"t||ƒD ]\}}|  ||¡ q@W dS )z9Assign an extended slice by re-assigning individual itemszBattempt to assign sequence of size %d to extended slice of size %dN)r   r   r>   r\   r   )r   rX   rY   rT   rW   r]   r   r)   r   r   r   r
   %  s    z ListMixin._assign_extended_slicec                sL   t ˆƒ‰ tˆˆƒ‰ˆ ˆ ˆ t ˆƒ }‡ ‡‡‡‡fdd„}ˆ ||ƒ ¡ dS )z5Assign a simple slice; Can assign slice of any lengthc              3   sP   xJt ˆ d ƒD ]:} | ˆkr$ˆE d H  | ˆ k r| ˆk s<| ˆkrˆ | ¡V  qW d S )Nr5   )r   r   )r   )r#   r   rX   rY   rW   r   r   r%   7  s    
z0ListMixin._assign_simple_slice.<locals>.newItemsN)r   Úmaxr"   )r   rX   rY   rW   r$   r%   r   )r#   r   rX   rY   rW   r   rU   1  s
    
	zListMixin._assign_simple_slice)rB   )NNF)$Ú__name__Ú
__module__Ú__qualname__Ú__doc__rN   rO   r   r   r&   r*   r-   r.   r0   r3   r4   r7   r;   r<   r=   r   r?   r/   rA   rD   rE   rF   rL   r"   r   r   r(   r'   r	   r
   rU   Ú__classcell__r   r   )r   r   r      s@   )




r   N)rb   Ú	functoolsr   r   r   r   r   r   Ú<module>
   s   