ó
D()Qc           @   sV   d  Z  d d l m Z d d l m Z d d l m Z e d e f d „  ƒ  Yƒ Z d S(   s¾   
This module contains a base type which provides list-style mutations
without specific data storage methods.

See also http://www.aryehleib.com/MutableLists.html

Author: Aryeh Leib Taurog.
iÿÿÿÿ(   t   total_ordering(   t   six(   t   xranget	   ListMixinc           B   s=  e  Z d  Z d Z d! Z e Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d „  Z d „  Z d! d! e d „ Z d „  Z d „  Z  e! d „ Z" d „  Z# d „  Z$ d „  Z% d „  Z& d  „  Z' RS("   s×  
    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]

    class _IndexError:
        The type of exception to be raise on invalid index [Optional]
    i    c         O   se   t  |  d ƒ s |  j |  _ n  t  |  d ƒ sH |  j |  _ |  j |  _ n  t t |  ƒ j	 | | Ž  d  S(   Nt   _get_single_internalt   _set_single(
   t   hasattrt   _get_single_externalR   t   _set_single_rebuildR   t   _assign_extended_slice_rebuildt   _assign_extended_slicet   superR   t   __init__(   t   selft   argst   kwargs(    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyR   C   s    c         C   sd   t  | t ƒ rD g  t | j t |  ƒ ƒ Œ  D] } |  j | ƒ ^ q+ S|  j | ƒ } |  j | ƒ Sd S(   s-   Get the item(s) at the specified index/slice.N(   t
   isinstancet   sliceR   t   indicest   lenR   t   _checkindex(   R   t   indext   i(    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyt   __getitem__M   s    5c            s½   t  | t j t f ƒ s, t d | ƒ ‚ n  t ˆ ƒ } t  | t j ƒ re ˆ j | ƒ } | g ‰  n t | j | ƒ Œ  ‰  | t ˆ  ƒ } ‡ ‡  f d †  t	 | ƒ Dƒ } ˆ j
 | | ƒ d S(   s0   Delete the item(s) at the specified index/slice.s   %s is not a legal indexc         3   s*   |  ]  } | ˆ k r ˆ  j  | ƒ Vq d  S(   N(   R   (   t   .0R   (   R   t
   indexRange(    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pys	   <genexpr>c   s   N(   R   R   t   integer_typesR   t	   TypeErrorR   R   t   rangeR   R   t   _rebuild(   R   R   t   origLent   newLent   newItems(    (   R   R   s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyt   __delitem__U   s    c         C   sU   t  | t ƒ r" |  j | | ƒ n/ |  j | ƒ } |  j | f ƒ |  j | | ƒ d S(   s-   Set the item(s) at the specified index/slice.N(   R   R   t
   _set_sliceR   t   _check_allowedR   (   R   R   t   val(    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyt   __setitem__i   s
    c         c   s*   x# t  t |  ƒ ƒ D] } |  | Vq Wd S(   s"   Iterate over the items in the listN(   R   R   (   R   R   (    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyt   __iter__r   s    c         C   s   |  j  t |  ƒ t | ƒ ƒ S(   s   add another list-like object(   t	   __class__t   list(   R   t   other(    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyt   __add__x   s    c         C   s   | j  t | ƒ t |  ƒ ƒ S(   s   add to another list-like object(   R'   R(   (   R   R)   (    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyt   __radd__|   s    c         C   s   |  j  t | ƒ ƒ |  S(   s$   add another list-like object to self(   t   extendR(   (   R   R)   (    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyt   __iadd__€   s    c         C   s   |  j  t |  ƒ | ƒ S(   t   multiply(   R'   R(   (   R   t   n(    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyt   __mul__…   s    c         C   s   |  j  t |  ƒ | ƒ S(   R.   (   R'   R(   (   R   R/   (    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyt   __rmul__‰   s    c         C   sK   | d k r |  2n4 t  |  ƒ } x% t | d ƒ D] } |  j | ƒ q0 W|  S(   R.   i    i   (   R(   R   R,   (   R   R/   t   cacheR   (    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyt   __imul__   s    c         C   sm   t  | ƒ } xN t | ƒ D]@ } y |  | | | k } Wn |  j k
 rN t SX| s t Sq Wt  |  ƒ | k S(   N(   R   R   t   _IndexErrort   False(   R   R)   t   olenR   t   c(    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyt   __eq__—   s    c         C   s…   t  | ƒ } xf t | ƒ D]X } y |  | | | k  } Wn |  j k
 rN t SX| rY | S| | |  | k  r t Sq Wt  |  ƒ | k  S(   N(   R   R   R4   t   TrueR5   (   R   R)   R6   R   R7   (    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyt   __lt__£   s    c         C   s4   d } x' |  D] } | | k r | d 7} q q W| S(   s   Standard list count methodi    i   (    (   R   R$   t   countR   (    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyR;   ³   s
     c         C   sN   x1 t  d t |  ƒ ƒ D] } |  | | k r | Sq Wt d t | ƒ ƒ ‚ d S(   s   Standard list index methodi    s   %s not found in objectN(   R   R   t
   ValueErrort   str(   R   R$   R   (    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyR   º   s     c         C   s   | g |  t  |  ƒ )d S(   s   Standard list append methodN(   R   (   R   R$   (    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyt   appendÁ   s    c         C   s   | |  t  |  ƒ )d S(   s   Standard list extend methodN(   R   (   R   t   vals(    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyR,   Å   s    c         C   s9   t  | t j ƒ s% t d | ƒ ‚ n  | g |  | | +d S(   s   Standard list insert methods   %s is not a legal indexN(   R   R   R   R   (   R   R   R$   (    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyt   insertÉ   s    iÿÿÿÿc         C   s   |  | } |  | =| S(   s   Standard list pop method(    (   R   R   t   result(    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyt   popÏ   s    
c         C   s   |  |  j  | ƒ =d S(   s   Standard list remove methodN(   R   (   R   R$   (    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyt   removeÕ   s    c         C   s   |  d d d … |  (d S(   s   Standard list reverse methodiÿÿÿÿN(    (   R   (    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyt   reverseÙ   s    c         C   s±   | re g  |  D] } | | ƒ | f ^ q } | j  d d „  d | ƒ g  | D] } | d ^ qK |  (nH t |  ƒ } | d k	 r– | j  d | d | ƒ n | j  d | ƒ | |  (d S(   s   Standard list sort methodt   keyc         S   s   |  d S(   Ni    (    (   t   x(    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyt   <lambda>á   s    RD   i   t   cmpN(   t   sortR(   t   None(   R   RH   RE   RD   t   vt   temp(    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyRI   Ý   s    %!c         C   sm   | |  j  k  r% t d |  j  ƒ ‚ n  |  j d  k	 rY | |  j k rY t d |  j ƒ ‚ n  |  j | | ƒ d  S(   Ns   Must have at least %d itemss   Cannot have more than %d items(   t
   _minlengthR<   t
   _maxlengthRJ   t	   _set_list(   R   R   R    (    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyR   ì   s
    c         C   s'   |  j  t | | d d ƒ | g ƒ d  S(   Ni   (   R"   R   (   R   R   t   value(    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyR   ô   s    c         C   st   t  |  ƒ } d | k o# | k  n r, | S| rW | | k oJ d k  n rW | | S|  j d t | ƒ ƒ ‚ d  S(   Ni    s   invalid index: %s(   R   R4   R=   (   R   R   t   correctt   length(    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyR   ÷   s    #c         C   sP   t  |  d ƒ rL t g  | D] } t | |  j ƒ ^ q k rL t d ƒ ‚ qL n  d  S(   Nt   _alloweds*   Invalid type encountered in the arguments.(   R   R5   R   RS   R   (   R   t   itemsR$   (    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyR#   ÿ   s    +c         C   sª   y t  | ƒ Wn t k
 r- t d ƒ ‚ n X|  j | ƒ t |  ƒ } t | ƒ } | j | ƒ \ } } } | j d k r |  j | | | ƒ n |  j	 | | | | ƒ d S(   s&   Assign values to a slice of the objects&   can only assign an iterable to a sliceN(
   t   iterR   R#   R   R(   R   t   stepRJ   t   _assign_simple_sliceR
   (   R   R   t   valuesR   t	   valueListt   startt   stopRV   (    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyR"     s    c            sœ   t  | | | ƒ } t | ƒ t | ƒ k rO t d t | ƒ t | ƒ f ƒ ‚ n  t ˆ ƒ ‰  t t | | ƒ ƒ ‰ ‡ ‡ ‡  f d †  } ˆ j ˆ  | ƒ  ƒ d S(   s2   Assign an extended slice by rebuilding entire listsB   attempt 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   t   newValsR   (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyR    $  s    N(   R   R   R<   t   dictt   zipR   (   R   RZ   R[   RV   RY   t	   indexListR    (    (   R   R\   R   s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyR	     s    c         C   sƒ   t  | | | ƒ } t | ƒ t | ƒ k rO t d t | ƒ t | ƒ f ƒ ‚ n  x- t | | ƒ D] \ } } |  j | | ƒ q_ Wd S(   s9   Assign an extended slice by re-assigning individual itemssB   attempt to assign sequence of size %d to extended slice of size %dN(   R   R   R<   R^   R   (   R   RZ   R[   RV   RY   R_   R   R$   (    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyR
   -  s    c            se   t  ˆ  ƒ ‰ t ˆ ˆ ƒ ‰ ˆ ˆ ˆ t  ˆ ƒ } ‡  ‡ ‡ ‡ ‡ f d †  } ˆ  j | | ƒ  ƒ d S(   s5   Assign 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(   Ni   (   R   R   (   R   R$   (   R   R[   RZ   RY   R   (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyR    >  s    N(   R   t   maxR   (   R   RZ   R[   RY   R   R    (    (   R   R[   RZ   RY   R   s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyRW   9  s
    
N((   t   __name__t
   __module__t   __doc__RM   RJ   RN   t
   IndexErrorR4   R   R   R!   R%   R&   R*   R+   R-   R0   R1   R3   R8   R:   R;   R   R>   R,   R@   RB   RC   RD   R5   RI   R   R   R9   R   R#   R"   R	   R
   RW   (    (    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyR      sD   ,	
											
															N(	   Rc   t   django.utils.functionalR    t   django.utilsR   t   django.utils.six.movesR   t   objectR   (    (    (    s=   ../Django//lib/python/django/contrib/gis/geos/mutable_list.pyt   <module>
   s
   