o
    k{a8                     @   sn   d Z ddlZddlmZ ddlmZmZ ddlmZm	Z	 dZ
dd Zd	d
 ZG dd dZdd Zdd ZdS )a  
Cycler
======

Cycling through combinations of values, producing dictionaries.

You can add cyclers::

    from cycler import cycler
    cc = (cycler(color=list('rgb')) +
          cycler(linestyle=['-', '--', '-.']))
    for d in cc:
        print(d)

Results in::

    {'color': 'r', 'linestyle': '-'}
    {'color': 'g', 'linestyle': '--'}
    {'color': 'b', 'linestyle': '-.'}


You can multiply cyclers::

    from cycler import cycler
    cc = (cycler(color=list('rgb')) *
          cycler(linestyle=['-', '--', '-.']))
    for d in cc:
        print(d)

Results in::

    {'color': 'r', 'linestyle': '-'}
    {'color': 'r', 'linestyle': '--'}
    {'color': 'r', 'linestyle': '-.'}
    {'color': 'g', 'linestyle': '-'}
    {'color': 'g', 'linestyle': '--'}
    {'color': 'g', 'linestyle': '-.'}
    {'color': 'b', 'linestyle': '-'}
    {'color': 'b', 'linestyle': '--'}
    {'color': 'b', 'linestyle': '-.'}
    N)reduce)productcycle)muladdz0.10.0c                 C   s`   | dur
t t| ni }|durt t|ni }t| }t| }||@ r,td||B S )a  
    Helper function to compose cycler keys.

    Parameters
    ----------
    left, right : iterable of dictionaries or None
        The cyclers to be composed.

    Returns
    -------
    keys : set
        The keys in the composition of the two cyclers.
    Nz"Can not compose overlapping cycles)nextitersetkeys
ValueError)leftrightZl_peekZr_peekZl_keyZr_key r   (/usr/lib/python3/dist-packages/cycler.py_process_keys4   s   r   c                    sZ   | j |j krtdj| j |j @ | j |j A d|   | tt fdd| j D S )aS  
    Concatenate `Cycler`\s, as if chained using `itertools.chain`.

    The keys must match exactly.

    Examples
    --------
    >>> num = cycler('a', range(3))
    >>> let = cycler('a', 'abc')
    >>> num.concat(let)
    cycler('a', [0, 1, 2, 'a', 'b', 'c'])

    Returns
    -------
    `Cycler`
        The concatenated cycler.
    zBKeys do not match:
	Intersection: {both!r}
	Disjoint: {just_one!r})ZbothZjust_onec                 3   s&    | ]}t | | |  V  qd S N_cycler.0kZ_lZ_rr   r   	<genexpr>e   s   $ zconcat.<locals>.<genexpr>)r
   r   formatby_keyr   r   )r   r   r   r   r   concatK   s   

r   c                   @   s   e Zd ZdZdd Zd+ddZdd Zed	d
 Zdd Z	e
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" ZdZd#d$ Zd%d& Zd'd( ZeZd)d* ZeZdS ),Cyclera  
    Composable cycles.

    This class has compositions methods:

    ``+``
      for 'inner' products (zip)

    ``+=``
      in-place ``+``

    ``*``
      for outer products (`itertools.product`) and integer multiplication

    ``*=``
      in-place ``*``

    and supports basic slicing via ``[]``.

    Parameters
    ----------
    left, right : Cycler or None
        The 'left' and 'right' cyclers.
    op : func or None
        Function which composes the 'left' and 'right' cyclers.
    c                 C   s   t | S r   )r   selfr   r   r   __call__      zCycler.__call__Nc                 C   s   t |trt|j|j|j| _n|durdd |D | _nd| _t |tr0t|j|j|j| _n|dur=dd |D | _nd| _t| j| j| _|| _dS )zf
        Semi-private init.

        Do not use this directly, use `cycler` function instead.
        Nc                 S      g | ]}t  |qS r   copyr   vr   r   r   
<listcomp>       z#Cycler.__init__.<locals>.<listcomp>c                 S   r!   r   r"   r$   r   r   r   r&      r'   )
isinstancer   _left_right_opr   _keys)r   r   r   opr   r   r   __init__   s   


zCycler.__init__c                 C   s
   || j v S r   )r,   )r   r   r   r   r   __contains__      
zCycler.__contains__c                 C   s
   t | jS )z!The keys this Cycler knows about.)r	   r,   r   r   r   r   r
      s   
zCycler.keysc                    s    krdS  | j v rtdj d| j vr"tdj d| j  | j   | jdurB| jjv rB| j  dS t	| j
trQ| j
  dS  fdd| j
D | _
dS )a  
        Change a key in this cycler to a new name.
        Modification is performed in-place.

        Does nothing if the old key is the same as the new key.
        Raises a ValueError if the new key is already a key.
        Raises a KeyError if the old key isn't a key.
        Nz6Can't replace {old} with {new}, {new} is already a key)oldnewz2Can't replace {old} with {new}, {old} is not a keyc                    s   g | ]} | iqS r   r   )r   entryr2   r1   r   r   r&      s    z%Cycler.change_key.<locals>.<listcomp>)r,   r   r   KeyErrorremover   r*   r
   
change_keyr(   r)   r   )r   r1   r2   r   r4   r   r7      s&   	

zCycler.change_keyc                    s,   | d}t  fdd|D |_ h|_|S )a  
        Class method to create 'base' Cycler objects
        that do not have a 'right' or 'op' and for which
        the 'left' object is not another Cycler.

        Parameters
        ----------
        label : str
            The property key.

        itr : iterable
            Finite length iterable of the property values.

        Returns
        -------
        `Cycler`
            New 'base' cycler.
        Nc                 3   s    | ]} |iV  qd S r   r   r$   labelr   r   r          z$Cycler._from_iter.<locals>.<genexpr>)listr)   r,   )clsr9   itrretr   r8   r   
_from_iter   s   zCycler._from_iterc                    s6   t  tr|  }tt fdd| D S td)Nc                 3   s"    | ]\}}t ||  V  qd S r   r   r   r   r%   keyr   r   r           z%Cycler.__getitem__.<locals>.<genexpr>z+Can only use slices with Cycler.__getitem__)r(   slicer   r   r   itemsr   )r   rB   transr   rA   r   __getitem__   s   
zCycler.__getitem__c                 c   sb    | j d u r| jD ]}t|V  q	d S | | j| j D ]\}}i }|| || |V  qd S r   )r*   r)   dictr+   update)r   r   aboutr   r   r   __iter__   s   



zCycler.__iter__c                 C   s8   t | t |krtdt |  dt | t| |tS )z
        Pair-wise combine two equal length cyclers (zip).

        Parameters
        ----------
        other : Cycler
        z&Can only add equal length cycles, not z and )lenr   r   zipr   otherr   r   r   __add__   s   zCycler.__add__c                    sH   t  trt|  tS t  tr"|  }tt fdd| D S tS )z
        Outer product of two cyclers (`itertools.product`) or integer
        multiplication.

        Parameters
        ----------
        other : Cycler or int
        c                 3   s"    | ]\}}t ||  V  qd S r   r   r@   rQ   r   r   r     rC   z!Cycler.__mul__.<locals>.<genexpr>)	r(   r   r   intr   r   r   rE   NotImplemented)r   rQ   rF   r   rS   r   __mul__  s   
	
zCycler.__mul__c                 C   s   | | S r   r   rP   r   r   r   __rmul__  r    zCycler.__rmul__c                 C   sD   t ttti}| jd u rt| jS t| j}t| j}|| j ||S r   )rO   minr   r   r*   rN   r)   r+   )r   Zop_dictZl_lenZr_lenr   r   r   __len__  s   



zCycler.__len__c                 C   L   t |ts	tdt| }t||| _|| _t| _t|j|j	|j| _	| S )z
        In-place pair-wise combine two equal length cyclers (zip).

        Parameters
        ----------
        other : Cycler
        z"Cannot += with a non-Cycler object)
r(   r   	TypeErrorr#   r   r,   r)   rO   r+   r*   r   rQ   Zold_selfr   r   r   __iadd__#     

zCycler.__iadd__c                 C   rZ   )z
        In-place outer product of two cyclers (`itertools.product`).

        Parameters
        ----------
        other : Cycler
        z"Cannot *= with a non-Cycler object)
r(   r   r[   r#   r   r,   r)   r   r+   r*   r\   r   r   r   __imul__5  r^   zCycler.__imul__c                 C   s<   t | t |kr
dS | j|jA rdS tdd t| |D S )NFc                 s   s    | ]	\}}||kV  qd S r   r   )r   rJ   rK   r   r   r   r   L  s    z Cycler.__eq__.<locals>.<genexpr>)rN   r
   allrO   rP   r   r   r   __eq__G  s
   zCycler.__eq__c                 C   s
   | |k S r   r   rP   r   r   r   __ne__N  r0   zCycler.__ne__c                    sn   t dtdi}| jd u r$| j  t fdd| D }d d|dS || jd}d	}|j| j	|| jd
S )N+*c                 3       | ]}|  V  qd S r   r   r$   Zlabr   r   r   W  r:   z"Cycler.__repr__.<locals>.<genexpr>zcycler(z, )?z({left!r} {op} {right!r}))r   r-   r   )
rO   r   r*   r
   popr;   getr+   r   r)   )r   Zop_mapr=   r-   msgr   rf   r   __repr__S  s   

zCycler.__repr__c                 C   st   d}t | jtd}|D ]
}|d|d7 }qt| D ]}|d7 }|D ]}|d|| d7 }q"|d7 }q|d	7 }|S )
Nz<table>rA   z<th>z</th>z<tr>z<td>z</td>z</tr>z</table>)sortedr
   reprr   )r   outputZsorted_keysrB   dr   r   r   r   _repr_html_^  s   
zCycler._repr_html_c                 C   s>   | j }dd |D }| D ]}|D ]}|| ||  qq|S )a  
        Values by key.

        This returns the transposed values of the cycler.  Iterating
        over a `Cycler` yields dicts with a single value for each key,
        this method returns a `dict` of `list` which are the values
        for the given key.

        The returned value can be used to create an equivalent `Cycler`
        using only `+`.

        Returns
        -------
        transpose : dict
            dict of lists of the values for each key.
        c                 S   s   i | ]}|t  qS r   )r;   r   r   r   r   
<dictcomp>  s    z!Cycler.by_key.<locals>.<dictcomp>)r
   append)r   r
   rL   rp   r   r   r   r   r   l  s   zCycler.by_keyc                 C   s    |   }ttdd | D S )z
        Simplify the cycler into a sum (but no products) of cyclers.

        Returns
        -------
        simple : Cycler
        c                 s       | ]
\}}t ||V  qd S r   r   r@   r   r   r   r         z"Cycler.simplify.<locals>.<genexpr>)r   r   r   rE   )r   rF   r   r   r   simplify  s   zCycler.simplify)NN)__name__
__module____qualname____doc__r   r.   r/   propertyr
   r7   classmethodr?   rG   rM   rR   rV   rW   rY   r]   r_   ra   rb   __hash__rl   rq   r   Z
_transposerv   r   r   r   r   r   r   h   s6    

$
r   c                  O   s   | r|rt dt| dkrt| d tst dt| d S t| dkr)t|  S t| dkr3t d|rAttdd | D S t d	)
a  
    Create a new `Cycler` object from a single positional argument,
    a pair of positional arguments, or the combination of keyword arguments.

    cycler(arg)
    cycler(label1=itr1[, label2=iter2[, ...]])
    cycler(label, itr)

    Form 1 simply copies a given `Cycler` object.

    Form 2 composes a `Cycler` as an inner product of the
    pairs of keyword arguments. In other words, all of the
    iterables are cycled simultaneously, as if through zip().

    Form 3 creates a `Cycler` from a label and an iterable.
    This is useful for when the label cannot be a keyword argument
    (e.g., an integer or a name that has a space in it).

    Parameters
    ----------
    arg : Cycler
        Copy constructor for Cycler (does a shallow copy of iterables).
    label : name
        The property key. In the 2-arg form of the function,
        the label can be any hashable object. In the keyword argument
        form of the function, it must be a valid python identifier.
    itr : iterable
        Finite length iterable of the property values.
        Can be a single-property `Cycler` that would
        be like a key change, but as a shallow copy.

    Returns
    -------
    cycler : Cycler
        New `Cycler` for the given property

    zBcyl() can only accept positional OR keyword arguments -- not both.   r   zDIf only one positional argument given, it must be a Cycler instance.   zdOnly a single Cycler can be accepted as the lone positional argument. Use keyword arguments instead.c                 s   rt   r   r   r@   r   r   r   r     ru   zcycler.<locals>.<genexpr>z4Must have at least a positional OR keyword arguments)r[   rN   r(   r   r   r   r   rE   )argskwargsr   r   r   cycler  s   &r   c                    sN   t |tr!|j}t|dkrd}t||   fdd|D }t| |S )aD  
    Create a new `Cycler` object from a property name and iterable of values.

    Parameters
    ----------
    label : hashable
        The property key.
    itr : iterable
        Finite length iterable of the property values.

    Returns
    -------
    cycler : Cycler
        New `Cycler` for the given property
    r~   z2Can not create Cycler from a multi-property Cyclerc                 3   re   r   r   r$   rf   r   r   r     r:   z_cycler.<locals>.<genexpr>)r(   r   r
   rN   r   ri   r?   )r9   r=   r
   rk   r   rf   r   r     s   
r   )rz   r#   	functoolsr   	itertoolsr   r   operatorr   r   __version__r   r   r   r   r   r   r   r   r   <module>   s    +  9;