B
    u9aå ã               @   sf  d dl Z d dlmZ d dlT d dlZd dlmZ d dlmZ d dl	Z	d dl
Z
d dlZd dlZd dlmZ d dlZd dlZejZe 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G dd dZdd Zdd Zdd Zd d! Z d"d# Z!d$d% Z"d&d' e#ej$d D Z%G d(d) d)e j&Z'G d*d+ d+e j&Z(G d,d- d-e j&Z)G d.d/ d/e j&Z*d0d1 Z+G d2d3 d3Z,G d4d5 d5Z-G d6d7 d7Z.G d8d9 d9Z/G d:d; d;Z0G d<d= d=Z1G d>d? d?Z2d@dA Z3G dBdC dCe j&Z4G dDdE dEe j&Z5G dFdG dGe j&Z6G dHdI dIe j&Z7ej8G dJdK dKe j&Z9dLZ:dMe:iZ;dSdNdOZ<e=dPkrbe<dQdR dS )Té    N)Úsupport)Ú*)ÚDecimal)ÚFraction)Úreduceé   c              G   s   t t|  S )N)ÚlistÚzip)Úargs© r   ú$/usr/lib/python3.7/test_itertools.pyÚlzip   s    r   c             C   s   d|  S )zTest function of one argumenté   r   )Úxr   r   r   Úonearg   s    r   c              G   s   t dS )z"Test function that raises an errorN)Ú
ValueError)r
   r   r   r   Úerrfunc   s    r   c              c   s   xdD ]
} | V  qW dS )zNon-restartable source sequence)r   r   r   Nr   )Úir   r   r   Úgen3   s    
r   c             C   s   | d dkS )zTest predicater   r   r   )r   r   r   r   ÚisEven!   s    r   c             C   s   | d dkS )zTest predicater   r   r   )r   r   r   r   ÚisOdd%   s    r   c              G   s   | S )Nr   )r
   r   r   r   Útupleize)   s    r   c             c   s   xt | D ]
}|V  q
W d S )N)Úrange)Únr   r   r   r   Úirange,   s    r   c               @   s    e Zd ZdZdd Zdd ZdS )ÚStopNowz"Class emulating an empty iterable.c             C   s   | S )Nr   )Úselfr   r   r   Ú__iter__2   s    zStopNow.__iter__c             C   s   t d S )N)ÚStopIteration)r   r   r   r   Ú__next__4   s    zStopNow.__next__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   0   s   r   c             C   s   t t|| S )zHConvenience function for partially consuming a long of infinite iterable)r   Úislice)r   Úseqr   r   r   Útake7   s    r&   c             C   s   t tj| dS )Nr   )r   ÚoperatorÚmul)Úiterabler   r   r   Úprod;   s    r*   c             C   s   t td| d S )Z	Factorialr   )r*   r   )r   r   r   r   Úfact>   s    r+   c             C   s   | d S )Nr   r   )Úrr   r   r   ÚtestRC   s    r-   c             C   s   | d S )Nr   r   )r,   r   r   r   ÚtestR2F   s    r.   c             C   s   | dk S )Né
   r   )r   r   r   r   ÚundertenI   s    r0   c             C   s   g | ]}|fd dqS )c             S   s   t  t  | |¡¡S )N)ÚpickleÚloadsÚdumps)ÚsÚprotor   r   r   Ú<lambda>L   ó    z<listcomp>.<lambda>r   )Ú.0r5   r   r   r   ú
<listcomp>L   s   r9   c               @   sÊ  e Zd Zd_ddZdd Zdd	 Zd
d Zdd Zdd Zdd Z	e
jdd Ze
 d¡dd Zdd Ze
jdd Ze
 d¡dd Zdd Ze
jdd  Ze
 d¡d!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Ze
 d¡d7d8 Zd9d: Z e
 d¡d;d< Z!d=d> Z"d?d@ Z#dAdB Z$e
jdCdD Z%e
 d¡dEdF Z&dGdH Z'dIdJ Z(dKdL Z)dMdN Z*dOdP Z+dQdR Z,dSdT Z-dUdV Z.dWdX Z/dYdZ Z0d[d\ Z1d]d^ Z2dS )`ÚTestBasicOpsé   r   Nc                s  d fdd	 t  ||¡}t  |¡}|  t|t|¡  | | }}	|  ||	¡ |rp |}
|  ||
¡ t  |¡}d}y&x t|D ]}t| |d7 }qW W n tk
rž   Y nX t  ||¡}t  |¡} | | }}	|  ||	¡ |r ||d }
|  ||
¡ dS )zITest that an iterator is the same after pickling, also when part-consumedr   c                sZ    dkrt dt| tr| S ytt| }W n tk
rD   | S X  fdd|D S )Nr/   zinfinite recursion encounteredc                s   g | ]} |d  qS )r   r   )r8   Úe)Úexpandr   r   r   r9   ]   s    z;TestBasicOps.pickletest.<locals>.expand.<locals>.<listcomp>)ÚRuntimeErrorÚ
isinstanceÚstrr   r$   Ú	TypeError)Úitr   Úl)r=   Ústop)r   r   r=   S   s    
z'TestBasicOps.pickletest.<locals>.expandr   N)r   )r1   r3   r2   ÚassertEqualÚtyper   Únextr   )r   ZprotocolrB   rD   r&   ÚcompareÚdumpZi2ÚaÚbÚcZi3Ztookr   Zi4r   )r=   rD   r   Ú
pickletestQ   s0    


zTestBasicOps.pickletestc             C   s,  |   tttdddddddddd	d
g
¡ |   tttddddddddddd	d
g
¡ xPttttfD ]@}|   ttt|tdtt|ddddddddd	d
g
¡ qhW |   ttddddg¡ |   ttg g ¡ |   ttdgdg¡ |  	t
ttddd¡ |  	t
t¡ | j	t
ttdd |  	t
ttdg g¡ ddddddddddg
}|   tt|tddddddddddg
¡ |   tt|tddddddddddg
¡ |   tt|tjddddddddddg
¡ |  	t
¡ tt|t W d Q R X x,ttjd D ]}|  |ttd¡ q
W d S )Nr/   r   r   é   é   é   é   é   é$   é-   )r)   ÚabcrJ   Úabé   é   )r   r   é   é	   r;   é   é   iÐ  i°  )rE   r   Ú
accumulater   ÚintÚcomplexr   r   ÚmapÚassertRaisesrA   ÚminÚmaxr'   r(   Úchrr1   ÚHIGHEST_PROTOCOLrM   )r   Útypr4   r5   r   r   r   Útest_accumulatez   s4    (zTestBasicOps.test_accumulatec             C   s   dd }xt |fD ]z}|  t|ddtd¡ |  t|dtd¡ |  t|dg ¡ |  td|ddtd¡ |  tt|d	d
¡ qW d S )Nc              w   s&   x | D ]}x|D ]
}|V  qW qW dS )zPure python version in the docsNr   )Ú	iterablesrB   Úelementr   r   r   Úchain2   s    

z'TestBasicOps.test_chain.<locals>.chain2rU   ÚdefÚabcdefÚ r;   Úabcdr   rN   )ÚchainrE   r   r&   ra   rA   )r   rj   rL   r   r   r   Ú
test_chain   s    zTestBasicOps.test_chainc             C   s   |   tt ddg¡td¡ |   tt dg¡td¡ |   tt dg¡g ¡ |   tdt ddg¡td¡ |  ttt ddg¡¡ d S )	NrU   rk   rl   rm   r;   rn   r   rN   )rE   r   ro   Úfrom_iterabler&   ra   rA   )r   r   r   r   Útest_chain_from_iterableŠ   s
     z%TestBasicOps.test_chain_from_iterablec          	   C   sæ   x®t jgt D ]}tdd}|  t||td¡ |  t|d¡ |  t||td¡ |  t|tdg ¡ |  td|tddtd¡ |  t	t|td	d
¡ qW x0t
tjd D ]}| j|tddtdd qÀW d S )NrU   rk   rl   rJ   Zbcdefrm   r;   rn   r   rN   r   )rH   )ÚcopyÚdeepcopyÚpicklecopiersro   rE   r   rG   r&   ra   rA   r   r1   re   rM   )r   ZoperrB   r5   r   r   r   Útest_chain_reducible­   s    
 z!TestBasicOps.test_chain_reduciblec          
   C   sÞ   |   tt jd¡ |   tt jg ¡ |   tt jd¡ |   tt jg f¡ |   tt jtg g f¡ t }| tddgf¡ |  t|ddddd	d
g¡ t }| tddgtdgf¡ |  t|dddddd	d
g¡ d S )Nr   r   rU   rk   rJ   rK   rL   Údr<   ÚfZghi)ra   rA   ro   Ú__setstate__ÚiterrE   r   )r   rB   r   r   r   Útest_chain_setstateº   s    z TestBasicOps.test_chain_setstatec       
   
      sÚ  |   ttd¡ |   ttddd¡ |   ttd ¡ |   ttdd¡ xÜdd gt D ]Ê}|  t|tddg ¡ |  t|tddd	d
ddddg¡ tdd}t| |  t||d
ddddg¡ |  t|ttddddddg¡ ttdd}t| |  t||dddg¡ qNW dd }dd }dd }xtdD ]}dd t|D xtt|d D ]b}tt|}|  t	|||krdnt
|t
| t
||  ¡ |  t	|t	t|¡ |  |t|¡ x|D ]x |  t	 |¡ |  t	t |¡ |  t t ¡ |  tfdd  D ¡ |  t  fd!dD ¡ qàW |  |t||¡ |  |t||¡ |  |t||¡ x*ttjd D ]}	|  |	t|¡ q®W qhW q@W d S )"NrU   r   r   éþÿÿÿc             S   s   | S )Nr   )rJ   r   r   r   r6   Í   r7   z0TestBasicOps.test_combinations.<locals>.<lambda>é    ÚABCD)ÚAÚB)r   ÚC)r   ÚD)r   r   )r   r   )r   r   r;   rN   )r   r   r   )r   r   rN   )r   r   rN   )r   r   rN   c             3   sÎ   t |  t }||krdS tt|}t  fdd|D V  xx.tt|D ]}|| || | krPP qPW dS ||  d7  < x*t|d |D ]}||d  d ||< qW t  fdd|D V  qBW dS )z%Pure python version shown in the docsNc             3   s   | ]} | V  qd S )Nr   )r8   r   )Úpoolr   r   ú	<genexpr>æ   s    zHTestBasicOps.test_combinations.<locals>.combinations1.<locals>.<genexpr>r   c             3   s   | ]} | V  qd S )Nr   )r8   r   )r   r   r   r   ð   s    )ÚtupleÚlenr   r   Úreversed)r)   r,   r   Úindicesr   Újr   )r   r   Úcombinations1ß   s    z5TestBasicOps.test_combinations.<locals>.combinations1c             3   sT   t |  t }x>tt||D ],}t|t|kr t  fdd|D V  q W dS )z%Pure python version shown in the docsc             3   s   | ]} | V  qd S )Nr   )r8   r   )r   r   r   r   ø   s    zHTestBasicOps.test_combinations.<locals>.combinations2.<locals>.<genexpr>N)r   r   Úpermutationsr   Úsortedr   )r)   r,   r   r   r   )r   r   Úcombinations2ò   s
    z5TestBasicOps.test_combinations.<locals>.combinations2c             3   sT   t |  t }x>tt||D ],}tt||kr t  fdd|D V  q W dS )zPure python version from cwr()c             3   s   | ]} | V  qd S )Nr   )r8   r   )r   r   r   r      s    zHTestBasicOps.test_combinations.<locals>.combinations3.<locals>.<genexpr>N)r   r   Úcombinations_with_replacementr   Úset)r)   r,   r   r   r   )r   r   Úcombinations3ú   s
    z5TestBasicOps.test_combinations.<locals>.combinations3rW   c             S   s   g | ]}d | d qS )rX   é   r   )r8   r   r   r   r   r9     s    z2TestBasicOps.test_combinations.<locals>.<listcomp>r   c             3   s   | ]}| kV  qd S )Nr   )r8   r<   )Úvaluesr   r   r     s    z1TestBasicOps.test_combinations.<locals>.<genexpr>c                s   g | ]}| kr|qS r   r   )r8   r<   )rL   r   r   r9     s    )ra   rA   Úcombinationsr   ru   rE   r   rG   r   r   r+   r   r   Ú
assertTrueÚallr1   re   rM   )
r   ÚopÚtestIntermediater   r   r   r   r,   Úresultr5   r   )rL   r   r   Útest_combinationsÇ   sP    
6

zTestBasicOps.test_combinationsc          	   C   s(   |   ttf¡ tdd W d Q R X d S )NÚAAi    )ra   ÚOverflowErrorÚMemoryErrorr   )r   r   r   r   Útest_combinations_overflow  s    z'TestBasicOps.test_combinations_overflowz"tuple reuse is specific to CPythonc          
   C   sH   |   tttttddd¡ |  ttttttddd¡ d S )NÚabcderN   r   )rE   r   r   r`   Úidr   ÚassertNotEqualr   )r   r   r   r   Útest_combinations_tuple_reuse  s     z*TestBasicOps.test_combinations_tuple_reusec          
      s  t }|  t|d¡ |  t|ddd¡ |  t|d ¡ |  t|dd¡ xldd gt D ]Z}|  t||dddd	d
dddg¡ |dd}t| |  t||d	d
dddg¡ qRW dd }dd }dd }xŒtdD ]®}dd t|D xt|d D ]}t||}	|  t	|	|||¡ |  t	|	t	t
|	¡ |  |	t|	¡ tt|}
|dksp|dkr~|  |	|
¡ n|  t
|	t
|
k¡ x|	D ] |  t	 |¡ dd t D }|  t	|t	t
|¡ |  t t ¡ |  tfdd D ¡ |  | fddD ¡ qW |  |	t||¡ |  |	t||¡ x*ttjd D ]}|  |||¡ qdW qüW qÔW d S )NrU   r   r   r|   c             S   s   | S )Nr   )rJ   r   r   r   r6   )  r7   zATestBasicOps.test_combinations_with_replacement.<locals>.<lambda>ÚABC)r   r   )r   r   )r   r   )r   r   )r   r   )r   r   c             3   sª   t |  t }|s|rdS dg| }t  fdd|D V  xfx*tt|D ]}|| |d krNP qNW dS || d g||  ||d< t  fdd|D V  q@W dS )z%Pure python version shown in the docsNr   c             3   s   | ]} | V  qd S )Nr   )r8   r   )r   r   r   r   :  s    zPTestBasicOps.test_combinations_with_replacement.<locals>.cwr1.<locals>.<genexpr>r   c             3   s   | ]} | V  qd S )Nr   )r8   r   )r   r   r   r   B  s    )r   r   r   r   )r)   r,   r   r   r   r   )r   r   Úcwr12  s    
z=TestBasicOps.test_combinations_with_replacement.<locals>.cwr1c             3   sV   t |  t }x@tt||dD ],}t|t|kr"t  fdd|D V  q"W dS )z%Pure python version shown in the docs)Úrepeatc             3   s   | ]} | V  qd S )Nr   )r8   r   )r   r   r   r   J  s    zPTestBasicOps.test_combinations_with_replacement.<locals>.cwr2.<locals>.<genexpr>N)r   r   Úproductr   r   r   )r)   r,   r   r   r   )r   r   Úcwr2D  s
    z=TestBasicOps.test_combinations_with_replacement.<locals>.cwr2c             S   s4   | s|rdS dS t | | d t | t | d  S )Nr   r   )r+   )r   r,   r   r   r   ÚnumcombsL  s    zATestBasicOps.test_combinations_with_replacement.<locals>.numcombsrW   c             S   s   g | ]}d | d qS )rX   r   r   )r8   r   r   r   r   r9   R  s    zCTestBasicOps.test_combinations_with_replacement.<locals>.<listcomp>r   c             S   s   g | ]\}}|qS r   r   )r8   ÚkÚvr   r   r   r9   b  s    c             3   s   | ]}| kV  qd S )Nr   )r8   r<   )r   r   r   r   e  s    zBTestBasicOps.test_combinations_with_replacement.<locals>.<genexpr>c                s   g | ]}| kr|qS r   r   )r8   r<   )rL   r   r   r9   g  s    )r   ra   rA   r   ru   rE   r   rG   r   r   r   r   r   r   Úgroupbyr   r1   re   rM   )r   Úcwrr   r   r£   rŠ   r§   r   r,   r   Zregular_combsZnorunsr5   r   )rL   r   r   Ú"test_combinations_with_replacement"  sL    

z/TestBasicOps.test_combinations_with_replacementc          	   C   s(   |   ttf¡ tdd W d Q R X d S )Nr   i   @)ra   r   r   r   )r   r   r   r   Ú+test_combinations_with_replacement_overflown  s    z8TestBasicOps.test_combinations_with_replacement_overflowc          
   C   sL   t }|  tttt|ddd¡ |  ttttt|ddd¡ d S )Nr   rN   r   )r   rE   r   r   r`   r   r    r   )r   r«   r   r   r   Ú.test_combinations_with_replacement_tuple_reuset  s     z;TestBasicOps.test_combinations_with_replacement_tuple_reusec          	      s   |   tt¡ |   ttddd¡ |   ttd ¡ |   ttdd¡ |  ttddg ¡ |   ttdd¡ |  tttdddd	d
dddg¡ ddd}ddd}x|tdD ]n}dd t|D  xTt|d D ]B}tt |}|  t|||krúdnt|t||  ¡ |  t|tt	|¡ |  |t
|¡ xN|D ]F}|  t||¡ |  tt	||¡ |  t fdd|D ¡ q>W |  |t| |¡ |  |t| |¡ ||krè|  |tt d ¡ |  |tt ¡ x*ttjd D ]}|  |t |¡ qøW qÐW qšW d S )NrU   r   r   r|   r}   r4   rN   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   c             3   s@  t |  t }|dkr|n|}||kr,dS tt|}tt|| d |d ddd }t  fdd|d| D V  xŸ|r:x²tt|D ]}||  d8  < || dkrè||d d |||d   ||d< || ||< q|| }||  ||  ||< || < t  fdd|d| D V  P qW dS q~W dS )z%Pure python version shown in the docsNr   éÿÿÿÿc             3   s   | ]} | V  qd S )Nr   )r8   r   )r   r   r   r     s    zHTestBasicOps.test_permutations.<locals>.permutations1.<locals>.<genexpr>r   c             3   s   | ]} | V  qd S )Nr   )r8   r   )r   r   r   r     s    )r   r   r   r   r   )r)   r,   r   r   Zcyclesr   r   r   )r   r   Úpermutations1  s&    $ ( z5TestBasicOps.test_permutations.<locals>.permutations1c             3   sf   t |  t }|dkr|n|}x@tt||dD ],}tt||kr2t  fdd|D V  q2W dS )z%Pure python version shown in the docsN)r€   c             3   s   | ]} | V  qd S )Nr   )r8   r   )r   r   r   r   £  s    zHTestBasicOps.test_permutations.<locals>.permutations2.<locals>.<genexpr>)r   r   r¥   r   r   )r)   r,   r   r   r   )r   r   Úpermutations2  s    z5TestBasicOps.test_permutations.<locals>.permutations2rW   c             S   s   g | ]}d | d qS )rX   r   r   )r8   r   r   r   r   r9   Š  s    z2TestBasicOps.test_permutations.<locals>.<listcomp>r   c             3   s   | ]}| kV  qd S )Nr   )r8   r<   )r   r   r   r   ¯  s    z1TestBasicOps.test_permutations.<locals>.<genexpr>)N)N)ra   rA   r   r   rE   r   r   r   r+   r   r   r   r   r1   re   rM   )r   r°   r±   r   r,   r   Úpr5   r   )r   r   Útest_permutationsz  s8    

	,
"
zTestBasicOps.test_permutationsc          	   C   s(   |   ttf¡ tdd W d Q R X d S )Nr   i   @)ra   r   r   r   )r   r   r   r   Útest_permutations_overflow¹  s    z'TestBasicOps.test_permutations_overflowc          
   C   sH   |   tttttddd¡ |  ttttttddd¡ d S )Nr   rN   r   )rE   r   r   r`   r   r   r    r   )r   r   r   r   Útest_permutations_tuple_reuseŸ  s     z*TestBasicOps.test_permutations_tuple_reusec          	      s(  x t dD ]}dd | }xþt dD ]ð tt| d}tt| }tt| }tt| }|  t||  ¡ |  t||r°t|  d t  t|d  n  ¡ |  t| |krÎdnt|t|   ¡ |  t| |krúdnt|t  t|   ¡ |  |t	t
|¡ |  |t	t
|¡ |  |t	t
|¡ |  |t	t
|¡ |  |dd |D ¡ |  | fd	d|D ¡ |  |d
d |D ¡ |  | fdd|D ¡ |  |ttt
|j|¡ |  |ttt
|j|¡ |  |t	t
|t
|@ ¡ q*W qW d S )NrO   ÚABCDEFGrY   )r€   r   r   c             S   s    g | ]}t |t|kr|qS r   )r   r   )r8   Útr   r   r   r9   Ü  s    z3TestBasicOps.test_combinatorics.<locals>.<listcomp>c                s    g | ]}t t| kr|qS r   )r   r   )r8   r·   )r,   r   r   r9   Ý  s    c             S   s    g | ]}t |t|kr|qS r   )r   r   )r8   r·   r   r   r   r9   Þ  s    c                s    g | ]}t t| kr|qS r   )r   r   )r8   r·   )r,   r   r   r9   ß  s    )r   r   r¥   r   r   r   rE   r   r+   r   r   ÚfilterÚ__contains__)r   r   r4   r*   r«   ZpermZcombr   )r,   r   Útest_combinatoricsÃ  s,    :,4zTestBasicOps.test_combinatoricsc             C   s  |   ttdddddddgdtd¡ |   ttdddddddgtd¡ |   ttdddddddgtd¡ |   ttdddddddgtd¡ |   ttddddgtd¡ |   ttdddddddgtd	¡ d
}t ttd|¡}t td¡}|   tt||dddg| ¡ |  ttd td¡ |  tttdd ¡ |  tttd¡ |  tttdd ¡ xdd dd gt	 D ]ô}xìdddddddgddfdddddddgddfdddddddgddfddddgddfdddddddgd	dfgD ]|\}}}}|   t|t||dt|¡ |   t|t||t|¡ t||}|rþt
| |   t||t|¡ qþW qW d S )NÚABCDEFr   r   )ÚdataÚ	selectorsÚACEFrm   ZACr¢   ZBCi'  rO   )r   r   rN   rX   c             S   s
   t   | ¡S )N)rs   )rJ   r   r   r   r6   õ  r7   z,TestBasicOps.test_compress.<locals>.<lambda>c             S   s
   t  | ¡S )N)rs   rt   )rJ   r   r   r   r6   õ  r7   ZCEFZBCDEFr   )rE   r   Úcompressro   rq   r€   r   ra   rA   ru   rG   )r   r   rŒ   rœ   r   Zresult1Zresult2r   r   r   r   Útest_compressä  s6    (&&& & & 
zTestBasicOps.test_compressc          	   C   sn  |   tdt dddg¡ |   tdtddddg¡ |   td	tdtdddg¡ |   td	tdtd
ddg¡ |   td	tdtdddg¡ |  ttd	dd¡ |  ttd¡ |   tdttd tt	td td ¡ |   tdtt d tt	t d t d ¡ |   tdtddddg¡ |   tdtddddg¡ |   tdtt
dt
dt
dt
dg¡ |   tdttd	dtd	dtddtddg¡ dd> }|   tdt|||d |d	 g¡ td}|   t|d ¡ t| |   t|d!¡ td"}|   t|d#¡ t| |   t|d$¡ |   ttd%d&¡ |   ttd'd(¡ |   tttd't¡ x\tj d tj d d)d
d*dtjd tjd fD ](}tt|}d+ |¡}|   ||¡ qªW x|ddtd td fD ]d}t|}|   tt |¡|¡ |   tt |¡|¡ x(t	tjd D ]}|  |t|¡ q4W qìW tdtd  t ¡  d S ),NrU   )rJ   r   )rK   r   )rL   r   rN   )rJ   rN   )rK   r;   )rL   rX   r   r¯   )rJ   r¯   )rK   r   éýÿÿÿ)rJ   rÁ   )rK   r|   r;   rJ   r/   rX   g      
@g      @g      @y      
@      Ày      @      Ày      @      Àz1.1z2.1z3.1rY   r   iè  zcount(3)zcount(4)i÷ÿÿÿz	count(-9)iøÿÿÿg     $@zcount(10.25)g      $@zcount(10.0)iöÿÿÿr   z	count(%r))rE   r   Úcountr&   r	   ra   rA   Úmaxsizer   r   r   r   ÚreprrG   rF   ÚfloatÚsysÚ__mod__rs   rt   r1   re   rM   Úexc_info)r   ÚBIGINTrL   r   Úr1Úr2Úvaluer5   r   r   r   Ú
test_count  sT       $6
 zTestBasicOps.test_countc             C   sv  |   tdtdddddg¡ |   tdtddddddg¡ |   tdtdd	d
ddg¡ |  ttdd¡ |   tdtdddddg¡ |   tdtdddddg¡ |   tdtdddddg¡ |   tdttd dtdttd td d¡ |   tdtt d dtdtt d t d d¡ |   tdtdtd ttdddtd   td ¡ |   tdtdddddg¡ |   tdtdddddg¡ |   tdtt	d t	d!t	d t	d"t	d#g¡ |   tdtt
ddt
dd$t
ddt
d%d&t
dd&g¡ dd'> }|   tdt|d	d|d| g¡ |   ttdtdd(tdd)d*g¡ tdd}|   t|d+¡ t| |   t|d,¡ td-d}|   t|d.¡ t| |   t|d.¡ td-d/}|   t|d0¡ t| |   t|d1¡ |   t|d1¡ |   ttd2dd3¡ |   ttd2dd4¡ |   ttd2d5d6¡ |   ttdd5d7¡ tdd5}|   tt|t¡ |   tt|t¡ xÞtj d tj d d8dddtjd tjd fD ]ª}x¢tj d tj d d8ddddtjd tjd f	D ]l}tt||}|dkr"d9| }nd:||f }|   ||¡ x*ttjd D ]}|  |t||¡ qJW qüW qÄW d S );NrU   r   rN   )rJ   r   )rK   rX   )rL   rY   )ÚstartÚstepr¯   )rÏ   )rJ   r   )rK   r¯   )rL   r|   rJ   rK   r   )rK   r   )rL   r   r   )rK   rN   )rL   r;   é   rP   éd   r/   rX   g      ô?g      
@g      @y      
@      Ày      @      Ày      !@       Àz1.1z.1z1.2z1.3rW   é   rQ   iè  g      @g      )@g      .@zcount(3, 5)zcount(8, 5)i÷ÿÿÿzcount(-9, 0)rÁ   zcount(-9, -3)zcount(-12, -3)g      %@zcount(10.5, 1.25)zcount(10.5)g      ð?zcount(10.5, 1.0)zcount(10, 1.0)iöÿÿÿz	count(%r)zcount(%r, %r))rE   r   rÂ   ra   rA   r&   rÃ   r   r   r   r   rÄ   rG   rF   r^   rÅ   rÆ   r1   re   rM   )r   rÉ   rL   r   r   rÊ   rË   r5   r   r   r   Útest_count_with_stride6  sf    28""&



68

z#TestBasicOps.test_count_with_stridec                s  |   tdtdtd¡ |   ttdg ¡ |  tt¡ |  ttd¡ |   tttt dddddddddddg
¡ td |   t d	¡ |   tdt	 
 ¡td
¡ xvttjd D ]d}|   tdt t  |¡¡td
¡ t  |   tdt t  |¡¡td¡ t  t  qŒW x(ttjd D ]}|  |td¡ q4W xŒttjd D ]ª}td}t|  fddtdD }t  |¡}t |¡}|   td|td¡ td}t|  fddtdD }t  |¡}t |¡}|   td|td¡ q^W d S )Nr/   rU   Z
abcabcabcarm   rX   r   r   r   rJ   Z
bcabcabcabZ
cabcabcabcr   c                s   g | ]}t  qS r   )rG   )r8   r   )rL   r   r   r9     s    z+TestBasicOps.test_cycle.<locals>.<listcomp>rÐ   Zcdeabcdeabcdeabcdeabc                s   g | ]}t  qS r   )rG   )r8   r   )rL   r   r   r9     s    rW   )rE   r&   Úcycler   ra   rA   r$   r   rG   rs   rt   r   r1   re   r2   r3   rM   rz   )r   r5   rB   Ú_r²   rw   r   )rL   r   Ú
test_cyclem  s>    0



zTestBasicOps.test_cyclec          	   C   s   t d}| tddf¡ |  td|td¡ t d}| tddf¡ |  td|td¡ |  t¡ t d tddg¡ W d Q R X |  t¡  t d}| tddf¡ W d Q R X td| |  t¡ t d tddf¡ W d Q R X |  tt d	jd
¡ |  tt d	jg f¡ d S )NÚdefgrU   r   rÐ   ZdefgabcdefgabcdefgabÚabcdefgr   r   rm   r   )rÔ   ry   r   rE   r&   ra   rA   r   )r   rL   r   r   r   Útest_cycle_setstate  s      
 z TestBasicOps.test_cycle_setstatec          
      s6  |   g ttg ¡ |   g ttg td¡ |  tttdg ¡ |  ttd ¡ |  ttddd d¡ dddd	d
dddg}g }xBt|dd D ]0\}}x&|D ]}|   ||d ¡ | |¡ qW qW |   ||¡ xvttj	d D ]d}g }xNt 
t t|t|¡¡D ]2\}}x(|D ] }|   ||d ¡ | |¡ qW qþW |   ||¡ qÜW g }xlt|tD ]^\}}xRt|tD ]D\}}x8|D ]0}|   ||d ¡ |   ||d ¡ | |¡ qvW qhW qTW |   ||¡ x²ttj	d D ] }g }xt 
t t|t|¡¡D ]l\}}x`t 
t t|t|¡¡D ]D\}}x8|D ]0}|   ||d ¡ |   ||d ¡ | |¡ q$W qW qôW |   ||¡ qÒW dd t|tD }	tdd |D }
|   t|	|
¡ |   t|	t|
¡ ttdtd}t|t}t|\}}t|\}}t|\}}|   t|g ¡ |   t|g ¡ |   t|d¡ t| |   t|g ¡ x\ttj	d D ]J}t|t}t|\}}t| t| |   tt 
t ||¡¡g ¡ qXW d}dd tt|D }|   |dddddg¡ dd tt|D }|   |dddg¡ d d tt|D }|   |d!d"d#d$d%g¡ td&d tt|D d'd(d d) }|   |d!d%d"g¡ G d*d+ d+t d5 fd,d-	}d tfd.d/}|   ||d¡ |   ||d¡ G  fd0d1d1}| | d g}| j ||td2 |   ||¡  fd3d4d_|   |d g¡ d_|   |d d g¡ d S )6N)ÚkeyrU   c             S   s   | S )Nr   )r   r   r   r   r6   Á  r7   z+TestBasicOps.test_groupby.<locals>.<lambda>r/   )r   r/   rÐ   )r   é   rQ   )r   r   rQ   )r   é   rQ   )r   é   é   )r   rP   rÞ   )rN   r[   é   )rN   rÒ   rß   c             S   s   | d S )Nr   r   )r,   r   r   r   r6   Ç  r7   r   r   r   c             S   s   g | ]\}}|qS r   r   )r8   rš   Úgr   r   r   r9   í  s    z-TestBasicOps.test_groupby.<locals>.<listcomp>c             S   s   g | ]}|d  qS )r   r   )r8   r,   r   r   r   r9   î  s    Z	AABBBAAAArZ   )r   rX   Zabracadabrac             S   s   g | ]\}}|qS r   r   )r8   rš   rà   r   r   r   r9   	  s    rJ   rK   rL   rw   r,   c             S   s$   g | ]\}}t t|d dr|qS )r   r   )r   r$   )r8   rš   rà   r   r   r   r9     s    c             S   s    g | ]\}}t t||fqS r   )r   r   )r8   rš   rà   r   r   r   r9     s    )rX   rJ   )r   rK   )r   rL   )r   rw   )r   r,   c             S   s    g | ]\}}t t||fqS r   )r   r   )r8   rš   rà   r   r   r   r9     s    T)ÚreverserN   c               @   s   e Zd ZdS )z0TestBasicOps.test_groupby.<locals>.ExpectedErrorN)r    r!   r"   r   r   r   r   ÚExpectedError  s   râ   c             3   s    xt | D ]
}dV  q
W  d S )NZyo)r   )r   r   )râ   r   r   Údelayed_raise  s    
z0TestBasicOps.test_groupby.<locals>.delayed_raisec                s    fddt | |D S )Nc                s   g | ]\}} |qS r   r   )r8   rš   rà   )Úfuncr   r   r9     s    z;TestBasicOps.test_groupby.<locals>.gulp.<locals>.<listcomp>)rª   )r)   Zkeyprä   r   )rä   r   Úgulp  s    z'TestBasicOps.test_groupby.<locals>.gulpc                   s   e Zd Z fddZdS )z+TestBasicOps.test_groupby.<locals>.DummyCmpc                s    d S )Nr   )r   Údst)râ   r   r   Ú__eq__&  s    z2TestBasicOps.test_groupby.<locals>.DummyCmp.__eq__N)r    r!   r"   rç   r   )râ   r   r   ÚDummyCmp%  s   rè   )rä   c                s$   j dkr j d8  _ | S  d S )Nr   r   )Úskip)Úobj)râ   Úkeyfuncr   r   rë   0  s    
z*TestBasicOps.test_groupby.<locals>.keyfunc)r   )rE   r   rª   r   ra   rA   Úappendr   r1   re   r2   r3   r-   r.   r   r   r	   rG   r   Ú	Exceptionré   )r   r4   Úduprš   rà   Úelemr5   ZikZigÚkeysZexpectedkeysrB   rÕ   Zg1Zg2Zg3r,   rã   rå   rè   r   )râ   rë   r   Útest_groupby»  s     


"

""


$&zTestBasicOps.test_groupbyc          
   C   sì  |   ttttddddg¡ |   ttd dddddgddg¡ |   tttdddddgddg¡ |   tdttt ddddg¡ |  t	t¡ |  t	tdd ¡ |  t	tdd tdd	¡ |  t	ttd
¡ |  t	t
ttdtd¡ dddg}tttd}|   tt |¡|¡ tttd}|   tt |¡|¡ xrttjd D ]`}tttd}|   tt t ||¡¡|¡ t
| |   tt t ||¡¡|dd  ¡ qPW x2ttjd D ] }tttd}|  ||¡ qÄW d S )NrO   r   r   r;   r   c             S   s   | S )Nr   )r   r   r   r   r6   C  r7   z*TestBasicOps.test_filter.<locals>.<lambda>c             S   s   | S )Nr   )r   r   r   r   r6   D  r7   rW   rN   )rE   r   rž   r   r   Úboolr&   rÂ   ra   rA   rG   rs   rt   r1   re   r2   r3   rM   )r   ÚansrL   r5   r   r   r   Útest_filter=  s,     $$"
,zTestBasicOps.test_filterc          
   C   s&  |   ttttddddg¡ |   ttd dddddgdddg¡ |   tttdddddgdddg¡ |   tdttt ddddg¡ |  t	t¡ |  t	td	d
 ¡ |  t	tdd
 tdd¡ |  t	ttd¡ |  t	t
ttdtd¡ x.ttjd D ]}|  |tttd¡ qW d S )NrO   r   rN   rX   r   r   r;   rW   c             S   s   | S )Nr   )r   r   r   r   r6   ^  r7   z/TestBasicOps.test_filterfalse.<locals>.<lambda>c             S   s   | S )Nr   )r   r   r   r   r6   _  r7   )rE   r   Úfilterfalser   r   rò   r&   rÂ   ra   rA   rG   r1   re   rM   )r   r5   r   r   r   Útest_filterfalseX  s     &&"zTestBasicOps.test_filterfalsec             C   s*  dd t dt D }|  |dddg¡ |  tt dtdtdtd¡ |  tt dtd	tdtd	¡ |  td	t dt tdtd	¡ |  tt dtd¡ |  tt  t ¡ |  tt d	¡ |  tt td	d	¡ |  d
d t ddD tdd¡ |  dd t ddD tdd¡ d S )Nc             S   s   g | ]\}}||fqS r   r   )r8   r   Úyr   r   r   r9   g  s    z)TestBasicOps.test_zip.<locals>.<listcomp>rU   )rJ   r   )rK   r   )rL   r   rO   rl   rN   c             S   s   g | ]}t t|qS r   )r   r   )r8   Úpairr   r   r   r9   p  s    rk   c             S   s   g | ]}|qS r   r   )r8   rø   r   r   r   r9   r  s    )	r	   rÂ   rE   r   r   r   r&   ra   rA   )r   ró   r   r   r   Útest_zipe  s    $$$zTestBasicOps.test_zipc          	   C   s  t tttdd}|  t|t|¡ t ttt tdd}|  tt 	|¡t|¡ dd t
 
tdt ¡D }|  |dddg¡ dd t
 tdt ¡D }|  |dddg¡ xLttjd	 D ]:}d
d t t tdt |¡¡D }|  |dddg¡ qÆW xXttjd	 D ]F}tdt }t| dd t t ||¡¡D }|  |ddg¡ qW x,ttjd	 D ]}|  |tdt ¡ qnW d S )NrU   rk   c             S   s   g | ]\}}||fqS r   r   )r8   r   r÷   r   r   r   r9   }  s    z5TestBasicOps.test_zip_tuple_reuse.<locals>.<listcomp>)rJ   r   )rK   r   )rL   r   c             S   s   g | ]\}}||fqS r   r   )r8   r   r÷   r   r   r   r9     s    r   c             S   s   g | ]\}}||fqS r   r   )r8   r   r÷   r   r   r   r9     s    c             S   s   g | ]\}}||fqS r   r   )r8   r   r÷   r   r   r   r9     s    )r   r`   r   r	   rE   rb   rc   r   ÚdictÚfromkeysrs   rÂ   rt   r   r1   re   r2   r3   rG   rM   )r   Úidsró   r5   r   r   r   r   Útest_zip_tuple_reuseu  s$    $z!TestBasicOps.test_zip_tuple_reusec                sV  xödt dgt ddgt dt ddt ddgt dt dt ddt d	t d
gt dt dt ddt d	t d
t dggD ]x  fddt ttt D }|  tt  |¡ |  tt i |¡ dd |D }|  tt tdd|¡ q|W |  tdtdt	 tt
dt d¡ |  tt tt
 ¡ |  ttg tt
g ¡ |  ttdtt
d¡ |  ttdi tt
tdd g d¡ |  ttd¡ |  ttt dd¡ xHdD ]@}yt|t t  W n tk
rð   Y nX |  d| ¡ qÂW |  dd tddD tt
dd¡ |  dd tddD tt
dd¡ d S )NrU   rO   iè  iÐ  i4  iž  iê  r   i°  iÜ  c                s"   g | ] t  fd dD qS )c                s$   g | ]} t |k r|  nd qS )N)r   )r8   Úarg)r   r   r   r9     s    z;TestBasicOps.test_ziplongest.<locals>.<listcomp>.<listcomp>)r   )r8   )r
   )r   r   r9     s   z0TestBasicOps.test_ziplongest.<locals>.<listcomp>c             S   s   g | ]}t d d |D qS )c             s   s   | ]}|d krdp|V  qd S )NÚXr   )r8   r<   r   r   r   r     s    z:TestBasicOps.test_ziplongest.<locals>.<listcomp>.<genexpr>)r   )r8   r·   r   r   r   r9     s    rÿ   )Ú	fillvaluerN   rl   r×   )zzip_longest('abc', fv=1)z3zip_longest('abc', fillvalue=1, bogus_keyword=None)zDid not raise Type in:  c             S   s   g | ]}t t|qS r   )r   r   )r8   rø   r   r   r   r9   µ  s    rk   c             S   s   g | ]}|qS r   r   )r8   rø   r   r   r   r9   ·  s    )rU   r×   )r   rc   r`   r   rE   r   Úzip_longestrú   r&   rÂ   r	   ra   rA   ÚevalÚglobalsÚlocalsZfail)r   ÚtargetZstmtr   )r
   r   Útest_ziplongest  s<    

"0
 (
zTestBasicOps.test_ziplongestc             C   s^   t tttdd}|  t|t|¡ t ttt tdd}|  tt 	|¡t|¡ d S )NrU   rk   )
r   r`   r   r  rE   rb   rc   r   rú   rû   )r   rü   r   r   r   Útest_zip_longest_tuple_reuseº  s    z)TestBasicOps.test_zip_longest_tuple_reusec          	   C   sh   xbt tjd D ]P}|  |tdd¡ |  |tdd¡ |  |tdddd¡ |  |tdd¡ qW d S )Nr   rU   rk   Zdefgh)r   rm   )r   r1   re   rM   r  )r   r5   r   r   r   Útest_zip_longest_picklingÁ  s
    z&TestBasicOps.test_zip_longest_picklingc             C   s°   G dd d}|ddt }|ddt }dd }|  |||d	d	d	d
g¡ |ddt}|ddt }t||dd}|  t|d	¡ |  t|d	¡ |  t|d	¡ |  tt|¡ d S )Nc               @   s$   e Zd Zdd Zdd Zdd ZdS )z,TestBasicOps.test_bug_7244.<locals>.Repeaterc             S   s   || _ t|| _|| _d S )N)Úor^   r·   r<   )r   r	  r·   r<   r   r   r   Ú__init__Ì  s    
z5TestBasicOps.test_bug_7244.<locals>.Repeater.__init__c             S   s   | S )Nr   )r   r   r   r   r   Ð  s    z5TestBasicOps.test_bug_7244.<locals>.Repeater.__iter__c             S   s(   | j dkr|  j d8  _ | jS | jd S )Nr   r   )r·   r	  r<   )r   r   r   r   r   Ò  s    
z5TestBasicOps.test_bug_7244.<locals>.Repeater.__next__N)r    r!   r"   r
  r   r   r   r   r   r   ÚRepeaterÊ  s   r  r   rN   r   r;   c          
   S   sT   g }xJt | |ddD ]8\}}t d¡ t||f W d Q R X | ||f¡ qW |S )Nr   )r   Ústdout)r  r   Zcaptured_outputÚprintrì   )rÊ   rË   r   r   r   r   r   r   ÚrunÝ  s    z'TestBasicOps.test_bug_7244.<locals>.run)r   r   )r   r   r   )r   )r   rE   r>   r  rG   ra   )r   r  rÊ   rË   r  rB   r   r   r   Útest_bug_7244È  s    zTestBasicOps.test_bug_7244c                s  xØg dgfdgddgft dt dgddd	d
ddgft dt dt dgg ft dt dt dgg ft dt dt dgg fgD ]V\}}|  tt| |¡ x8t dD ],}|  tt||  tt|t|d¡ qŠW qW |  tttt dgd  d¡ |  ttt dd ¡ dd }dd }dddt dt dtddddtdt dt	t dg	 x®t dD ]¢} fdd t t
 d!¡D }ttt|}|  ttt| |¡ |  tt| t|| ¡ |  tt| t|| ¡ tt|}|  ttt| |¡ qbW d S )"Nr   rV   )rJ   )rK   r   rN   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   r   r;   )r€   rW   rO   iË c                 sø   t tt| | dd¡ }t|}|dkr4dV  d S tdd |D rJd S dg| }tdd t||D V  xx~tt|D ]j}|| t|| d krq~||  d7  < xt|d |D ]}d||< qŒW tdd t||D V  P q~W d S qpW d S )	Nr€   r   r   r   c             s   s   | ]}t |d kV  qdS )r   N)r   )r8   r   r   r   r   r     s    z>TestBasicOps.test_product.<locals>.product1.<locals>.<genexpr>c             s   s   | ]\}}|| V  qd S )Nr   )r8   r   r   r   r   r   r   	  s    c             s   s   | ]\}}|| V  qd S )Nr   )r8   r   r   r   r   r   r     s    )	r   r`   r   Úgetr   Úanyr	   r   r   )r
   ÚkwdsÚpoolsr   r   r   r   r   r   r   Úproduct1   s&    
z+TestBasicOps.test_product.<locals>.product1c              ?   s\   t tt| | dd¡ }g g}x|D ]  fdd|D }q&W x|D ]}t|V  qFW dS )z Pure python version used in docsr€   r   c                s    g | ]} D ]}||g qqS r   r   )r8   r   r÷   )r   r   r   r9     s    z?TestBasicOps.test_product.<locals>.product2.<locals>.<listcomp>N)r   r`   r   r  )r
   r  r  r   r*   r   )r   r   Úproduct2  s    

z+TestBasicOps.test_product.<locals>.product2rm   rU   r   )rJ   rK   rL   rØ   rÛ   rÜ   rÑ   c                s   g | ]}t   ¡qS r   )ÚrandomÚchoice)r8   r   )Úargtypesr   r   r9   "  s    z-TestBasicOps.test_product.<locals>.<listcomp>rX   )r   rE   r   r¥   rú   r   ra   rA   r   r   r  Z	randranger*   r`   rz   )r   r
   r   r,   r  r  r   Zexpected_lenr   )r  r   Útest_productð  s2    $"	
zTestBasicOps.test_productc          	   C   s2   |   ttf¡ tdgd ddi W d Q R X d S )NrV   r}   r€   i   )ra   r   r   r¥   )r   r   r   r   Útest_product_overflow*  s    z"TestBasicOps.test_product_overflowc          
   C   sH   |   tttttddd¡ |  ttttttddd¡ d S )NrU   rk   r   )rE   r   r   r`   r   r¥   r    r   )r   r   r   r   Útest_product_tuple_reuse/  s     z%TestBasicOps.test_product_tuple_reusec          	   C   sì   xæg dgfdgddgft dt dgddd	d
ddgft dt dt dgg ft dt dt dgg ft dt dt dgg fgD ]d\}}|  tt t| ¡|¡ |  tt t| ¡|¡ x&t tjd D ]}|  |t| ¡ qÌW qW d S )Nr   rV   )rJ   )rK   r   rN   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   r   r   )	r   rE   r   rs   r¥   rt   r1   re   rM   )r   r
   r   r5   r   r   r   Útest_product_pickling4  s    $z"TestBasicOps.test_product_picklingc             C   sL   t dd}| d¡ |  t|d¡ t ddd}| d¡ |  tt|¡ d S )N)r   r   )rN   )r   i   )r   rN   r   )r   r   i   )r¥   ry   rE   rG   ra   r   )r   r²   r   r   r   Útest_product_issue_25021C  s    


z%TestBasicOps.test_product_issue_25021c             C   s   |   ttddddddg¡ |   ttdtddddg¡ |   ttdddddg¡ |   tdtddddg¡ |   ttddg ¡ |   ttddg ¡ |  tt¡ |  ttd dd	¡ |  ttd d¡ td
}|   t|d¡ td
d}|   t|d¡ t| |   t|d¡ tddd}|   t	|d¡ |   tdt
 
|¡td¡ |   tdt
 |¡td¡ x,ttjd D ]}|  |tddd¡ q~W d S )NrJ   rN   )ÚobjectÚtimes)r   rJ   )r   rJ   )r   rJ   r   rÁ   r;   y      ð?        zrepeat((1+0j))rX   zrepeat((1+0j), 5)zrepeat((1+0j), 0)r/   r   Zaar   )rE   r   r€   r   r   r&   ra   rA   rÄ   rG   rs   rt   r1   re   rM   )r   r,   rL   r5   r   r   r   Útest_repeatM  s,    
zTestBasicOps.test_repeatc             C   s`   |   ttddd¡ |   ttddd¡ |   ttdddd¡ |   ttdddd¡ d S )NrJ   r¯   zrepeat('a', 0)r|   )r  )rE   rÄ   r€   )r   r   r   r   Útest_repeat_with_negative_timesg  s    z,TestBasicOps.test_repeat_with_negative_timesc          	   C   sŽ  |   tttjtdtdddddg¡ |   tttdtddd	d
g¡ |   tttdt dd	d
g¡ |   tdttdt dd	g¡ |   tttjg g ¡ |  	t
t¡ |  	t
ttd tdtd¡ |  	t
ttj¡ |  	t
ttdtd¡ |  	ttttdgdg¡ |  	t
tttdgdg¡ dd	d
g}ttdt }|   tt |¡|¡ ttdt }|   tt |¡|¡ x2ttjd D ] }ttdt }|  ||¡ qW d S )NrN   r   rW   r   rY   rU   rX   )rJ   r   )rK   r   )rL   r   r   r/   r;   )rE   r   r`   r'   Úpowr   r   rÂ   r&   ra   rA   ÚnegrG   r   r   r   rs   rt   r1   re   rM   )r   ró   rL   r5   r   r   r   Útest_mapm  s.    

zTestBasicOps.test_mapc          
   C   sÆ  |   tttjttdtdddddg¡ |   tdttjtt tddddg¡ |   tttjg g ¡ |   tttjt	ddggdg¡ |  
ttttjd g¡ |  
tt¡ |  
tttjd	gd
¡ |  
tttdd	g¡ |  
ttttd	g¡ |  
ttttd	g¡ dddg}ttjttdtdd}|   tt |¡|¡ ttjttdtdd}|   tt |¡|¡ x@ttjd D ].}ttjttdtdd}|  ||¡ qW d S )NrN   r   rW   r   rY   r;   rX   i   )r;   rX   Úextrar/   )rE   r   Ústarmapr'   r"  r	   r   r&   rÂ   rz   ra   rA   rG   r   r   r   rs   rt   r1   re   rM   )r   ró   rL   r5   r   r   r   Útest_starmap  s(    " $
zTestBasicOps.test_starmapc       	   	   C   sX  x2dD ]*}|   tttdf| tt| ¡ qW x6dD ].\}}|   tttdf| tt| ¡ q:W |   tttdd ttd¡ |   tttdd d ttd¡ |   tttdd d d ttd¡ |   tttddd ttdd¡ |   tttddd dttddd¡ ttd}|   tt|dttd¡ |   t|ttdd¡ ttd}|   tt|ddg ¡ |   t|ttdd¡ td}|  tt|¡ |  tt|dddd¡ |  tt|d	dd¡ |  tt|dd	d
¡ |  tt|ddd
¡ |  tt|ddd¡ |  tt|d¡ |  tt|dd¡ |  tt|dd¡ |  tt|ddd¡ |  tt|ddd¡ |   tttt	 ddt
d¡ t	 }|   tt|ddddg¡ |   t|d¡ xdD ]}|   tt ttdf| ¡tt| ¡ |   tt ttdf| ¡tt| ¡ x2ttjd D ] }|  |ttdf| ¡ qFW qÚW dd dD }t |¡}t|d}|  | ¡ t| t ¡  |  | ¡ G dd dt}|   tttd|dttd¡ |   tttd|d|dttdd¡ |   tttd|d|d|dttddd¡ d S )N))r/   rÐ   rN   )r/   rN   rÐ   )r/   rÐ   )r/   r/   )r/   rN   )rÐ   rÑ   )))r/   én   rN   )r/   rÑ   rN   ))r/   r(  )r/   rÑ   ))r(  )rÑ   r/   r   r   rN   r;   éûÿÿÿr¯   r   rJ   é2   ))r/   rÐ   rN   )r/   rN   rÐ   )r/   rÐ   )r/   rN   )rÐ   c             s   s   | ]
}|V  qd S )Nr   )r8   r   r   r   r   r   ï  s    z+TestBasicOps.test_islice.<locals>.<genexpr>)r   r   c               @   s   e Zd Zdd Zdd ZdS )z)TestBasicOps.test_islice.<locals>.IntLikec             S   s
   || _ d S )N)Úval)r   r+  r   r   r   r
  ú  s    z2TestBasicOps.test_islice.<locals>.IntLike.__init__c             S   s   | j S )N)r+  )r   r   r   r   Ú	__index__ü  s    z3TestBasicOps.test_islice.<locals>.IntLike.__index__N)r    r!   r"   r
  r,  r   r   r   r   ÚIntLikeù  s   r-  rX   )rE   r   r$   r   rz   ra   rA   r   r   rÂ   rÃ   rG   rs   rt   r1   re   rM   ÚweakrefÚrefZassertIsNotNoner   Z
gc_collectZassertIsNoner  )	r   r
   ZtgtargsrB   ZrarL   r5   Zwrr-  r   r   r   Útest_isliceŠ  sj    
"$&&* 
&

&$zTestBasicOps.test_islicec             C   sR  ddddddddg}|   ttt|dddg¡ |   tttg g ¡ |  tt¡ |  tttj¡ |  tttjd	gd
¡ |  tttdd	g¡ |  t	ttt
d	g¡ ttddddddg}|   t|dddg¡ |  tt|¡ |   tt tt|¡dddg¡ |   tt tt|¡dddg¡ x*ttjd D ]}|  |tt|¡ q2W d S )Nr   rN   rX   rÐ   r   r;   rO   rY   )r;   rX   r%  r/   r   )rE   r   Ú	takewhiler0   ra   rA   r'   r"  rG   r   r   rò   r   rs   rt   r   r1   re   rM   )r   rŒ   r·   r5   r   r   r   Útest_takewhile  s     "zTestBasicOps.test_takewhilec             C   s$  ddddddddg}|   ttt|dddddg¡ |   tttg g ¡ |  tt¡ |  tttj¡ |  tttjd	gd
¡ |  tttdd	g¡ |  t	ttt
d	g¡ |   tt tt|¡dddddg¡ |   tt tt|¡dddddg¡ x*ttjd D ]}|  |tt|¡ qW d S )Nr   rN   rX   rÐ   r   r;   rO   rY   )r;   rX   r%  r/   )rE   r   Ú	dropwhiler0   ra   rA   r'   r"  rG   r   r   rs   rt   r   r1   re   rM   )r   rŒ   r5   r   r   r   Útest_dropwhile  s     &zTestBasicOps.test_dropwhilec             C   s  d}t g \}}|  t|g ¡ |  t|g ¡ t t|\}}|  t||tt|t|¡ t t|\}}|  t|tt|¡ |  t|tt|¡ t t|\}}x tdD ]}|  t||¡ qºW ~|  t|tt|¡ t t|\}}x"tdD ]}|  t||¡ qW ~|  t|ttd|¡ xtdD ]}dg| dg|  }t |¡ g g f}t t|}x(|D ] }t|| }	||  	|	¡ qW |  |d tt|¡ |  |d tt|¡ qFW |  
tt ¡ |  
tt d¡ |  
tt ddgd¡ |  
tt ddgdd¡ t d	\}}t|d
}
|  t|
td
¡ t tdd\}}}
x"tdD ]}|  t||¡ qjW |  t|ttd¡ |  t|
t|
gttd¡ |  t|ttdd¡ |  t|
ttdd¡ |  
tt d	d¡ |  
tt g d¡ x\tdD ]P}t d	|}|  t|t¡ |  t||¡ |  dd |D td	g| ¡ qW t d	\}}t |\}
}|  ||
k¡ t d	\}}t|}|  
t|¡ |  
t|d¡ ||}|  t|t|  koþt|  koþtd	kn  ¡ t td\}}t |¡}|  t|dt|¡ ~|  
tt|d¡ td	}ttd}t d	\}}|  tt |¡|¡ |  tt |¡|¡ t ttd\}}|  tt |¡|¡ |  tt |¡|¡ t d	\}}td| td| |  tt |¡|dd  ¡ |  tt |¡|dd  ¡ |  t||dd  ¡ |  t||dd  ¡ t td\}}td| td| |  tt |¡|dd  ¡ |  tt |¡|dd  ¡ |  t||dd  ¡ |  t||dd  ¡ t d	\}}|  tt |¡|¡ |  tt |¡|¡ |  t||¡ |  t||¡ t td\}}|  tt |¡|¡ |  tt |¡|¡ |  t||¡ |  t||¡ t d	\}}td| td| |  tt |¡|dd  ¡ |  tt |¡|dd  ¡ |  t||dd  ¡ |  t||dd  ¡ t td\}}td| td| |  tt |¡|dd  ¡ |  tt |¡|dd  ¡ |  t||dd  ¡ |  t||dd  ¡ xXttjd D ]F}|  |tt d	¡ t d	\}}| j|||d | j|||d qÐW d S )NéÈ   rÑ   rX   r   r   rN   r   r   rU   rk   iÐ  Zinvalidr¯   c             S   s   g | ]}t |qS r   )r   )r8   r   r   r   r   r9   i  s    z)TestBasicOps.test_tee.<locals>.<listcomp>r/   Ú	__class__i'  é<   )rH   )ÚteerE   r   r   r   r   rG   r  Zshufflerì   ra   rA   rF   r   r   r   r   r.  ÚproxyÚgetattrÚReferenceErrorrs   r&   rt   r1   re   rM   rz   )r   r   rJ   rK   r   r   ÚorderZlistsZitsrÌ   rL   r   rw   Zt1Zt2ZtnewZt3r²   ró   Zlong_ansr5   r   r   r   Útest_tee)  sæ     

 
&<








zTestBasicOps.test_teec             C   s8   t td d\}}yt| ~W n   ~~ Y nX d S )Ni -1)r8  r€   r  )r   ZforwardZbackwardr   r   r   Útest_tee_del_backwardŸ  s    z"TestBasicOps.test_tee_del_backwardc             C   s,  |   ttt ¡ x:ttttfD ]*}|   tt|g ¡ |   tt|t ¡ qW |   tttg d ¡ |   tttt d ¡ t	g \}}|   tt|¡ |   tt|¡ t	t \}}|   tt|¡ |   tt|¡ |   ttt
d d¡ xJttttttfD ]6}|   tt|dd g ¡ |   tt|dd t ¡ qîW d S )Nr   c             S   s   | S )Nr   )r   r   r   r   r6   Û  r7   z1TestBasicOps.test_StopIteration.<locals>.<lambda>c             S   s   | S )Nr   )r   r   r   r   r6   Ü  r7   )ra   r   rG   r	   ro   rÔ   rª   r   r$   r8  r€   rž   rõ   r`   r1  r3  r&  )r   rx   r²   Úqr   r   r   Útest_StopIterationÇ  s     zTestBasicOps.test_StopIteration)r;   r   N)3r    r!   r"   rM   rg   rp   rr   rv   r{   r   r   Zbigaddrspacetestr   Zimpl_detailr¡   r¬   r­   r®   r³   rŽ   rµ   rº   rÀ   rÍ   rÓ   rÖ   rÙ   rñ   rô   rö   rù   rý   r  r  r  r  r  r  r  r  r  r   r!  r$  r'  r0  r2  r4  r=  r>  r@  r   r   r   r   r:   O   s^   
)PL?!"07*$ *(:
^ 	r:   c               @   sÄ   e Z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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/S )0ÚTestExamplesc          	   C   s,   |   ttdddddgdddddg¡ d S )	Nr   r   rN   r;   rX   rO   r/   rP   )rE   r   r]   )r   r   r   r   rg   à  s    zTestExamples.test_accumulatec          
   C   sð   dddddg}dddddg}xzt tjd D ]h}t|}|  tt t ||¡¡|d d  ¡ |  t|d¡ |  tt t ||¡¡|dd  ¡ q,W t|}|  t|d¡ |  tt	 
|¡|dd  ¡ |  tt	 	|¡|dd  ¡ d S )	Nr   r   rN   r;   rX   rO   r/   rP   )r   r1   re   r]   rE   r   r2   r3   rG   rs   rt   )r   rŒ   Zaccumulatedr5   rB   r   r   r   Útest_accumulate_reducibleã  s    &*z&TestExamples.test_accumulate_reduciblec             C   s   t d d d gtj}|  t|d ¡ x<ttjd D ]*}t t 	||¡¡}|  t
|ddg¡ q2W |  t
t |¡ddg¡ |  t
t |¡ddg¡ d S )Nr   TF)r]   r'   Úis_rE   rG   r   r1   re   r2   r3   r   rs   rt   )r   rB   r5   Zit_copyr   r   r   Útest_accumulate_reducible_noneò  s    z+TestExamples.test_accumulate_reducible_nonec             C   s   |   d tdd¡d¡ d S )Nrm   r¢   ÚDEFr»   )rE   Újoinro   )r   r   r   r   rp   ü  s    zTestExamples.test_chainc             C   s    |   d t ddg¡¡d¡ d S )Nrm   r¢   rE  r»   )rE   rF  ro   rq   )r   r   r   r   rr   ÿ  s    z%TestExamples.test_chain_from_iterablec          	   C   sH   |   ttddddddddg¡ |   tttd	d
ddddg¡ d S )Nr~   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   r;   rN   )r   r   r   )r   r   rN   )r   r   rN   )r   r   rN   )rE   r   r   r   )r   r   r   r   r     s    zTestExamples.test_combinationsc          	   C   s&   |   ttddddddddg¡ d S )	Nr¢   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )rE   r   r   )r   r   r   r   r¬     s    z/TestExamples.test_combinations_with_replacementc             C   s*   |   ttdddddddgtd¡ d S )Nr»   r   r   rŸ   )rE   r   r¿   )r   r   r   r   rÀ     s    zTestExamples.test_compressc             C   s(   |   tttdddddddg¡ d S )Nr/   rX   rÛ   r   rÜ   rÝ   )rE   r   r$   rÂ   )r   r   r   r   rÍ     s    zTestExamples.test_countc             C   s"   |   tttddtd¡ d S )Nr~   r   ZABCDABCDABCD)rE   r   r$   rÔ   )r   r   r   r   rÖ     s    zTestExamples.test_cyclec          
   C   s.   |   ttdd dddddgdddg¡ d S )Nc             S   s   | dk S )NrX   r   )r   r   r   r   r6     r7   z-TestExamples.test_dropwhile.<locals>.<lambda>r   r;   rO   )rE   r   r3  )r   r   r   r   r4    s    zTestExamples.test_dropwhilec             C   sT   |   dd tdD td¡ |   dd tdD tdtdtd	td
g¡ d S )Nc             S   s   g | ]\}}|qS r   r   )r8   rš   rà   r   r   r   r9     s    z-TestExamples.test_groupby.<locals>.<listcomp>ZAAAABBBCCDAABBBZABCDABc             S   s   g | ]\}}t |qS r   )r   )r8   rš   rà   r   r   r   r9     s    Z
AAAABBBCCDZAAAAZBBBZCCr   )rE   rª   r   )r   r   r   r   rñ     s    
zTestExamples.test_groupbyc             C   s,   |   ttdd tddddddg¡ d S )	Nc             S   s   | d S )Nr   r   )r   r   r   r   r6     r7   z*TestExamples.test_filter.<locals>.<lambda>r/   r   rN   rX   rW   rZ   )rE   r   rž   r   )r   r   r   r   rô     s    zTestExamples.test_filterc             C   s,   |   ttdd tddddddg¡ d S )	Nc             S   s   | d S )Nr   r   )r   r   r   r   r6   "  r7   z/TestExamples.test_filterfalse.<locals>.<lambda>r/   r   r   r;   rO   rY   )rE   r   rõ   r   )r   r   r   r   rö   !  s    zTestExamples.test_filterfalsec             C   s"   |   tttdddddg¡ d S )N)r   rN   r/   )rX   r   rN   r}   rZ   iè  )rE   r   r`   r"  )r   r   r   r   r$  $  s    zTestExamples.test_mapc             C   st   |   ttddtd¡ |   ttdddtd¡ |   ttddd td¡ |   ttddd dtd¡ d S )	Nr¶   r   ÚABr;   ÚCDÚCDEFGr   ÚACEG)rE   r   r$   )r   r   r   r   r0  '  s    zTestExamples.test_islicec             C   s   |   ttddddg¡ d S )Nr~   Úxy)r   r   )r   r÷   )rE   r   r	   )r   r   r   r   rù   -  s    zTestExamples.test_zipc             C   s&   |   ttddddddddg¡ d S )	Nr~   rK  ú-)r   )r   r   )r   r÷   )r   rL  )r   rL  )rE   r   r  )r   r   r   r   Útest_zip_longest0  s    zTestExamples.test_zip_longestc          	   C   sL   |   ttddtttd ¡ ¡ |   tttdddddd	d
g¡ d S )Nr~   r   z#AB AC AD BA BC BD CA CB CD DA DB DCrN   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )rE   r   r   r`   r   Úsplitr   )r   r   r   r   r³   4  s    zTestExamples.test_permutationsc             C   sT   |   ttddtttd ¡ ¡ |   tttdddddd	d
ddddg¡ d S )Nr~   rK  zAx Ay Bx By Cx Cy Dx Dyr   rN   )r€   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )rE   r   r¥   r`   r   rN  r   )r   r   r   r   r  :  s
    zTestExamples.test_productc             C   s    |   ttdddddg¡ d S )Nr/   rN   )rE   r   r€   )r   r   r   r   r   A  s    zTestExamples.test_repeatc             C   s&   |   tttdddgdddg¡ d S )N)r   rX   )rN   r   )r/   rN   r}   rZ   iè  )rE   r   r&  r"  )r   r   r   r   Útest_stapmapD  s    zTestExamples.test_stapmapc          
   C   s,   |   ttdd dddddgddg¡ d S )Nc             S   s   | dk S )NrX   r   )r   r   r   r   r6   I  r7   z-TestExamples.test_takewhile.<locals>.<lambda>r   r;   rO   )rE   r   r1  )r   r   r   r   r2  H  s    zTestExamples.test_takewhileN)r    r!   r"   rg   rB  rD  rp   rr   r   r¬   rÀ   rÍ   rÖ   r4  rñ   rô   rö   r$  r0  rù   rM  r³   r  r   rO  r2  r   r   r   r   rA  Þ  s.   
rA  c               @   s    e Zd Zedd Zdd ZdS )ÚTestPurePythonRoughEquivalentsc       
      g   sæ   t | }|jpd|jptj|jp"d  }}}tt|||}yt|}W n0 t	k
rx   xt
t|| D ]\}}	qhW d S X y0x*t| D ]\}}	||kr|	V  t|}qW W n6 t	k
rà   x t
t|d || D ]\}}	qÐW Y nX d S )Nr   r   )ÚslicerÎ   rD   rÆ   rÃ   rÏ   rz   r   rG   r   r	   Ú	enumerate)
r)   r
   r4   rÎ   rD   rÏ   rB   Znextir   ri   r   r   r   r$   N  s"    $z%TestPurePythonRoughEquivalents.islicec          	   C   s6  |   t|  dd¡td¡ |   t|  ddd¡td¡ |   t|  ddd ¡td¡ |   t|  ddd d¡td¡ ttd	}|   t|  |d
¡ttd
¡ |   t|ttd
d	¡ ttd	}|   t|  |d
d
¡g ¡ |   t|ttd
d	¡ t }|   t|  |dd
d¡dg¡ |   t|d
¡ d S )Nr¶   r   rG  r;   rH  rI  r   rJ  r/   rN   r   r*  )rE   r   r$   rz   r   rÂ   rG   )r   rB   rL   r   r   r   Útest_islice_reciped  s      z1TestPurePythonRoughEquivalents.test_islice_recipeN)r    r!   r"   Ústaticmethodr$   rS  r   r   r   r   rP  L  s   rP  c               @   sÄ   e Z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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/S )0ÚTestGCc             C   s   |  |¡ t| ~~d S )N)rì   rG   )r   ÚiteratorÚ	containerr   r   r   Ú	makecyclex  s    
zTestGC.makecyclec             C   s    g }|   tdd|dg|¡ d S )Nr   r   rN   )rX  r]   )r   rJ   r   r   r   rg   }  s    zTestGC.test_accumulatec             C   s   g }|   t||¡ d S )N)rX  ro   )r   rJ   r   r   r   rp     s    zTestGC.test_chainc             C   s   g }|   t |g¡|¡ d S )N)rX  ro   rq   )r   rJ   r   r   r   rr     s    zTestGC.test_chain_from_iterablec             C   s"   g }|   tdd|dgd|¡ d S )Nr   r   rN   )rX  r   )r   rJ   r   r   r   r     s    zTestGC.test_combinationsc             C   s"   g }|   tdd|dgd|¡ d S )Nr   r   rN   )rX  r   )r   rJ   r   r   r   r¬     s    z)TestGC.test_combinations_with_replacementc          
   C   s&   g }|   tdddddddg|¡ d S )Nr»   r   r   )rX  r¿   )r   rJ   r   r   r   rÀ     s    zTestGC.test_compressc             C   s6   g }t dtft|d}|  t|d|d|¡ d S )NÚInt)r   r   r   )rF   r^   rú   rX  rÂ   )r   rJ   rY  r   r   r   rÍ     s    zTestGC.test_countc             C   s   g }|   t|gd |¡ d S )Nr   )rX  rÔ   )r   rJ   r   r   r   rÖ     s    zTestGC.test_cyclec             C   s    g }|   ttd||g|¡ d S )Nr   )rX  r3  rò   )r   rJ   r   r   r   r4    s    zTestGC.test_dropwhilec             C   s$   g }|   t|gd dd |¡ d S )Nr   c             S   s   | S )Nr   )r   r   r   r   r6   €  r7   z%TestGC.test_groupby.<locals>.<lambda>)rX  rª   )r   rJ   r   r   r   rñ   ¢  s    zTestGC.test_groupbyc             C   sB   d}dd }x0t t||dD ]\}}|j dg ¡ |¡ qW d S )Nr/   c             S   s   | S )Nr   )r   r   r   r   r6   ©  r7   z'TestGC.test_issue2246.<locals>.<lambda>)rÚ   r   )rª   r   Ú__dict__Ú
setdefaultrì   )r   r   rë   r   r   r   r   r   Útest_issue2246Š  s    zTestGC.test_issue2246c             C   s$   g }|   tdd |gd |¡ d S )Nc             S   s   dS )NTr   )r   r   r   r   r6   ¯  r7   z$TestGC.test_filter.<locals>.<lambda>r   )rX  rž   )r   rJ   r   r   r   rô   ­  s    zTestGC.test_filterc             C   s   g }|   tdd ||¡ d S )Nc             S   s   dS )NFr   )r   r   r   r   r6   ³  r7   z)TestGC.test_filterfalse.<locals>.<lambda>)rX  rõ   )r   rJ   r   r   r   rö   ±  s    zTestGC.test_filterfalsec             C   s&   g }|   t|gd |gd |¡ d S )Nr   rN   )rX  r	   )r   rJ   r   r   r   rù   µ  s    zTestGC.test_zipc             C   sP   g }|   t|gd |gd |¡ |d g}|   t|gd |gd |d|¡ d S )Nr   rN   )r   )rX  r  )r   rJ   rK   r   r   r   rM  ¹  s    zTestGC.test_zip_longestc             C   s$   g }|   tdd |gd |¡ d S )Nc             S   s   | S )Nr   )r   r   r   r   r6   Á  r7   z!TestGC.test_map.<locals>.<lambda>r   )rX  r`   )r   rJ   r   r   r   r$  ¿  s    zTestGC.test_mapc             C   s    g }|   t|gd d |¡ d S )Nr   )rX  r$   )r   rJ   r   r   r   r0  Ã  s    zTestGC.test_islicec             C   s"   g }|   tdd|dgd|¡ d S )Nr   r   rN   )rX  r   )r   rJ   r   r   r   r³   Ç  s    zTestGC.test_permutationsc             C   s$   g }|   tdd|dgdd|¡ d S )Nr   r   rN   )r€   )rX  r¥   )r   rJ   r   r   r   r  Ë  s    zTestGC.test_productc             C   s   g }|   t||¡ d S )N)rX  r€   )r   rJ   r   r   r   r   Ï  s    zTestGC.test_repeatc             C   s(   g }|   tdd ||fgd |¡ d S )Nc              W   s   | S )Nr   )r·   r   r   r   r6   Õ  r7   z%TestGC.test_starmap.<locals>.<lambda>r   )rX  r&  )r   rJ   r   r   r   r'  Ó  s    zTestGC.test_starmapc             C   s"   g }|   ttdd||g|¡ d S )Nr   r   )rX  r1  rò   )r   rJ   r   r   r   r2  ×  s    zTestGC.test_takewhileN)r    r!   r"   rX  rg   rp   rr   r   r¬   rÀ   rÍ   rÖ   r4  rñ   r\  rô   rö   rù   rM  r$  r0  r³   r  r   r'  r2  r   r   r   r   rU  v  s.   rU  c             c   s   x| D ]
}|V  qW dS )zRegular generatorNr   )Úseqnr   r   r   r   ÚRÛ  s    
r^  c               @   s    e Zd ZdZdd Zdd ZdS )ÚGzSequence using __getitem__c             C   s
   || _ d S )N)r]  )r   r]  r   r   r   r
  â  s    z
G.__init__c             C   s
   | j | S )N)r]  )r   r   r   r   r   Ú__getitem__ä  s    zG.__getitem__N)r    r!   r"   r#   r
  r`  r   r   r   r   r_  à  s   r_  c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	ÚIz Sequence using iterator protocolc             C   s   || _ d| _d S )Nr   )r]  r   )r   r]  r   r   r   r
  é  s    z
I.__init__c             C   s   | S )Nr   )r   r   r   r   r   ì  s    z
I.__iter__c             C   s2   | j t| jkrt| j| j  }|  j d7  _ |S )Nr   )r   r   r]  r   )r   r©   r   r   r   r   î  s
     z
I.__next__N)r    r!   r"   r#   r
  r   r   r   r   r   r   ra  ç  s   ra  c               @   s    e Zd ZdZdd Zdd ZdS )ÚIgz9Sequence using iterator protocol defined with a generatorc             C   s   || _ d| _d S )Nr   )r]  r   )r   r]  r   r   r   r
  ö  s    zIg.__init__c             c   s   x| j D ]
}|V  qW d S )N)r]  )r   r+  r   r   r   r   ù  s    zIg.__iter__N)r    r!   r"   r#   r
  r   r   r   r   r   rb  ô  s   rb  c               @   s    e Zd ZdZdd Zdd ZdS )rÿ   z Missing __getitem__ and __iter__c             C   s   || _ d| _d S )Nr   )r]  r   )r   r]  r   r   r   r
  ÿ  s    z
X.__init__c             C   s2   | j t| jkrt| j| j  }|  j d7  _ |S )Nr   )r   r   r]  r   )r   r©   r   r   r   r     s
     z
X.__next__N)r    r!   r"   r#   r
  r   r   r   r   r   rÿ   ý  s   rÿ   c               @   s    e Zd ZdZdd Zdd ZdS )ÚNzIterator missing __next__()c             C   s   || _ d| _d S )Nr   )r]  r   )r   r]  r   r   r   r
  
  s    z
N.__init__c             C   s   | S )Nr   )r   r   r   r   r     s    z
N.__iter__N)r    r!   r"   r#   r
  r   r   r   r   r   rc    s   rc  c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	ÚEzTest propagation of exceptionsc             C   s   || _ d| _d S )Nr   )r]  r   )r   r]  r   r   r   r
    s    z
E.__init__c             C   s   | S )Nr   )r   r   r   r   r     s    z
E.__iter__c             C   s   dd  d S )NrN   r   r   )r   r   r   r   r     s    z
E.__next__N)r    r!   r"   r#   r
  r   r   r   r   r   r   rd    s   rd  c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	ÚSzTest immediate stopc             C   s   d S )Nr   )r   r]  r   r   r   r
    s    z
S.__init__c             C   s   | S )Nr   )r   r   r   r   r     s    z
S.__iter__c             C   s   t d S )N)r   )r   r   r   r   r      s    z
S.__next__N)r    r!   r"   r#   r
  r   r   r   r   r   r   re    s   re  c             C   s   t tdd ttt| S )z Test multiple tiers of iteratorsc             S   s   | S )Nr   )r   r   r   r   r6   %  r7   zL.<locals>.<lambda>)ro   r`   r^  rb  r_  )r]  r   r   r   ÚL#  s    rf  c               @   s   e Z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dd Zdd Zdd Zdd  Zd!S )"ÚTestVariousIteratorArgsc             C   sª   dddddg}dddddg}t |}x.tttttfD ]}|  tt|||¡ q4W |  ttt	|g ¡ |  
ttt|¡ |  
ttt|¡ |  
tttt|¡ d S )	Nr   r   rN   r;   rX   rO   r/   rP   )r   r_  ra  rb  rf  r^  rE   r   r]   re  ra   rA   rÿ   rc  ÚZeroDivisionErrorrd  )r   r4   r,   r   rà   r   r   r   rg   *  s    z'TestVariousIteratorArgs.test_accumulatec          	   C   sÖ   xÐddt ddt dddfD ]²}xjttttttfD ]V}|  tt	||t||¡ |  tt	||||t||t|| ¡ q2W |  
ttt	t|¡ |  
ttt	t|¡ |  
ttt	t|¡ qW d S )NÚ123rm   iè  )Údog333333ó?iÐ  i  rX   )r   r_  ra  rb  re  rf  r^  rE   r   ro   ra   rA   rÿ   rc  rh  rd  )r   r4   rà   r   r   r   rp   5  s      6z"TestVariousIteratorArgs.test_chainc          	   C   sŒ   x¶ddt ddt dddfD ]}t|}x>ttttttfD ]*}|  t	t
||tdt	||¡ q:W |  tt
t|td¡ |  tt
t|td¡ |  tt	t
t|td¡ qW d S )	Nri  rm   iè  )rj  g333333ó?iÐ  i  rX   r   )r   r   r_  ra  rb  re  rf  r^  rE   r   r¿   r€   ra   rA   rÿ   rc  rh  rd  )r   r4   r   rà   r   r   r   rÀ   >  s     *z%TestVariousIteratorArgs.test_compressc             C   s^   xXddt ddt dddfD ]:}|  ttt|¡ |  ttt|¡ |  ttt|¡ qW d S )Nri  rm   iè  )rj  g333333ó?iÐ  i  rX   )r   ra   rA   r¥   rÿ   rc  rh  rd  )r   r4   r   r   r   r  G  s     z$TestVariousIteratorArgs.test_productc             C   sº   xŽddt ddt dddfD ]}xVttttttfD ]B}t|d }t||d }tt	t
|||}|  ||¡ q2W |  tt
t|¡ |  tt
t|¡ |  ttt
t|¡ qW d S )	Nri  rm   iè  )rj  g333333ó?iÐ  i  rX   rN   )r   r_  ra  rb  re  rf  r^  r   r   r$   rÔ   rE   ra   rA   rÿ   rc  rh  rd  )r   r4   rà   ZtgtlenZexpectedÚactualr   r   r   rÖ   M  s     z"TestVariousIteratorArgs.test_cyclec             C   sª   x€t dt dt ddt dddfD ]~}x>ttttttfD ]*}|  dd	 t||D t	||¡ q:W |  
ttt|¡ |  
ttt|¡ |  
tt	tt|¡ q$W d S )
Nr/   r   iè  )rW   rÛ   iÐ  i  rX   c             S   s   g | ]\}}|qS r   r   )r8   rš   Zsbr   r   r   r9   [  s    z8TestVariousIteratorArgs.test_groupby.<locals>.<listcomp>)r   r_  ra  rb  re  rf  r^  rE   rª   r   ra   rA   rÿ   rc  rh  rd  )r   r4   rà   r   r   r   rñ   X  s    (*z$TestVariousIteratorArgs.test_groupbyc          	   C   s²   x¬t dt dt ddt dddfD ]}x@ttttttfD ],}|  tt	t
||dd	 ||D ¡ q:W |  tt	t
t|¡ |  tt	t
t|¡ |  ttt	t
t|¡ q$W d S )
Nr/   r   iè  )rW   rÛ   iÐ  i  rX   c             S   s   g | ]}t |r|qS r   )r   )r8   r   r   r   r   r9   d  s    z7TestVariousIteratorArgs.test_filter.<locals>.<listcomp>)r   r_  ra  rb  re  rf  r^  rE   r   rž   r   ra   rA   rÿ   rc  rh  rd  )r   r4   rà   r   r   r   rô   `  s    (z#TestVariousIteratorArgs.test_filterc          	   C   s²   x¬t dt dt ddt dddfD ]}x@ttttttfD ],}|  tt	t
||dd	 ||D ¡ q:W |  tt	t
t|¡ |  tt	t
t|¡ |  ttt	t
t|¡ q$W d S )
Nr/   r   iè  )rW   rÛ   iÐ  i  rX   c             S   s   g | ]}t |r|qS r   )r   )r8   r   r   r   r   r9   m  s    z<TestVariousIteratorArgs.test_filterfalse.<locals>.<listcomp>)r   r_  ra  rb  re  rf  r^  rE   r   rõ   r   ra   rA   rÿ   rc  rh  rd  )r   r4   rà   r   r   r   rö   i  s    (z(TestVariousIteratorArgs.test_filterfalsec          	   C   sÈ   xÂddt ddt dddfD ]€}xdttttttfD ]P}|  tt	||t
||¡ |  tt	||||t
||||¡ q2W |  tt	t|¡ |  tt	t|¡ |  ttt	t|¡ qW d S )Nri  rm   iè  )rj  g333333ó?iÐ  i  rX   )r   r_  ra  rb  re  rf  r^  rE   r   r	   r   ra   rA   rÿ   rc  rh  rd  )r   r4   rà   r   r   r   rù   r  s      0z TestVariousIteratorArgs.test_zipc          
   C   sÐ   xÊddt ddt dddfD ]¬}xlttttttfD ]X}|  tt	||tt
||¡ |  tt	||||tt
||||¡ q2W |  tt	t|¡ |  tt	t|¡ |  ttt	t|¡ qW d S )Nri  rm   iè  )rj  g333333ó?iÐ  i  rX   )r   r_  ra  rb  re  rf  r^  rE   r   r  r	   ra   rA   rÿ   rc  rh  rd  )r   r4   rà   r   r   r   r  {  s     $4z'TestVariousIteratorArgs.test_ziplongestc          
   C   sâ   xÜt dt dt ddt dddfD ]¶}xpttttttfD ]\}|  tt	t
||dd	 ||D ¡ |  tt	tj||||d
d	 ||D ¡ q:W |  tt	t
t|¡ |  tt	t
t|¡ |  ttt	t
t|¡ q$W d S )Nr/   r   rÑ   )rW   rÛ   rÐ   r*  rX   c             S   s   g | ]}t |qS r   )r   )r8   r   r   r   r   r9     s    z4TestVariousIteratorArgs.test_map.<locals>.<listcomp>c             S   s   g | ]}|| qS r   r   )r8   r   r   r   r   r9     s    )r   r_  ra  rb  re  rf  r^  rE   r   r`   r   r'   r"  ra   rA   rÿ   rc  rh  rd  )r   r4   rà   r   r   r   r$    s    (z TestVariousIteratorArgs.test_mapc          
   C   s²   x¬ddt ddt dddfD ]}xHttttttfD ]4}|  tt	||dd d	t||dd d	 ¡ q2W |  
tt	t|d
¡ |  
tt	t|d
¡ |  
ttt	t|d
¡ qW d S )NZ12345rm   iè  )rj  g333333ó?iÐ  i  rX   r   r   r/   )r   r_  ra  rb  re  rf  r^  rE   r   r$   ra   rA   rÿ   rc  rh  rd  )r   r4   rà   r   r   r   r0    s     4z#TestVariousIteratorArgs.test_islicec          	   C   sÄ   xŸt dt dt ddt dddfD ]}xLttttttfD ]8}t||}|  t	t
tj||dd	 ||D ¡ q:W |  tt
tjt|¡ |  tt
tjt|¡ |  tt	t
tjt|¡ q$W d S )
Nr/   r   rÑ   )rW   rÛ   rÐ   r*  rX   c             S   s   g | ]}|| qS r   r   )r8   r   r   r   r   r9     s    z8TestVariousIteratorArgs.test_starmap.<locals>.<listcomp>)r   r_  ra  rb  re  rf  r^  r   rE   r   r&  r'   r"  ra   rA   rÿ   rc  rh  rd  )r   r4   rà   Ússr   r   r   r'    s    (
z$TestVariousIteratorArgs.test_starmapc          	   C   sÎ   xÈt dt dt ddt dddfD ]¢}x\ttttttfD ]H}g }x$||D ]}t|sZP | |¡ qLW |  	t
tt|||¡ q:W |  tttt|¡ |  tttt|¡ |  tt
ttt|¡ q$W d S )Nr/   r   iè  )rW   rÛ   iÐ  i  rX   )r   r_  ra  rb  re  rf  r^  r   rì   rE   r   r1  ra   rA   rÿ   rc  rh  rd  )r   r4   rà   Útgtrï   r   r   r   r2  ¡  s    ( z&TestVariousIteratorArgs.test_takewhilec          	   C   sÒ   xÌt dt dt ddt dddfD ]Š}x`ttttttfD ]L}g }x(||D ]}|s^t|r^qL| |¡ qLW |  	t
tt|||¡ q:W |  tttt|¡ |  tttt|¡ |  tt
ttt|¡ q$W d S )Nr/   r   iè  )rW   rÛ   iÐ  i  rX   )r   r_  ra  rb  re  rf  r^  r   rì   rE   r   r3  ra   rA   rÿ   rc  rh  rd  )r   r4   rà   rm  rï   r   r   r   r4  ­  s    ( z&TestVariousIteratorArgs.test_dropwhilec             C   sÀ   xºddt ddt dddfD ]}xXttttttfD ]D}t||\}}|  t	|t	||¡ |  t	|t	||¡ q2W |  
ttt|¡ |  
ttt|¡ |  
tt	tt|d ¡ qW d S )	Nri  rm   iè  )rj  g333333ó?iÐ  i  rX   r   )r   r_  ra  rb  re  rf  r^  r8  rE   r   ra   rA   rÿ   rc  rh  rd  )r   r4   rà   Zit1Zit2r   r   r   r=  ¹  s     z TestVariousIteratorArgs.test_teeN)r    r!   r"   rg   rp   rÀ   r  rÖ   rñ   rô   rö   rù   r  r$  r0  r'  r2  r4  r=  r   r   r   r   rg  (  s    						
rg  c               @   s   e Zd Zdd Zdd ZdS )ÚLengthTransparencyc             C   sL   |   t td d¡d¡ |   t td d¡d¡ |   t td d¡d¡ d S )Nr*  r   r   )rE   r'   Úlength_hintr€   )r   r   r   r   r   Å  s    zLengthTransparency.test_repeatc             C   sh   |   t td d¡d¡ |   t td d¡d¡ |   t td dd¡d¡ |   t td dd¡d¡ d S )Nr¯   r   r|   )r  )rE   r'   ro  r€   )r   r   r   r   r!  Ê  s    z2LengthTransparency.test_repeat_with_negative_timesN)r    r!   r"   r   r!  r   r   r   r   rn  Ã  s   rn  c               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )ÚRegressionTestsc                s<   dd } fdd}g  |d|d t t}|   |¡ d S )Nc                sT   dgf fdd	}t |}t | |dd< t||}t|gt|   t d S )Nr   c                s   |r|d d =  t  | S )N)rG   )rÌ   Úfirst)rx   Úzr   r   rà   Ù  s    
z@RegressionTests.test_sf_793826.<locals>.mutatingtuple.<locals>.g)r   r`   r	   r   rG   )Ztuple1rx   Ztuple2rà   ÚitemsÚgenr   )rx   rr  r   ÚmutatingtupleÕ  s    
z5RegressionTests.test_sf_793826.<locals>.mutatingtuplec                s   | a tt  d d < d S )N)ÚTr   )r·   )rq  r   r   rx   ä  s    z)RegressionTests.test_sf_793826.<locals>.f)r   r   rN   )r;   rX   rO   )r   rv  rE   )r   ru  rx   Úsecondr   )rq  r   Útest_sf_793826Ò  s    zRegressionTests.test_sf_793826c                s     fdd} fdd}g  |   ttt| |d¡ |   ddg¡ g  |   ttt| |d¡ |   ddg¡ g  |   ttt| ¡ |   ddg¡ d S )	Nc               3   s,      d¡ dV     d¡ t   d¡ d S )Nr   r   r   )rì   ÚAssertionErrorr   )Úhistr   r   Úgen1ó  s
    

z,RegressionTests.test_sf_950057.<locals>.gen1c             3   s      d¡ dV     d¡ d S )NrN   r   r;   )rì   )r   )rz  r   r   Úgen2ú  s    
z,RegressionTests.test_sf_950057.<locals>.gen2Fr   r   T)ra   ry  r   ro   rE   rÔ   )r   r{  r|  r   )rz  r   Útest_sf_950057ï  s    zRegressionTests.test_sf_950057c          	   C   s:   t  dd tdD ¡}|  t¡ t| W d Q R X d S )Nc             s   s   | ]
}d V  qdS )r   Nr   )r8   Zunusedr   r   r   r     s    zERegressionTests.test_long_chain_of_empty_iterables.<locals>.<genexpr>i )ro   rq   r   ra   r   rG   )r   rB   r   r   r   Ú"test_long_chain_of_empty_iterables  s    z2RegressionTests.test_long_chain_of_empty_iterablesc                s4    fdd}x"t td|D ]\} t  qW d S )Nc                s   | dkrt   | dkS )NrX   rO   )r   )r   )rK   r   r   rx     s    z,RegressionTests.test_issue30347_1.<locals>.fr/   )rª   r   r   )r   rx   rš   r   )rK   r   Útest_issue30347_1  s    z!RegressionTests.test_issue30347_1c                sN   G  fddd}dt ttd|d  xtdD ]}t  d  q8W d S )Nc                   s"   e Zd Zdd Z fddZdS )z,RegressionTests.test_issue30347_2.<locals>.Kc             S   s   d S )Nr   )r   r©   r   r   r   r
    s    z5RegressionTests.test_issue30347_2.<locals>.K.__init__c                s   d7 dkrt  d  dS )Nr   T)rG   )r   Úother)rà   r   r   r   rç     s    
z3RegressionTests.test_issue30347_2.<locals>.K.__eq__N)r    r!   r"   r
  rç   r   )rà   r   r   r   ÚK  s   r  r   r/   r   r   )rG   rª   r   )r   r  r   r   )rà   r   r   Útest_issue30347_2  s
    	z!RegressionTests.test_issue30347_2N)r    r!   r"   rx  r}  r~  r  r  r   r   r   r   rp  Ð  s
   rp  c               @   s   e Zd Zdd ZdS )ÚSubclassWithKwargsTestc                s   x|t ttttttttt	t
tfD ]\ G  fddd }y|dd W q tk
rx } z|  d|jd ¡ W d d }~X Y qX qW d S )Nc                   s   e Zd Zd fdd	ZdS )zBSubclassWithKwargsTest.test_keywords_in_subclass.<locals>.SubclassNc                s    j | f|  d S )N)r
  )r   Únewargr
   )Úclsr   r   r
  1  s    zKSubclassWithKwargsTest.test_keywords_in_subclass.<locals>.Subclass.__init__)N)r    r!   r"   r
  r   )r  r   r   ÚSubclass0  s   r  r   )r  zkeyword argumentsr   )r€   r	   rž   rõ   ro   r`   r&  r$   r1  r3  rÔ   r¿   rA   ZassertNotInr
   )r   r  Úerrr   )r  r   Útest_keywords_in_subclass,  s    z0SubclassWithKwargsTest.test_keywords_in_subclassN)r    r!   r"   r  r   r   r   r   r  +  s   r  c               @   s:   e Zd Zdd ZejZdd Zdd Zdd Zd	d
 Z	dS )Ú
SizeofTestc             C   s   t  d¡| _d S )Nr   )ÚstructÚcalcsizeÚssize_t)r   r   r   r   ÚsetUp;  s    zSizeofTest.setUpc             C   sF   t  d¡}| j}|tdd|d| j   |td |d| j   d S )NZ3PirV   Z12r   )
rU   rU   rU   rU   rU   rU   rU   rU   rU   rU   r/   )r   ÚcalcobjsizeÚcheck_sizeofr¥   r  )r   ÚbasesizeÚcheckr   r   r   Útest_product_sizeof@  s    
zSizeofTest.test_product_sizeofc             C   sL   t  d¡}| j}|tdd|d| j   |ttdd|d| j   d S )NÚ3Pnirn   rN   r/   r;   )r   r  r  r   r  r   )r   r  r  r   r   r   Útest_combinations_sizeofF  s    
z#SizeofTest.test_combinations_sizeofc             C   sP   t }t d¡}| j}||dd|d| j   ||tdd|d| j   d S )Nr  rn   rN   r/   r;   )r   r   r  r  r  r   )r   r«   r  r  r   r   r   Ú)test_combinations_with_replacement_sizeofL  s
    
z4SizeofTest.test_combinations_with_replacement_sizeofc             C   sŠ   t  d¡}| j}|td|d| j  d| j   |tdd|d| j  d| j   |tdd|d| j  d| j   |ttdd|d| j  d| j   d S )NZ4Pnirn   r;   rN   r   rX   r/   )r   r  r  r   r  r   )r   r  r  r   r   r   Útest_permutations_sizeofS  s    


z#SizeofTest.test_permutations_sizeofN)
r    r!   r"   r  r   r  r  r  r  r  r   r   r   r   r  9  s   r  a"   Doctest for examples in the library reference: libitertools.tex


>>> amounts = [120.15, 764.05, 823.14]
>>> for checknum, amount in zip(count(1200), amounts):
...     print('Check %d is for $%.2f' % (checknum, amount))
...
Check 1200 is for $120.15
Check 1201 is for $764.05
Check 1202 is for $823.14

>>> import operator
>>> for cube in map(operator.pow, range(1,4), repeat(3)):
...    print(cube)
...
1
8
27

>>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele']
>>> for name in islice(reportlines, 3, None, 2):
...    print(name.title())
...
Alex
Laura
Martin
Walter
Samuele

>>> from operator import itemgetter
>>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3)
>>> di = sorted(sorted(d.items()), key=itemgetter(1))
>>> for k, g in groupby(di, itemgetter(1)):
...     print(k, list(map(itemgetter(0), g)))
...
1 ['a', 'c', 'e']
2 ['b', 'd', 'f']
3 ['g']

# Find runs of consecutive numbers using groupby.  The key to the solution
# is differencing with a range so that consecutive numbers all appear in
# same group.
>>> data = [ 1,  4,5,6, 10, 15,16,17,18, 22, 25,26,27,28]
>>> for k, g in groupby(enumerate(data), lambda t:t[0]-t[1]):
...     print(list(map(operator.itemgetter(1), g)))
...
[1]
[4, 5, 6]
[10]
[15, 16, 17, 18]
[22]
[25, 26, 27, 28]

>>> def take(n, iterable):
...     "Return first n items of the iterable as a list"
...     return list(islice(iterable, n))

>>> def prepend(value, iterator):
...     "Prepend a single value in front of an iterator"
...     # prepend(1, [2, 3, 4]) -> 1 2 3 4
...     return chain([value], iterator)

>>> def enumerate(iterable, start=0):
...     return zip(count(start), iterable)

>>> def tabulate(function, start=0):
...     "Return function(0), function(1), ..."
...     return map(function, count(start))

>>> import collections
>>> def consume(iterator, n=None):
...     "Advance the iterator n-steps ahead. If n is None, consume entirely."
...     # Use functions that consume iterators at C speed.
...     if n is None:
...         # feed the entire iterator into a zero-length deque
...         collections.deque(iterator, maxlen=0)
...     else:
...         # advance to the empty slice starting at position n
...         next(islice(iterator, n, n), None)

>>> def nth(iterable, n, default=None):
...     "Returns the nth item or a default value"
...     return next(islice(iterable, n, None), default)

>>> def all_equal(iterable):
...     "Returns True if all the elements are equal to each other"
...     g = groupby(iterable)
...     return next(g, True) and not next(g, False)

>>> def quantify(iterable, pred=bool):
...     "Count how many times the predicate is true"
...     return sum(map(pred, iterable))

>>> def padnone(iterable):
...     "Returns the sequence elements and then returns None indefinitely"
...     return chain(iterable, repeat(None))

>>> def ncycles(iterable, n):
...     "Returns the sequence elements n times"
...     return chain(*repeat(iterable, n))

>>> def dotproduct(vec1, vec2):
...     return sum(map(operator.mul, vec1, vec2))

>>> def flatten(listOfLists):
...     return list(chain.from_iterable(listOfLists))

>>> def repeatfunc(func, times=None, *args):
...     "Repeat calls to func with specified arguments."
...     "   Example:  repeatfunc(random.random)"
...     if times is None:
...         return starmap(func, repeat(args))
...     else:
...         return starmap(func, repeat(args, times))

>>> def pairwise(iterable):
...     "s -> (s0,s1), (s1,s2), (s2, s3), ..."
...     a, b = tee(iterable)
...     try:
...         next(b)
...     except StopIteration:
...         pass
...     return zip(a, b)

>>> def grouper(n, iterable, fillvalue=None):
...     "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
...     args = [iter(iterable)] * n
...     return zip_longest(*args, fillvalue=fillvalue)

>>> def roundrobin(*iterables):
...     "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
...     # Recipe credited to George Sakkis
...     pending = len(iterables)
...     nexts = cycle(iter(it).__next__ for it in iterables)
...     while pending:
...         try:
...             for next in nexts:
...                 yield next()
...         except StopIteration:
...             pending -= 1
...             nexts = cycle(islice(nexts, pending))

>>> def powerset(iterable):
...     "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
...     s = list(iterable)
...     return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))

>>> def unique_everseen(iterable, key=None):
...     "List unique elements, preserving order. Remember all elements ever seen."
...     # unique_everseen('AAAABBBCCDAABBB') --> A B C D
...     # unique_everseen('ABBCcAD', str.lower) --> A B C D
...     seen = set()
...     seen_add = seen.add
...     if key is None:
...         for element in iterable:
...             if element not in seen:
...                 seen_add(element)
...                 yield element
...     else:
...         for element in iterable:
...             k = key(element)
...             if k not in seen:
...                 seen_add(k)
...                 yield element

>>> def unique_justseen(iterable, key=None):
...     "List unique elements, preserving order. Remember only the element just seen."
...     # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B
...     # unique_justseen('ABBCcAD', str.lower) --> A B C A D
...     return map(next, map(itemgetter(1), groupby(iterable, key)))

>>> def first_true(iterable, default=False, pred=None):
...     '''Returns the first true value in the iterable.
...
...     If no true value is found, returns *default*
...
...     If *pred* is not None, returns the first item
...     for which pred(item) is true.
...
...     '''
...     # first_true([a,b,c], x) --> a or b or c or x
...     # first_true([a,b], x, f) --> a if f(a) else b if f(b) else x
...     return next(filter(pred, iterable), default)

>>> def nth_combination(iterable, r, index):
...     'Equivalent to list(combinations(iterable, r))[index]'
...     pool = tuple(iterable)
...     n = len(pool)
...     if r < 0 or r > n:
...         raise ValueError
...     c = 1
...     k = min(r, n-r)
...     for i in range(1, k+1):
...         c = c * (n - k + i) // i
...     if index < 0:
...         index += c
...     if index < 0 or index >= c:
...         raise IndexError
...     result = []
...     while r:
...         c, n, r = c*r//n, n-1, r-1
...         while index >= c:
...             index -= c
...             c, n = c*(n-r)//n, n-1
...         result.append(pool[-1-n])
...     return tuple(result)


This is not part of the examples but it tests to make sure the definitions
perform as purported.

>>> take(10, count())
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> list(prepend(1, [2, 3, 4]))
[1, 2, 3, 4]

>>> list(enumerate('abc'))
[(0, 'a'), (1, 'b'), (2, 'c')]

>>> list(islice(tabulate(lambda x: 2*x), 4))
[0, 2, 4, 6]

>>> it = iter(range(10))
>>> consume(it, 3)
>>> next(it)
3
>>> consume(it)
>>> next(it, 'Done')
'Done'

>>> nth('abcde', 3)
'd'

>>> nth('abcde', 9) is None
True

>>> [all_equal(s) for s in ('', 'A', 'AAAA', 'AAAB', 'AAABA')]
[True, True, True, False, False]

>>> quantify(range(99), lambda x: x%2==0)
50

>>> a = [[1, 2, 3], [4, 5, 6]]
>>> flatten(a)
[1, 2, 3, 4, 5, 6]

>>> list(repeatfunc(pow, 5, 2, 3))
[8, 8, 8, 8, 8]

>>> import random
>>> take(5, map(int, repeatfunc(random.random)))
[0, 0, 0, 0, 0]

>>> list(pairwise('abcd'))
[('a', 'b'), ('b', 'c'), ('c', 'd')]

>>> list(pairwise([]))
[]

>>> list(pairwise('a'))
[]

>>> list(islice(padnone('abc'), 0, 6))
['a', 'b', 'c', None, None, None]

>>> list(ncycles('abc', 3))
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']

>>> dotproduct([1,2,3], [4,5,6])
32

>>> list(grouper(3, 'abcdefg', 'x'))
[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')]

>>> list(roundrobin('abc', 'd', 'ef'))
['a', 'd', 'e', 'b', 'f', 'c']

>>> list(powerset([1,2,3]))
[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]

>>> all(len(list(powerset(range(n)))) == 2**n for n in range(18))
True

>>> list(powerset('abcde')) == sorted(sorted(set(powerset('abcde'))), key=len)
True

>>> list(unique_everseen('AAAABBBCCDAABBB'))
['A', 'B', 'C', 'D']

>>> list(unique_everseen('ABBCcAD', str.lower))
['A', 'B', 'C', 'D']

>>> list(unique_justseen('AAAABBBCCDAABBB'))
['A', 'B', 'C', 'D', 'A', 'B']

>>> list(unique_justseen('ABBCcAD', str.lower))
['A', 'B', 'C', 'A', 'D']

>>> first_true('ABC0DEF1', '9', str.isdigit)
'0'

>>> population = 'ABCDEFGH'
>>> for r in range(len(population) + 1):
...     seq = list(combinations(population, r))
...     for i in range(len(seq)):
...         assert nth_combination(population, r, i) == seq[i]
...     for i in range(-len(seq), 0):
...         assert nth_combination(population, r, i) == seq[i]


Ú
libreftestc          	   C   s   t ttttttttf	}t	j
|  | r|ttdr|dd l}d gd }x2tt|D ]"}t	j
|  | ¡  t ¡ ||< qNW t| t	 tjt | ¡ d S )NÚgettotalrefcountr   rX   )r:   rg  rU  rp  rn  r  rA  rP  r  r   Zrun_unittestÚhasattrrÆ   Úgcr   r   Zcollectr  r  Zrun_doctestÚmodulesr    )ÚverboseZtest_classesr  Zcountsr   r   r   r   Ú	test_main	  s    


r  Ú__main__T)r  )N)>ZunittestZtestr   Ú	itertoolsr.  Údecimalr   Z	fractionsr   r'   r  rs   r1   Ú	functoolsr   rÆ   r  ZMAX_Py_ssize_trÃ   Zminsizer   r   r   r   r   r   r   r   r   r&   r*   r+   r-   r.   r0   r   re   ru   ZTestCaser:   rA  rP  rU  r^  r_  ra  rb  rÿ   rc  rd  re  rf  rg  rn  rp  r  Zcpython_onlyr  r  Z__test__r  r    r   r   r   r   Ú<module>   s   
           n*e	
	 [
  _

