
    k{a8                     l    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 Zd Z G d	 d
      Zd Zd Zy)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                     | t        t        |             ni }|t        t        |            ni }t        |j                               }t        |j                               }||z  rt	        d      ||z  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.
    z"Can not compose overlapping cycles)nextitersetkeys
ValueError)leftrightl_peekr_peekl_keyr_keys         (/usr/lib/python3/dist-packages/cycler.py_process_keysr   4   sk     "&!1T$t*rF"'"3T$u+FEEu}=>>5=    c                 T   | j                   |j                   k7  rJt        dj                  | j                   |j                   z  | j                   |j                   z              | j                         |j                         t	        t
        f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})bothjust_onec              3   H   K   | ]  }t        ||   |   z           y wN_cycler).0k_l_rs     r   	<genexpr>zconcat.<locals>.<genexpr>e   s$     Ea2a52a5=1Es   ")r   r   formatby_keyr   r   )r   r   r    r!   s     @@r   concatr%   K   s    $ yyEJJ 44:F"&))ejj"8&*ii%**&< 5; 5>? 	?
 
B	B#E499EFFr   c                       e Zd ZdZd ZddZd Zed        Zd Z	e
d        Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd ZdZd Zd Zd ZeZd ZeZy)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                     t        |       S r   )r   selfs    r   __call__zCycler.__call__   s    T{r   Nc                 <   t        |t              r1t        |j                  |j                  |j                        | _        n2|)|D cg c]  }t        j
                  |       c}| _        nd| _        t        |t              r1t        |j                  |j                  |j                        | _        n2|)|D cg c]  }t        j
                  |       c}| _        nd| _        t        | j                  | j                        | _        || _        yc c}w c c}w )zf
        Semi-private init.

        Do not use this directly, use `cycler` function instead.
        N)
isinstancer'   _left_right_opcopyr   _keys)r*   r   r   opvs        r   __init__zCycler.__init__   s     dF#

DKKBDJ 1551$))A,5DJDJeV$ ellEIIFDK 277A499Q<7DKDK"4::t{{;
 6 8s   D;Dc                     || j                   v S r   )r2   )r*   r   s     r   __contains__zCycler.__contains__   s    DJJr   c                 ,    t        | j                        S )z!The keys this Cycler knows about.)r   r2   r)   s    r   r   zCycler.keys   s     4::r   c                 f   ||k(  ry|| j                   v rt        dj                  ||            || j                   vrt        dj                  ||            | j                   j	                  |       | j                   j                  |       | j                  5|| j                  j                  v r| j                  j                  ||       yt        | j                  t              r| j                  j                  ||       y| j                  D cg c]	  }|||   i c}| _
        yc c}w )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 key)r2   r   r#   KeyErrorremover   r/   r   
change_keyr-   r.   r'   )r*   r:   r;   entrys       r   r>   zCycler.change_key   s     #:$**HCS)  djj O"FsF46 6 	

#

s;;"sdkk.>.>'>KK""3, 

F+JJ!!#s+
 :>D3c
+DDJDs   D.c                 Z     | d      }t        f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   &   K   | ]  }|i 
 y wr    )r   r4   labels     r   r"   z$Cycler._from_iter.<locals>.<genexpr>   s     1%1s   )listr.   r2   )clsrC   itrrets    `  r   
_from_iterzCycler._from_iter   s.    ( $i1S11	G	
r   c                     t        t              r7| j                         }t        t        fd|j                         D              S t        d      )Nc              3   B   K   | ]  \  }}t        ||           y wr   r   )r   r   r4   keys      r   r"   z%Cycler.__getitem__.<locals>.<genexpr>   s      Mtq!1S6 2M   z+Can only use slices with Cycler.__getitem__)r-   slicer$   r   r   itemsr   )r*   rK   transs    ` r   __getitem__zCycler.__getitem__   s=    c5!KKME#Mu{{}MNNJKKr   c              #     K   | j                   | j                  D ]  }t        |        y | j                  | j                  | j                         D ]-  \  }}i }|j	                  |       |j	                  |       | / y wr   )r/   r.   dictr0   update)r*   r   abouts        r   __iter__zCycler.__iter__   su     ;;

 !4j ! T[[9 1

1

1		s   BBc                     t        |       t        |      k7  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*   others     r   __add__zCycler.__add__   sP     t9E
" $$'I;eCJ<A B BdE3''r   c                     t        t              rt        | t              S t        t              r7| j	                         }t        t        fd|j                         D              S t        S )z
        Outer product of two cyclers (`itertools.product`) or integer
        multiplication.

        Parameters
        ----------
        other : Cycler or int
        c              3   B   K   | ]  \  }}t        ||z          y wr   r   )r   r   r4   r\   s      r   r"   z!Cycler.__mul__.<locals>.<genexpr>  s      N11U7 3NrL   )	r-   r'   r   intr$   r   r   rN   NotImplemented)r*   r\   rO   s    ` r   __mul__zCycler.__mul__  sR     eV$$w//s#KKME#NNOO!!r   c                     | |z  S r   rB   r[   s     r   __rmul__zCycler.__rmul__  s    e|r   c                     t         t        t        t        i}| j                  t        | j                        S t        | j                        }t        | j                        } || j                     ||      S r   )rZ   minr   r   r/   rY   r.   r0   )r*   op_dictl_lenr_lens       r   __len__zCycler.__len__  s[    Wc*;;tzz?"DJJDKK  wtxx ..r   c                    t        |t              st        d      t        j                  |       }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'   	TypeErrorr1   r   r2   r.   rZ   r0   r/   r*   r\   old_selfs      r   __iadd__zCycler.__iadd__#  sd     %(@AA99T?"8U3

U[[%,,		Br   c                    t        |t              st        d      t        j                  |       }t	        ||      | _        || _        t        | _        t        |j                  |j                  |j                        | _	        | S )z
        In-place outer product of two cyclers (`itertools.product`).

        Parameters
        ----------
        other : Cycler
        z"Cannot *= with a non-Cycler object)
r-   r'   rl   r1   r   r2   r.   r   r0   r/   rm   s      r   __imul__zCycler.__imul__5  sd     %(@AA99T?"8U3

U[[%,,		Br   c                     t        |       t        |      k7  ry| j                  |j                  z  ryt        d t        | |      D              S )NFc              3   ,   K   | ]  \  }}||k(    y wr   rB   )r   rT   rU   s      r   r"   z Cycler.__eq__.<locals>.<genexpr>L  s     7da167s   )rY   r   allrZ   r[   s     r   __eq__zCycler.__eq__G  s@    t9E
"99uzz!7c$&6777r   c                     | |k(   S r   rB   r[   s     r   __ne__zCycler.__ne__N  s    EM""r   c                 2   t         dt        di}| j                  7| j                  j	                         t        fd| D              }dd|dS |j                  | j                  d      }d}|j                  | j                  || j                  	      S )
N+*c              3   (   K   | ]	  }|     y wr   rB   r   r4   labs     r   r"   z"Cycler.__repr__.<locals>.<genexpr>W  s     ,!qv,   zcycler(z, )?z({left!r} {op} {right!r}))r   r3   r   )
rZ   r   r/   r   poprD   getr0   r#   r.   )r*   op_maprF   r3   msgr}   s        @r   __repr__zCycler.__repr__S  s    sGS);;))--/C,t,,CSG2cWA..DHHc*B-C::4::"DKK:HHr   c                     d}t        | j                  t              }|D ]  }|d|dz  } t        |       D ]  }|dz  }|D ]  }|d||   dz  } |dz  }! |d	z  }|S )
Nz<table>)rK   z<th>z</th>z<tr>z<td>z</td>z</tr>z</table>)sortedr   reprr
   )r*   outputsorted_keysrK   dr   s         r   _repr_html_zCycler._repr_html_^  s    TYYD1 	*CSG5))F	*d 	AfF  /D1../gF		
 	*r   c                     | j                   }|D ci c]  }|t                }}| D ]   }|D ]  }||   j                  ||           " |S c c}w )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.
        )r   rD   append)r*   r   r   rV   r   s        r   r$   zCycler.by_keyl  sd    * yy"&'Qq$&y'' 	$A $Aad#$	$ 
 (s   Ac                 l    | j                         }t        t        d |j                         D              S )z
        Simplify the cycler into a sum (but no products) of cyclers.

        Returns
        -------
        simple : Cycler
        c              3   :   K   | ]  \  }}t        ||        y wr   r   r   r   r4   s      r   r"   z"Cycler.simplify.<locals>.<genexpr>  s     DdaGAqMD   )r$   r   r   rN   )r*   rO   s     r   simplifyzCycler.simplify  s(     cDekkmDEEr   )NN)__name__
__module____qualname____doc__r+   r5   r7   propertyr   r>   classmethodrH   rP   rW   r]   rb   rd   rj   ro   rq   ru   rw   __hash__r   r   r$   
_transposer   r%   rB   r   r   r'   r'   h   s    66  "EH  0L	(""/$$8# H	I< JF  Fr   r'   c                  V   | 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kD  rt        d      |r%t        t        d |j                         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              3   :   K   | ]  \  }}t        ||        y wr   r   r   s      r   r"   zcycler.<locals>.<genexpr>  s     EdaGAqMEr   z4Must have at least a positional OR keyword arguments)rl   rY   r-   r'   r   r   r   rN   )argskwargss     r   cyclerr     s    L  1 2 	2 4yA~$q'6* 4 5 5d1g	Ta~	TQ N O 	O cEfllnEFF
J
KKr   c                     t        |t              rB|j                  }t        |      dk7  rd}t	        |      |j                         fd|D        }t        j                  | |      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   (   K   | ]	  }|     y wr   rB   r|   s     r   r"   z_cycler.<locals>.<genexpr>  s     #!qv#r~   )r-   r'   r   rY   r   r   rH   )rC   rF   r   r   r}   s       @r   r   r     s\      #vxxt9>FCS/!hhj $s#UC((r   )r   r1   	functoolsr   	itertoolsr   r   operatorr   r   __version__r   r%   r'   r   r   rB   r   r   <module>r      sB   (V   $ .G:t tn	8Lv)r   