ó
¸á|Uc           @   s,   d  g Z  d  e f d     YZ d   Z d S(   t   Originc           B   sh   e  Z d  Z d   Z e d    Z d   Z d   Z d   Z d   Z	 d d  Z
 d	   Z d
   Z RS(   sG  This represents the origin of some object in some string.

    For example, if we have an object ``x1_obj`` that was produced by parsing
    the ``x1`` in the formula ``"y ~ x1:x2"``, then we conventionally keep
    track of that relationship by doing::

      x1_obj.origin = Origin("y ~ x1:x2", 4, 6)

    Then later if we run into a problem, we can do::

      raise PatsyError("invalid factor", x1_obj)

    and we'll produce a nice error message like::

      PatsyError: invalid factor
          y ~ x1:x2
              ^^

    Origins are compared by value, and hashable.
    c         C   s   | |  _  | |  _ | |  _ d  S(   N(   t   codet   startt   end(   t   selfR   R   R   (    (    s+   lib/python2.7/site-packages/patsy/origin.pyt   __init__#   s    		c         C   sü   g  } xU | D]M } | d k	 r; t | t  r; | j } n  | d k rM q n  | j |  q W| sh d St g  | D] } | j ^ qr  } t |  d k s˘ t  t	 g  | D] } | j
 ^ qŹ  } t g  | D] } | j ^ qÎ  } |  | j   | |  S(   s!  Class method for combining a set of Origins into one large Origin
        that spans them.

        Example usage: if we wanted to represent the origin of the "x1:x2"
        term, we could do ``Origin.combine([x1_obj, x2_obj])``.

        Single argument is an iterable, and each element in the iterable
        should be either:

        * An Origin object
        * ``None``
        * An object that has a ``.origin`` attribute which fulfills the above
          criteria.
          
        Returns either an Origin object, or None.
        i   N(   t   Nonet
   isinstanceR    t   origint   appendt   setR   t   lent   AssertionErrort   minR   t   maxR   t   pop(   t   clst   origin_objst   originst   objt   ot   codesR   R   (    (    s+   lib/python2.7/site-packages/patsy/origin.pyt   combine(   s    """c         C   s   |  j  |  j |  j !S(   sg   Extracts and returns the span of the original code represented by
        this Origin. Example: ``x1``.(   R   R   R   (   R   (    (    s+   lib/python2.7/site-packages/patsy/origin.pyt   relevant_codeI   s    c         C   sC   t  | t  oB |  j | j k oB |  j | j k oB |  j | j k S(   N(   R   R    R   R   R   (   R   t   other(    (    s+   lib/python2.7/site-packages/patsy/origin.pyt   __eq__N   s    c         C   s   |  | k S(   N(    (   R   R   (    (    s+   lib/python2.7/site-packages/patsy/origin.pyt   __ne__T   s    c         C   s   t  t |  j |  j |  j f  S(   N(   t   hashR    R   R   R   (   R   (    (    s+   lib/python2.7/site-packages/patsy/origin.pyt   __hash__W   s    i    c         C   s7   d d | |  j  d | d |  j d |  j |  j f S(   s3  Produces a user-readable two line string indicating the origin of
        some code. Example::

          y ~ x1:x2
              ^^

        If optional argument 'indent' is given, then both lines will be
        indented by this much. The returned string does not have a trailing
        newline.
        s   %s%s
%s%s%st    t   ^(   R   R   R   (   R   t   indent(    (    s+   lib/python2.7/site-packages/patsy/origin.pyt   caretizeZ   s    
c         C   sA   d |  j  |  j  |  j  |  j |  j !|  j  |  j |  j |  j f S(   Ns   <Origin %s->%s<-%s (%s-%s)>(   R   R   R   (   R   (    (    s+   lib/python2.7/site-packages/patsy/origin.pyt   __repr__l   s
    c         C   s
   t   d  S(   N(   t   NotImplementedError(   R   (    (    s+   lib/python2.7/site-packages/patsy/origin.pyt   __getstate__t   s    (   t   __name__t
   __module__t   __doc__R   t   classmethodR   R   R   R   R   R    R!   R#   (    (    (    s+   lib/python2.7/site-packages/patsy/origin.pyR       s   	!					c          C   sŤ  t  d d d  }  t  d d d  } |  j   d k s< t  | j   d k sT t  t  j |  | g  } | j d k s~ t  | j d k s t  | j d k s¨ t  | j d d  d k sĆ t  | t  d d d  k sä t  d	 t f d
     Y} t  j | |   |   d  g  } | |  k s-t  t  j | |   | g  } | | k sZt  t  j |   |   g  d  k st  d d l	 m
 } | t  d d d   d  S(   Nt   012345i   i   i   s   012345
  ^^s   012345
    ^R   s     012345
    ^^^t   ObjWithOriginc           B   s   e  Z d d   Z RS(   c         S   s   | |  _  d  S(   N(   R   (   R   R   (    (    s+   lib/python2.7/site-packages/patsy/origin.pyR      s    N(   R$   R%   R   R   (    (    (    s+   lib/python2.7/site-packages/patsy/origin.pyR)      s   i˙˙˙˙(   t   assert_no_picklingt    i    (   R    R    R   R   R   R   R   t   objectR   t
   patsy.utilR*   (   t   o1t   o2t   o3R)   t   o4t   o5R*   (    (    s+   lib/python2.7/site-packages/patsy/origin.pyt   test_Originw   s$    !'N(   t   __all__R,   R    R3   (    (    (    s+   lib/python2.7/site-packages/patsy/origin.pyt   <module>   s   	j