U
    Kvf                     @   s"   d gZ G dd  d eZdd ZdS )Originc                   @   s^   e Zd ZdZdd Ze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dS )r   aG  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codestartend)selfr   r   r    r   0/tmp/pip-unpacked-wheel-68fdvdus/patsy/origin.py__init__#   s    zOrigin.__init__c                 C   s   g }|D ]0}|dk	r$t |ts$|j}|dkr.q|| q|sBdS tdd |D }t|dksdttdd |D }tdd |D }| |	 ||S )a!  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.
        Nc                 S   s   g | ]
}|j qS r   )r   .0or   r   r	   
<listcomp>C   s     z"Origin.combine.<locals>.<listcomp>   c                 S   s   g | ]
}|j qS r   )r   r   r   r   r	   r   E   s     c                 S   s   g | ]
}|j qS r   )r   r   r   r   r	   r   F   s     )

isinstancer   originappendsetlenAssertionErrorminmaxpop)clsZorigin_objsZoriginsobjcodesr   r   r   r   r	   combine(   s    zOrigin.combinec                 C   s   | j | j| j S )zgExtracts and returns the span of the original code represented by
        this Origin. Example: ``x1``.r   r   r   r   r	   relevant_codeI   s    zOrigin.relevant_codec                 C   s.   t |to,| j|jko,| j|jko,| j|jkS r   )r   r   r   r   r   r   otherr   r   r	   __eq__N   s    



zOrigin.__eq__c                 C   s
   | |k S r   r   r   r   r   r	   __ne__T   s    zOrigin.__ne__c                 C   s   t t| j| j| jfS r   )hashr   r   r   r   r   r   r   r	   __hash__W   s    zOrigin.__hash__    c                 C   s.   dd| | j d| d| j d| j| j  f S )a3  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.
        z%s%s
%s%s%s ^r   )r   indentr   r   r	   caretizeZ   s    zOrigin.caretizec                 C   s<   d| j d | j | j | j| j | j | jd  | j| jf S )Nz<Origin %s->%s<-%s (%s-%s)>r   r   r   r   r	   __repr__l   s     zOrigin.__repr__c                 C   s   t d S r   )NotImplementedErrorr   r   r   r	   __getstate__t   s    zOrigin.__getstate__N)r%   )__name__
__module____qualname____doc__r
   classmethodr   r   r!   r"   r$   r)   r*   r,   r   r   r   r	   r      s   
 
c                  C   s$  t ddd} t ddd}|  dks(t| dks8tt | |g}|jdksTt|jdksbt|jdkspt|jdddkst|t dddkstG d	d
 d
t}t || | d g}|| kstt || |g}||kstt | | gd kstddlm	} |t ddd d S )NZ012345         z012345
  ^^z012345
    ^)r(   z  012345
    ^^^c                   @   s   e Zd ZdddZdS )z"test_Origin.<locals>.ObjWithOriginNc                 S   s
   || _ d S r   )r   )r   r   r   r   r	   r
      s    z+test_Origin.<locals>.ObjWithOrigin.__init__)N)r-   r.   r/   r
   r   r   r   r	   ObjWithOrigin   s   r5   r%   )assert_no_pickling )
r   r)   r   r   r   r   r   objectZ
patsy.utilr6   )Zo1Zo2Zo3r5   Zo4Zo5r6   r   r   r	   test_Originw   s$    r9   N)__all__r8   r   r9   r   r   r   r	   <module>   s   j