
 X)                 @   sV   d  Z  d d l m Z d d l m Z d d l m Z e Gd d   d e   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_ordering)six)rangec                   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 dA d' d(  Z d) d*   Z d+ d,   Z d d d- d. d/  Z d0 d1   Z d2 d3   Z d4 d5 d6  Z d7 d8   Z d9 d:   Z d; d<   Z  d= d>   Z! d? d@   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                se   t  |  d  s |  j |  _ n  t  |  d  sH |  j |  _ |  j |  _ n  t t |   j	 | |   d  S)N_get_single_internal_set_single)
hasattr_get_single_externalr   _set_single_rebuildr   _assign_extended_slice_rebuild_assign_extended_slicesuperr   __init__)selfargskwargs)	__class__ R/home/ubuntu/projects/ifolica/build/django/django/contrib/gis/geos/mutable_list.pyr   A   s    zListMixin.__init__c                s[   t  | t  r;   f d d   t | j t       D S  j |  }   j |  Sd S)z-Get the item(s) at the specified index/slice.c                s   g  |  ] }   j  |   q Sr   )r	   ).0i)r   r   r   
<listcomp>N   s   	 z)ListMixin.__getitem__.<locals>.<listcomp>N)
isinstanceslicer   indiceslen_checkindexr	   )r   indexr   )r   r   __getitem__K   s    ,zListMixin.__getitem__c                s   t  | t j t f  s, t d |   n  t   } t  | t j  re  j |  } | g   n t | j |      | t    }    f d d   t |  D }  j	 | |  d S)z0Delete the item(s) at the specified index/slice.z%s is not a legal indexc             3   s*   |  ]  } |   k r  j  |  Vq d  S)N)r   )r   r   )
indexRanger   r   r   	<genexpr>a   s   z(ListMixin.__delitem__.<locals>.<genexpr>N)
r   r   integer_typesr   	TypeErrorr   r   r   r   _rebuild)r   r   origLennewLennewItemsr   )r   r   r   __delitem__S   s    zListMixin.__delitem__c             C   sU   t  | t  r" |  j | |  n/ |  j |  } |  j | f  |  j | |  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__g   s
    zListMixin.__setitem__c             C   s   |  j  t |   t |   S)zadd another list-like object)r   list)r   otherr   r   r   __add__q   s    zListMixin.__add__c             C   s   | j  t |  t |    S)zadd to another list-like object)r   r,   )r   r-   r   r   r   __radd__u   s    zListMixin.__radd__c             C   s   |  j  t |   |  S)z$add another list-like object to self)extendr,   )r   r-   r   r   r   __iadd__y   s    zListMixin.__iadd__c             C   s   |  j  t |   |  S)multiply)r   r,   )r   nr   r   r   __mul__~   s    zListMixin.__mul__c             C   s   |  j  t |   |  S)r2   )r   r,   )r   r3   r   r   r   __rmul__   s    zListMixin.__rmul__c             C   sT   | d k r |  d d  =n4 t  |   } x% t | d  D] } |  j |  q9 W|  S)r2   r   N   )r,   r   r0   )r   r3   cacher   r   r   r   __imul__   s    zListMixin.__imul__c             C   sn   t  |  } xO t |  D]A } y |  | | | k } Wn t k
 rO d SYn X| s d Sq Wt  |   | k S)NF)r   r   
IndexError)r   r-   olenr   cr   r   r   __eq__   s    	zListMixin.__eq__c             C   s   t  |  } xg t |  D]Y } y |  | | | k  } Wn t k
 rO d SYn X| rZ | S| | |  | k  r d Sq Wt  |   | k  S)NTF)r   r   r9   )r   r-   r:   r   r;   r   r   r   __lt__   s    	zListMixin.__lt__c             C   s4   d } x' |  D] } | | k r | d 7} q q W| S)zStandard list count methodr   r6   r   )r   r*   countr   r   r   r   r>      s
    zListMixin.countc             C   sN   x1 t  d t |    D] } |  | | k r | Sq Wt d t |    d S)zStandard list index methodr   z%s not found in objectN)r   r   
ValueErrorstr)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   r0      s    zListMixin.extendc             C   s<   t  | t j  s% t d |   n  | g |  | |  <d S)zStandard list insert methodz%s is not a legal indexN)r   r   r!   r"   )r   r   r*   r   r   r   insert   s    zListMixin.insertr6   c             C   s   |  | } |  | =| S)zStandard list pop methodr   )r   r   resultr   r   r   pop   s    
zListMixin.popc             C   s   |  |  j  |  =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 methodr6   NrG   r   )r   r   r   r   reverse   s    zListMixin.reverseFc                s     r[   f d d   |  D } | j  d d d   d |  d d   | D |  d d  <nQ t |   } | d k	 r | j  d	 | d |  n | j  d |  | |  d d  <d S)
zStandard list sort methodc                s"   g  |  ] }   |  | f  q Sr   r   )r   v)keyr   r   r      s   	 z"ListMixin.sort.<locals>.<listcomp>rJ   c             S   s   |  d S)Nr   r   )xr   r   r   <lambda>   s    z ListMixin.sort.<locals>.<lambda>rH   c             S   s   g  |  ] } | d   q S)r6   r   )r   rI   r   r   r   r      s   	 Ncmp)sortr,   )r   rM   rJ   rH   tempr   )rJ   r   rN      s     zListMixin.sortc             C   ss   | r+ | |  j  k  r+ t d |  j    n  |  j d  k	 r_ | |  j k r_ t d |  j   n  |  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'   |  j  t | | d d  | g  d  S)Nr6   )r(   r   )r   r   valuer   r   r   r
      s    zListMixin._set_single_rebuildTc             C   sq   t  |   } d | k o# | k  n r, | S| rW | | k oJ d k  n rW | | St d t |    d  S)Nr   zinvalid index: %s)r   r9   r@   )r   r   correctlengthr   r   r   r      s    #zListMixin._checkindexc                sD   t    d  r@ d   f d d   | D k r@ t d   q@ n  d  S)N_allowedFc                s"   g  |  ] } t  |   j   q Sr   )r   rU   )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   s   y t  |  Wn t k
 r. t d   Yn X|  j |  t |   } t |  } | j |  \ } } } | j d k r |  j | | |  n |  j | | | |  d S)z&Assign values to a slice of the objectz&can only assign an iterable to a sliceN)	iterr"   r)   r   r,   r   step_assign_simple_slicer   )r   r   valuesr$   	valueListstartstoprX   r   r   r   r(      s    zListMixin._set_slicec                s   t  | | |  } t |  t |  k rO t d t |  t |  f   n  t     t t | |        f d d   }  j   |    d S)z2Assign an extended slice by rebuilding entire listzBattempt to assign sequence of size %d to extended slice of size %dc              3   sA   x: t     D], }  |   k r+  |  Vq  j |   Vq Wd  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   r\   r]   rX   r[   	indexListr&   r   )r%   r^   r   r   r     s    z(ListMixin._assign_extended_slice_rebuildc             C   s   t  | | |  } t |  t |  k rO t d t |  t |  f   n  x- t | |  D] \ } } |  j | |  q_ Wd 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   r\   r]   rX   r[   ra   r   r*   r   r   r   r   )  s    z ListMixin._assign_extended_slicec                sh   t      t         t    }       f d d   }  j | |    d S)z5Assign a simple slice; Can assign slice of any lengthc              3   s|   xu t    d  D]c }  |   k r< x  D] } | Vq* Wn  |    k  r |   k  s` |   k rt  j |   Vqt q q Wd  S)Nr6   )r   r   )r   r*   )r$   r   r\   r]   r[   r   r   r&   ;  s    z0ListMixin._assign_simple_slice.<locals>.newItemsN)r   maxr#   )r   r\   r]   r[   r%   r&   r   )r$   r   r\   r]   r[   r   rY   5  s
    
zListMixin._assign_simple_slicerG   )#__name__
__module____qualname____doc__rP   rQ   r   r   r'   r+   r.   r/   r1   r4   r5   r8   r<   r=   r>   r   rA   r0   rC   rE   rF   rH   rN   r#   r
   r   r)   r(   r   r   rY   r   r   )r   r   r      s@   )


r   N)	rf   	functoolsr   Zdjango.utilsr   Zdjango.utils.six.movesr   objectr   r   r   r   r   <module>
   s
   