3
$ßL]W>  ã               @   s”   d Z ddlmZmZmZmZ ddlZddlmZm	Z	 ddl
mZmZ ddlmZmZ ddlZdZdd	„ ZG d
d„ deƒZ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': '-.'}
é    )Úabsolute_importÚdivisionÚprint_functionÚunicode_literalsN)ÚproductÚcycle)ÚzipÚreduce)ÚmulÚaddz0.10.0c             C   s`   | dk	rt t| ƒƒni }|dk	r,t t|ƒƒni }t|jƒ ƒ}t|jƒ ƒ}||@ rXt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   ú*/tmp/pip-install-edjban26/cycler/cycler.pyÚ_process_keys7   s    r   c               @   s¾   e Zd ZdZdd„ Zd+dd„Zedd„ ƒZ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#d$„ Zd%d&„ ZeZd'd(„ Zd)d*„ 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 : Cycler or None
        The 'left' cycler

    right : Cycler or None
        The 'right' cycler

    op : func or None
        Function which composes the 'left' and 'right' cyclers.

    c             C   s   t | ƒS )N)r   )Úselfr   r   r   Ú__call__m   s    zCycler.__call__Nc             C   sš   t |tƒr t|j|j|jƒ| _n |dk	r:dd„ |D ƒ| _nd| _t |tƒr`t|j|j|jƒ| _n |dk	rzdd„ |D ƒ| _nd| _t| j| jƒ| _|| _dS )z\Semi-private init

        Do not use this directly, use `cycler` function instead.
        Nc             S   s   g | ]}t j |ƒ‘qS r   )Úcopy)Ú.0Úvr   r   r   ú
<listcomp>z   s    z#Cycler.__init__.<locals>.<listcomp>c             S   s   g | ]}t j |ƒ‘qS r   )r   )r   r   r   r   r   r   ƒ   s    )Ú
isinstancer   Ú_leftÚ_rightÚ_opr   Ú_keys)r   r   r   Úopr   r   r   Ú__init__p   s    

zCycler.__init__c             C   s
   t | jƒS )z2
        The keys this Cycler knows about
        )r   r!   )r   r   r   r   r   Š   s    zCycler.keysc                sº   ˆˆ krdS ˆ | j kr(tdˆˆ ˆ f ƒ‚ˆ| j krDtdˆˆ ˆf ƒ‚| j jˆƒ | j jˆ ƒ | jdk	r‚ˆ| jjkr‚| jjˆˆ ƒ n4t| j	t
ƒrž| j	jˆˆ ƒ n‡ ‡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.

        Nz-Can't replace %s with %s, %s is already a keyz)Can't replace %s with %s, %s is not a keyc                s   g | ]}ˆ |ˆ i‘qS r   r   )r   Úentry)ÚnewÚoldr   r   r   ²   s    z%Cycler.change_key.<locals>.<listcomp>)r!   r   ÚKeyErrorÚremover   r   r   Ú
change_keyr   r   r   )r   r&   r%   r   )r%   r&   r   r)   ‘   s    


zCycler.change_keyc             c   sB   x<| j | j| jƒD ](\}}tƒ }|j|ƒ |j|ƒ |V  qW dS )z†
        Compose the 'left' and 'right' components of this cycle
        with the proper operation (zip or product as of now)
        N)r    r   r   ÚdictÚupdate)r   ÚaÚbÚoutr   r   r   Ú_compose´   s
    

zCycler._composec                s0   | dƒ}t ‡ fdd„|D ƒƒ|_tˆ gƒ|_|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 : Cycler
            New 'base' `Cycler`
        Nc             3   s   | ]}ˆ |iV  qd S )Nr   )r   r   )Úlabelr   r   ú	<genexpr>Ô   s    z$Cycler._from_iter.<locals>.<genexpr>)Úlistr   r   r!   )Úclsr0   ÚitrÚretr   )r0   r   Ú
_from_iter¿   s    zCycler._from_iterc                s<   t ˆ tƒr0| jƒ }tt‡ fdd„tj|ƒD ƒƒS tdƒ‚d S )Nc             3   s    | ]\}}t ||ˆ  ƒV  qd S )N)Ú_cycler)r   Úkr   )Úkeyr   r   r1   Ü   s   z%Cycler.__getitem__.<locals>.<genexpr>z+Can only use slices with Cycler.__getitem__)r   ÚsliceÚby_keyr	   r   ÚsixÚ	iteritemsr   )r   r9   Útransr   )r9   r   Ú__getitem__Ø   s
    
zCycler.__getitem__c             C   s&   | j d krtdd„ | jD ƒƒS | jƒ S )Nc             s   s   | ]}t |ƒV  qd S )N)r*   )r   Úlr   r   r   r1   ã   s    z"Cycler.__iter__.<locals>.<genexpr>)r   r   r   r/   )r   r   r   r   Ú__iter__á   s    
zCycler.__iter__c             C   s4   t | ƒt |ƒkr(tdjt | ƒt |ƒƒƒ‚t| |tƒS )zœ
        Pair-wise combine two equal length cycles (zip)

        Parameters
        ----------
        other : Cycler
           The second Cycler
        z1Can only add equal length cycles, not {0} and {1})Úlenr   Úformatr   r   )r   Úotherr   r   r   Ú__add__ç   s    	zCycler.__add__c                sN   t ˆ tƒrt| ˆ tƒS t ˆ tƒrF| jƒ }tt‡ fdd„tj|ƒD ƒƒS t	S dS )zÓ
        Outer product of two cycles (`itertools.product`) or integer
        multiplication.

        Parameters
        ----------
        other : Cycler or int
           The second Cycler or integer
        c             3   s    | ]\}}t ||ˆ  ƒV  qd S )N)r7   )r   r8   r   )rD   r   r   r1     s   z!Cycler.__mul__.<locals>.<genexpr>N)
r   r   r   Úintr;   r	   r   r<   r=   ÚNotImplemented)r   rD   r>   r   )rD   r   Ú__mul__õ   s    


zCycler.__mul__c             C   s   | | S )Nr   )r   rD   r   r   r   Ú__rmul__  s    zCycler.__rmul__c             C   sD   t ttti}| jd kr t| jƒS t| jƒ}t| jƒ}|| j ||ƒS )N)r   Úminr   r
   r   rB   r   r    )r   Zop_dictZl_lenZr_lenr   r   r   Ú__len__  s    



zCycler.__len__c             C   sL   t |tƒstdƒ‚tj| ƒ}t||ƒ| _|| _t| _t|j|j	|jƒ| _	| S )z¥
        In-place pair-wise combine two equal length cycles (zip)

        Parameters
        ----------
        other : Cycler
           The second Cycler
        z"Cannot += with a non-Cycler object)
r   r   Ú	TypeErrorr   r   r!   r   r   r    r   )r   rD   Úold_selfr   r   r   Ú__iadd__  s    	

zCycler.__iadd__c             C   sL   t |tƒstdƒ‚tj| ƒ}t||ƒ| _|| _t| _t|j|j	|jƒ| _	| S )z§
        In-place outer product of two cycles (`itertools.product`)

        Parameters
        ----------
        other : Cycler
           The second Cycler
        z"Cannot *= with a non-Cycler object)
r   r   rL   r   r   r!   r   r   r    r   )r   rD   rM   r   r   r   Ú__imul__&  s    	

zCycler.__imul__c             C   s<   t | ƒt |ƒkrdS | j|jA r$dS tdd„ t| |ƒD ƒƒS )z 
        Check equality
        Fc             s   s   | ]\}}||kV  qd S )Nr   )r   r,   r-   r   r   r   r1   B  s    z Cycler.__eq__.<locals>.<genexpr>)rB   r   Úallr   )r   rD   r   r   r   Ú__eq__9  s
    zCycler.__eq__c                sn   t dtdi}| jd krD| jjƒ ‰ t‡ fdd„| D ƒƒ}djˆ |d�S |j| jdƒ}d}|j| j	|| jd	�S d S )
Nú+Ú*c             3   s   | ]}|ˆ  V  qd S )Nr   )r   r   )Úlabr   r   r1   H  s    z"Cycler.__repr__.<locals>.<genexpr>zcycler({lab!r}, {itr!r}))rT   r4   ú?z({left!r} {op} {right!r}))r   r"   r   )
r   r   r   r   Úpopr2   rC   Úgetr    r   )r   Zop_mapr4   r"   Úmsgr   )rT   r   Ú__repr__D  s    

zCycler.__repr__c             C   s€   d}t | jtd�}x|D ]}|dj|d�7 }qW xBt| ƒD ]6}|d7 }x |D ]}|dj|| d�7 }qLW |d7 }q:W |d7 }|S )	Nz<table>)r9   z<th>{key!r}</th>z<tr>z<td>{val!r}</td>)Úvalz</tr>z</table>)Úsortedr   ÚreprrC   r   )r   ÚoutputZsorted_keysr9   Údr8   r   r   r   Ú_repr_html_O  s    

zCycler._repr_html_c             C   sJ   | j }tdd„ |D ƒƒ}x,| D ]$}x|D ]}|| j|| ƒ q(W qW |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   | ]}|t ƒ fV  qd S )N)r2   )r   r8   r   r   r   r1   s  s    z Cycler.by_key.<locals>.<genexpr>)r   r*   Úappend)r   r   r.   r^   r8   r   r   r   r;   ]  s    

zCycler.by_keyc             C   s"   | j ƒ }ttdd„ tj|ƒD ƒƒS )zÊSimplify the Cycler

        Returned as a composition using only sums (no multiplications)

        Returns
        -------
        simple : Cycler
            An equivalent cycler using only summationc             s   s   | ]\}}t ||ƒV  qd S )N)r7   )r   r8   r   r   r   r   r1   �  s    z"Cycler.simplify.<locals>.<genexpr>)r;   r	   r   r<   r=   )r   r>   r   r   r   Úsimplify}  s    zCycler.simplifyc             C   s
   t | |ƒS )aF  Concatenate this cycler and an other.

        The keys must match exactly.

        This returns a single Cycler which is equivalent to
        `itertools.chain(self, other)`

        Examples
        --------

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

        Parameters
        ----------
        other : `Cycler`
            The `Cycler` to concatenate to this one.

        Returns
        -------
        ret : `Cycler`
            The concatenated `Cycler`
        )Úconcat)r   rD   r   r   r   rb   �  s    zCycler.concat)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r#   Úpropertyr   r)   r/   Úclassmethodr6   r?   rA   rE   rH   rI   rK   rN   rO   rQ   rY   r_   r;   Z
_transposera   rb   r   r   r   r   r   M   s,   
#	r   c                sj   | j |j kr>djdddgƒj| j |j @ | j |j A d�}t|ƒ‚| jƒ ‰ |jƒ ‰tt‡ ‡fdd„| j D ƒƒS )aø  Concatenate two cyclers.

    The keys must match exactly.

    This returns a single Cycler which is equivalent to
    `itertools.chain(left, right)`

    Examples
    --------

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

    Parameters
    ----------
    left, right : `Cycler`
        The two `Cycler` instances to concatenate

    Returns
    -------
    ret : `Cycler`
        The concatenated `Cycler`
    z
	zKeys do not match:zIntersection: {both!r}zDisjoint: {just_one!r})ZbothZjust_onec             3   s$   | ]}t |ˆ | ˆ|  ƒV  qd S )N)r7   )r   r8   )Ú_lÚ_rr   r   r1   Ñ  s    zconcat.<locals>.<genexpr>)r   ÚjoinrC   r   r;   r	   r   )r   r   rX   r   )ri   rj   r   rb   ¬  s    
rb   c              O   s�   | r|rt dƒ‚t| ƒdkr>t| d tƒs2t dƒ‚t| d ƒS t| ƒdkrRt| Ž S t| ƒdkrft dƒ‚|r„ttdd„ tj|ƒD ƒƒS t d	ƒ‚d
S )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   zEIf 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   s   | ]\}}t ||ƒV  qd S )N)r7   )r   r8   r   r   r   r   r1     s    zcycler.<locals>.<genexpr>z4Must have at least a positional OR keyword argumentsN)	rL   rB   r   r   r7   r	   r   r<   r=   )ÚargsÚkwargsr   r   r   ÚcyclerÔ  s    (rp   c                sN   t |tƒrB|j}t|ƒdkr(d}t|ƒ‚|jƒ ‰ ‡ fdd„|D ƒ}tj| |ƒS )aI  
    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
    rl   z2Can not create Cycler from a multi-property Cyclerc             3   s   | ]}|ˆ  V  qd S )Nr   )r   r   )rT   r   r   r1   ,  s    z_cycler.<locals>.<genexpr>)r   r   r   rB   r   rV   r6   )r0   r4   r   rX   r   )rT   r   r7     s    
r7   )rf   Ú
__future__r   r   r   r   r<   Ú	itertoolsr   r   Z	six.movesr   r	   Úoperatorr
   r   r   Ú__version__r   Úobjectr   rb   rp   r7   r   r   r   r   Ú<module>)   s     a(=