U
    ½mœdî  ã                   @   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   úE/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/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s„t‚|t dddƒks˜t‚G d	d
„ d
tƒ}t  || ƒ|ƒ d g¡}|| ksÊt‚t  || ƒ|g¡}||ksèt‚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o1Úo2Zo3r5   Zo4Zo5r6   r   r   r	   Útest_Originw   s$    r:   N)Ú__all__r8   r   r:   r   r   r   r	   Ú<module>   s   j