o
    #e                     @   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   ,lib/python3.10/site-packages/patsy/origin.py__init__#   s   
zOrigin.__init__c                 C   s   g }|D ]}|durt |ts|j}|du rq|| q|s!dS tdd |D }t|dks2J 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      g | ]}|j qS r   )r   .0or   r   r	   
<listcomp>C       z"Origin.combine.<locals>.<listcomp>   c                 S   r   r   )r   r   r   r   r	   r   E   r   c                 S   r   r   )r   r   r   r   r	   r   F   r   )	
isinstancer   originappendsetlenminmaxpop)clsZorigin_objsZoriginsobjZ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      
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 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J | dksJ t | |g}|jdks*J |jdks1J |jdks8J |jdddksBJ |t dddksLJ G d	d
 d
t}t || | d g}|| kseJ t || |g}||kstJ t | | gd u sJ 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
      r#   z+test_Origin.<locals>.ObjWithOrigin.__init__r   )r.   r/   r0   r
   r   r   r   r	   ObjWithOrigin   s    r6   r&   )assert_no_pickling )	r   r*   r   r   r   r   objectZ
patsy.utilr7   )Zo1Zo2Zo3r6   Zo4Zo5r7   r   r   r	   test_Originw   s$   r:   N)__all__r9   r   r:   r   r   r   r	   <module>   s   
j