ó
åŽ‹]c           @   sÜ   d  d l  Z  d  d l m Z m Z m Z m Z d  d l m Z m Z m	 Z	 m
 Z
 m Z m Z d  d l m Z d „  Z d e f d „  ƒ  YZ e ƒ  Z d „  Z e  j e ƒ d	 e f d
 „  ƒ  Yƒ Z d e f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   InvariantExceptiont   CheckedTypet   _restore_picklet   store_invariants(   t
   set_fieldst
   check_typet   is_field_ignore_extra_complaintt   PFIELD_NO_INITIALt	   serializet   check_global_invariants(   t	   transformc         C   s    t  |  ƒ d k o |  d t k S(   Ni   i    (   t   lenR   (   t   bases(    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyt
   _is_pclass	   s    t
   PClassMetac           B   s   e  Z d  „  Z RS(   c         C   s†   t  | | d d ƒt | | d d ƒ d	 t d „  | d Dƒ ƒ | d <t | ƒ rg | d c d
 7<n  t t |  ƒ j |  | | | ƒ S(   Nt   namet   _pclass_fieldst   _pclass_invariantst   __invariant__t   _pclass_frozenc         s   s   |  ] } | Vq d  S(   N(    (   t   .0t   key(    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pys	   <genexpr>   s    t	   __slots__t   __weakref__(   R   (   R   (   R   R   t   tupleR   t   superR   t   __new__(   t   mcsR   R   t   dct(    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyR      s    "(   t   __name__t
   __module__R   (    (    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyR      s   c         C   sR   t  |  | | | ƒ | j | ƒ \ } } | s> | j | ƒ n t | | | ƒ d  S(   N(   R   t	   invariantt   appendt   setattr(   t   clst   fieldR   t   valuet   resultt   invariant_errorst   is_okt
   error_code(    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyt   _check_and_set_attr   s
    t   PClassc           B   s¤   e  Z d  Z d „  Z d „  Z e d e d „ ƒ Z d d „ Z	 d „  Z
 d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s­  
    A PClass is a python class with a fixed set of specified fields. PClasses are declared as python classes inheriting
    from PClass. It is defined the same way that PRecords are and behaves like a PRecord in all aspects except that it
    is not a PMap and hence not a collection but rather a plain Python object.


    More documentation and examples of PClass usage is available at https://github.com/tobgu/pyrsistent
    c         K   së  t  t |  ƒ j |  ƒ } | j d d  ƒ } | j d d  ƒ } g  } g  } x)|  j j ƒ  D]\ } } | | k rù | d  k sˆ | | k rÌ t t | | ƒ r¶ | j | | d | ƒ}	 qÖ | j | | ƒ }	 n
 | | }	 t	 |  | | |	 | | ƒ | | =qX | j
 t k	 rHt | j
 ƒ r#| j
 ƒ  n | j
 }
 t	 |  | | |
 | | ƒ qX | j rX | j d j |  j | ƒ ƒ qX qX W| s€| r¡t t | ƒ t | ƒ d ƒ ‚ n  | rÎt d j d j | ƒ |  j ƒ ƒ ‚ n  t | |  j ƒ t | _ | S(   Nt   _factory_fieldst   ignore_extras   {0}.{1}s   Field invariant faileds0   '{0}' are not among the specified fields for {1}s   , (   R   R*   R   t   popt   NoneR   t   itemsR   t   factoryR)   t   initialR   t   callablet	   mandatoryR    t   formatR   R    R   t   AttributeErrort   joinR	   R   t   TrueR   (   R"   t   kwargsR%   t   factory_fieldsR,   t   missing_fieldsR&   R   R#   R$   R1   (    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyR   0   s8    

$	#!		c         O   sˆ   | r | d | | d <n  t  | ƒ } xK |  j D]@ } | | k r1 t |  | t ƒ } | t k	 rq | | | <qq q1 q1 W|  j d | |  S(   s  
        Set a field in the instance. Returns a new instance with the updated value. The original instance remains
        unmodified. Accepts key-value pairs or single string representing the field name and a value.

        >>> from pyrsistent import PClass, field
        >>> class AClass(PClass):
        ...     x = field()
        ...
        >>> a = AClass(x=1)
        >>> a2 = a.set(x=2)
        >>> a3 = a.set('x', 3)
        >>> a
        AClass(x=1)
        >>> a2
        AClass(x=2)
        >>> a3
        AClass(x=3)
        i   i    R+   (   t   setR   t   getattrt   _MISSING_VALUEt	   __class__(   t   selft   argsR8   R9   R   R$   (    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyR;   T   s    c            sK   t  ˆ  |  ƒ r ˆ  S| r5 ‡  f d †  |  j Dƒ ‰  n  |  d | d | ˆ   S(   s:  
        Factory method. Will create a new PClass of the current type and assign the values
        specified in kwargs.

        :param ignore_extra: A boolean which when set to True will ignore any keys which appear in kwargs that are not
                             in the set of fields on the PClass.
        c            s)   i  |  ] } | ˆ  k r ˆ  | | “ q S(    (    (   R   t   k(   R8   (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pys
   <dictcomp>   s   	 R+   R,   (   t
   isinstanceR   (   R"   R8   R+   R,   (    (   R8   s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyt   createt   s
    	c         C   s_   i  } xR |  j  D]G } t |  | t ƒ } | t k	 r t |  j  | j | | ƒ | | <q q W| S(   s‚   
        Serialize the current PClass using custom serializer functions for fields where
        such have been supplied.
        (   R   R<   R=   R   t
   serializer(   R?   R4   R%   R   R$   (    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyR   …   s    'c         G   s   t  |  | ƒ S(   sT  
        Apply transformations to the currency PClass. For more details on transformations see
        the documentation for PMap. Transformations on PClasses do not support key matching
        since the PClass is not a collection. Apart from that the transformations available
        for other persistent types work as expected.
        (   R
   (   R?   t   transformations(    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyR
   ’   s    c         C   sV   t  | |  j ƒ rR x9 |  j D]. } t |  | t ƒ t | | t ƒ k r t Sq Wt St S(   N(   RB   R>   R   R<   R=   t   FalseR7   t   NotImplemented(   R?   t   otherR   (    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyt   __eq__›   s    $c         C   s   |  | k S(   N(    (   R?   RH   (    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyt   __ne__¥   s    c            s#   t  t ‡  f d †  ˆ  j Dƒ ƒ ƒ S(   Nc         3   s'   |  ] } | t  ˆ  | t ƒ f Vq d  S(   N(   R<   R=   (   R   R   (   R?   (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pys	   <genexpr>ª   s    (   t   hashR   R   (   R?   (    (   R?   s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyt   __hash__¨   s    c         C   sJ   t  |  d t ƒ r- t d j | | ƒ ƒ ‚ n  t t |  ƒ j | | ƒ d  S(   NR   s'   Can't set attribute, key={0}, value={1}(   R<   RF   R5   R4   R   R*   t   __setattr__(   R?   R   R$   (    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyRM   ¬   s    c         C   s   t  d j | ƒ ƒ ‚ d  S(   Ns-   Can't delete attribute, key={0}, use remove()(   R5   R4   (   R?   R   (    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyt   __delattr__²   s    c         C   sI   i  } x< |  j  D]1 } t |  | t ƒ } | t k	 r | | | <q q W| S(   N(   R   R<   R=   (   R?   R%   R   R$   (    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyt   _to_dictµ   s    c         C   s5   d j  |  j j d j d „  |  j ƒ  j ƒ  Dƒ ƒ ƒ S(   Ns   {0}({1})s   , c         s   s-   |  ]# \ } } d  j  | t | ƒ ƒ Vq d S(   s   {0}={1}N(   R4   t   repr(   R   RA   t   v(    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pys	   <genexpr>À   s    (   R4   R>   R   R6   RO   R/   (   R?   (    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyt   __repr__¾   s    c            s2   t  ‡  f d †  ˆ  j Dƒ ƒ } t ˆ  j | f f S(   Nc         3   s3   |  ]) } t  ˆ  | ƒ r | t ˆ  | ƒ f Vq d  S(   N(   t   hasattrR<   (   R   R   (   R?   (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pys	   <genexpr>Ä   s    (   t   dictR   R   R>   (   R?   t   data(    (   R?   s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyt
   __reduce__Â   s    c         C   s   t  |  |  j ƒ  ƒ S(   s5   
        Returns an evolver for this object.
        (   t   _PClassEvolverRO   (   R?   (    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyt   evolverÇ   s    c         C   s   |  j  ƒ  } | | =| j ƒ  S(   s‰   
        Remove attribute given by name from the current instance. Raises AttributeError if the
        attribute doesn't exist.
        (   RX   t
   persistent(   R?   R   RX   (    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyt   removeÍ   s    N(   R   R   t   __doc__R   R;   t   classmethodR.   RF   RC   R   R
   RI   RJ   RL   RM   RN   RO   RR   RV   RX   RZ   (    (    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyR*   &   s"   		$	 			
									RW   c           B   s_   e  Z d Z d „  Z d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z	 d „  Z
 d „  Z RS(   t   _pclass_evolver_originalt   _pclass_evolver_datat   _pclass_evolver_data_is_dirtyR+   c         C   s+   | |  _  | |  _ t |  _ t ƒ  |  _ d  S(   N(   R]   R^   RF   R_   R;   R+   (   R?   t   originalt   initial_dict(    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyt   __init__Ú   s    			c         C   s   |  j  | S(   N(   R^   (   R?   t   item(    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyt   __getitem__à   s    c         C   sH   |  j  j | t ƒ | k	 rD | |  j  | <|  j j | ƒ t |  _ n  |  S(   N(   R^   t   getR=   R+   t   addR7   R_   (   R?   R   R$   (    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyR;   ã   s
    c         C   s   |  j  | | ƒ d  S(   N(   R;   (   R?   R   R$   (    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyt   __setitem__ë   s    c         C   sF   | |  j  k r6 |  j  | =|  j j | ƒ t |  _ |  St | ƒ ‚ d  S(   N(   R^   R+   t   discardR7   R_   R5   (   R?   Rc   (    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyRZ   î   s    
	c         C   s   |  j  | ƒ d  S(   N(   RZ   (   R?   Rc   (    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyt   __delitem__÷   s    c         C   s,   |  j  r% |  j j d |  j |  j  S|  j S(   NR+   (   R_   R]   R>   R+   R^   (   R?   (    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyRY   ú   s    	
c         C   s?   | |  j  k r" |  j | | ƒ n t t |  ƒ j | | ƒ d  S(   N(   R   R;   R   RW   RM   (   R?   R   R$   (    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyRM     s    c         C   s   |  | S(   N(    (   R?   Rc   (    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyt   __getattr__  s    (   R]   R^   R_   R+   (   R   R   R   Rb   Rd   R;   Rg   RZ   Ri   RY   RM   Rj   (    (    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyRW   ×   s   									(   t   sixt   pyrsistent._checked_typesR    R   R   R   t   pyrsistent._field_commonR   R   R   R   R   R	   t   pyrsistent._transformationsR
   R   t   typeR   t   objectR=   R)   t   add_metaclassR*   RW   (    (    (    s1   lib/python2.7/site-packages/pyrsistent/_pclass.pyt   <module>   s   ".				°