B
    18\å ã               @   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   úW/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Lib/test/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   ÚprotocolrB   rD   r&   ÚcompareÚdumpÚ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)   ÚabcrL   Úabé   é   )r   r   é   é	   r;   é   é   iÐ  i°  )rE   r   Ú
accumulater   ÚintÚcomplexr   r   ÚmapÚassertRaisesrA   ÚminÚmaxr'   r(   Úchrr1   ÚHIGHEST_PROTOCOLrO   )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>.chain2rW   ÚdefÚabcdefÚ r;   Úabcdr   rP   )ÚchainrE   r   r&   rc   rA   )r   rl   rN   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 )	NrW   rm   rn   ro   r;   rp   r   rP   )rE   r   rq   Úfrom_iterabler&   rc   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 )NrW   rm   rn   rL   Zbcdefro   r;   rp   r   rP   r   )rI   )ÚcopyÚdeepcopyÚpicklecopiersrq   rE   r   rG   r&   rc   rA   r   r1   rg   rO   )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   rW   rm   rL   rM   rN   Údr<   ÚfZghi)rc   rA   rq   Ú__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 )"NrW   r   r   éþÿÿÿc             S   s   | S )Nr   )rL   r   r   r   r6   Í   r7   z0TestBasicOps.test_combinations.<locals>.<lambda>é    ÚABCD)ÚAÚB)r   ÚC)r   ÚD)r   r   )r   r   )r   r   r;   rP   )r   r   r   )r   r   rP   )r   r   rP   )r   r   rP   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>.combinations3rY   c             S   s   g | ]}d | d qS )rZ   é   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<   )rN   r   r   r9     s    )rc   rA   Úcombinationsr   rw   rE   r   rG   r   r   r+   r   r   Ú
assertTrueÚallr1   rg   rO   )
r   ÚopÚtestIntermediater   r   r   r   r,   Úresultr5   r   )rN   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    )rc   Ú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ÚabcderP   r   )rE   r   r   rb   Ú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 )NrW   r   r   r~   c             S   s   | S )Nr   )rL   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>.numcombsrY   c             S   s   g | ]}d | d qS )rZ   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<   )rN   r   r   r9   g  s    )r   rc   rA   r   rw   rE   r   rG   r   r   r   r   r   r   Úgroupbyr   r1   rg   rO   )r   Úcwrr   r   r¥   rš   r©   r   r,   r   Zregular_combsZnorunsr5   r   )rN   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   @)rc   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    rP   r   )r   rE   r   r   rb   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 )NrW   r   r   r~   r   r4   rP   )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>.permutations2rY   c             S   s   g | ]}d | d qS )rZ   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)rc   rA   r   r   rE   r   r   r   r+   r   r   r   r   r1   rg   rO   )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   @)rc   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    rP   r   )rE   r   r   rb   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 )NrQ   ÚABCDEFGr[   )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ÚACEFro   ZACr€   ZBCi'  rQ   )r   r   rP   rZ   c             S   s
   t   | ¡S )N)ru   )rL   r   r   r   r6   õ  r7   z,TestBasicOps.test_compress.<locals>.<lambda>c             S   s
   t  | ¡S )N)ru   rv   )rL   r   r   r   r6   õ  r7   ZCEFZBCDEFr   )rE   r   Úcompressrq   rs   rŠ   r   rc   rA   rw   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 ),NrW   )rL   r   )rM   r   )rN   r   rP   )rL   rP   )rM   r;   )rN   rZ   r   r±   )rL   r±   )rM   r   éýÿÿÿ)rL   rÃ   )rM   r~   r;   rL   r/   rZ   g      
@g      @g      @y      
@      Ày      @      Ày      @      Àz1.1z2.1z3.1r[   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	   rc   rA   Úmaxsizer   r   r   r   ÚreprrG   rF   ÚfloatÚsysÚ__mod__ru   rv   r1   rg   rO   Úexc_info)r   ÚBIGINTrN   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 );NrW   r   rP   )rL   r   )rM   rZ   )rN   r[   )ÚstartÚstepr±   )rÑ   )rL   r   )rM   r±   )rN   r~   rL   rM   r   )rM   r   )rN   r   r   )rM   rP   )rN   r;   é   rR   éd   r/   rZ   g      ô?g      
@g      @y      
@      Ày      @      Ày      !@       Àz1.1z.1z1.2z1.3rY   é   rS   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Ä   rc   rA   r&   rÅ   r   r   r   r   rÆ   rG   rF   r`   rÇ   rÈ   r1   rg   rO   )r   rË   rN   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/   rW   Z
abcabcabcaro   rZ   r   r   r   rL   Z
bcabcabcabZ
cabcabcabcr    c                s   g | ]}t  qS r   )rG   )r8   r   )rN   r   r   r9     s    z+TestBasicOps.test_cycle.<locals>.<listcomp>rÒ   Zcdeabcdeabcdeabcdeabc                s   g | ]}t  qS r   )rG   )r8   r   )rN   r   r   r9     s    rY   )rE   r&   Úcycler   rc   rA   r$   r   rG   ru   rv   r   r1   rg   r2   r3   rO   r|   )r   r5   rB   Ú_rŽ   ry   r   )rN   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ÚdefgrW   r   rÒ   ZdefgabcdefgabcdefgabÚabcdefgr   r   ro   r   )rÖ   r{   r   rE   r&   rc   rA   r   )r   rN   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)ÚkeyrW   c             S   s   | S )Nr   )r   r   r   r   r6   Á  r7   z+TestBasicOps.test_groupby.<locals>.<lambda>r/   )r   r/   rÒ   )r   é   rS   )r   r   rS   )r   é   rS   )r   é   é   )r   rR   rà   )rP   r]   é   )rP   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	AABBBAAAAr\   )r   rZ   Zabracadabrac             S   s   g | ]\}}|qS r   r   )r8   rª   râ   r   r   r   r9   	  s    rL   rM   rN   ry   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    )rZ   rL   )r   rM   )r   rN   )r   ry   )r   r,   c             S   s    g | ]\}}t t||fqS r   )r   r   )r8   rª   râ   r   r   r   r9     s    T)ÚreverserP   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¡   rc   rA   Úappendr   r1   rg   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 )NrQ   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   rY   rP   )rE   r   rº   r   r   Úboolr&   rÄ   rc   rA   rG   ru   rv   r1   rg   r2   r3   rO   )r   ÚansrN   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 )NrQ   r   rP   rZ   r   r   r;   rY   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Ä   rc   rA   rG   r1   rg   rO   )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>rW   )rL   r   )rM   r   )rN   r   rQ   rn   rP   c             S   s   g | ]}t t|qS r   )r   r   )r8   Úpairr   r   r   r9   p  s    rm   c             S   s   g | ]}|qS r   r   )r8   rú   r   r   r   r9   r  s    )	r	   rÄ   rE   r   r   r   r&   rc   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 )NrW   rm   c             S   s   g | ]\}}||fqS r   r   )r8   r   rù   r   r   r   r9   }  s    z5TestBasicOps.test_zip_tuple_reuse.<locals>.<listcomp>)rL   r   )rM   r   )rN   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   rb   r¡   r	   rE   rd   re   r   ÚdictÚfromkeysru   rÄ   rv   r   r1   rg   r2   r3   rG   rO   )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 )NrW   rQ   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  )Ú	fillvaluerP   rn   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    rm   c             S   s   g | ]}|qS r   r   )r8   rú   r   r   r   r9   ·  s    )rW   rÙ   )r   re   rb   r   rE   r   Úzip_longestrü   r&   rÄ   r	   rc   rA   ÚevalÚglobalsÚlocalsÚ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 )NrW   rm   )
r   rb   r¡   r  rE   rd   re   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   rW   rm   Zdefgh)r  ro   )r   r1   rg   rO   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   rP   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   Ú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   rc   )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   rX   )rL   )rM   r   rP   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   r   r;   )rŠ   rY   rQ   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   rb   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   rb   r   r  )r
   r  r  r   r*   r   )r   r   Úproduct2  s    

z+TestBasicOps.test_product.<locals>.product2ro   rW   r   )rL   rM   rN   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>rZ   )r   rE   r   r§   rü   r   rc   rA   r   r   r  Ú	randranger*   rb   r|   )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 )NrX   r   rŠ   i   )rc   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 )NrW   rm   r   )rE   r   r   rb   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   rX   )rL   )rM   r   rP   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   r   r   )	r   rE   r   ru   r§   rv   r1   rg   rO   )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   )rP   )r   i   )r   rP   r   )r   r   i   )r§   r{   rE   rG   rc   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 )NrL   rP   )ÚobjectÚtimes)r   rL   )r   rL   )r   rL   r   rÃ   r;   y      ð?        zrepeat((1+0j))rZ   zrepeat((1+0j), 5)zrepeat((1+0j), 0)r/   r   Zaar   )rE   r   rŠ   r   r   r&   rc   rA   rÆ   rG   ru   rv   r1   rg   rO   )r   r,   rN   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 )NrL   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 )NrP   r   rY   r   r[   rW   rZ   )rL   r   )rM   r   )rN   r   r   r/   r;   )rE   r   rb   r'   Úpowr   r   rÄ   r&   rc   rA   ÚnegrG   r   r   r   ru   rv   r1   rg   rO   )r   rõ   rN   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 )NrP   r   rY   r   r[   r;   rZ   i   )r;   rZ   Úextrar/   )rE   r   Ústarmapr'   r'  r	   r   r&   rÄ   r|   rc   rA   rG   r   r   r   ru   rv   r1   rg   rO   )r   rõ   rN   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Ò   rP   )r/   rP   rÒ   )r/   rÒ   )r/   r/   )r/   rP   )rÒ   rÓ   )))r/   én   rP   )r/   rÓ   rP   ))r/   r-  )r/   rÓ   ))r-  )rÓ   r/   r   r   rP   r;   éûÿÿÿr±   r   rL   é2   ))r/   rÒ   rP   )r/   rP   rÒ   )r/   rÒ   )r/   rP   )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   r0  r   r   r   r  ú  s    z2TestBasicOps.test_islice.<locals>.IntLike.__init__c             S   s   | j S )N)r0  )r   r   r   r   Ú	__index__ü  s    z3TestBasicOps.test_islice.<locals>.IntLike.__index__N)r    r!   r"   r  r1  r   r   r   r   ÚIntLikeù  s   r2  rZ   )rE   r   r$   r   r|   rc   rA   r   r   rÄ   rÅ   rG   ru   rv   r1   rg   rO   ÚweakrefÚrefÚassertIsNotNoner   Ú
gc_collectÚassertIsNoner#  )	r   r
   ZtgtargsrB   ZrarN   r5   Úwrr2  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   rP   rZ   rÒ   r   r;   rQ   r[   )r;   rZ   r*  r/   r   )rE   r   Ú	takewhiler0   rc   rA   r'   r'  rG   r   r   rô   r   ru   rv   r   r1   rg   rO   )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   rP   rZ   rÒ   r   r;   rQ   r[   )r;   rZ   r*  r/   )rE   r   Ú	dropwhiler0   rc   rA   r'   r'  rG   r   r   ru   rv   r   r1   rg   rO   )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Ó   rZ   r   r   rP   r   r   rW   rm   iÐ  Ú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'  é<   )rI   )ÚteerE   r   r   r   r   rG   r  Úshufflerî   rc   rA   rF   r   r   r   r   r3  ÚproxyÚgetattrÚReferenceErrorru   r&   rv   r1   rg   rO   r|   )r   r   rL   rM   r   r   ÚorderZlistsZitsrÎ   rN   r   ry   Út1Út2ZtnewÚ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)rB  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   )rc   r   rG   r	   rq   rÖ   r¬   r   r$   rB  rŠ   rº   r÷   rb   r:  r<  r+  )r   rz   rŽ   Úqr   r   r   Útest_StopIterationÇ  s     zTestBasicOps.test_StopIteration)r;   r   N)3r    r!   r"   rO   ri   rr   rt   rx   r}   r   r   Úbigaddrspacetestr   Ú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,  r9  r;  r=  rK  rL  rN  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   rP   r;   rZ   rQ   r/   rR   )rE   r   r_   )r   r   r   r   ri   à  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   rP   r;   rZ   rQ   r/   rR   )r   r1   rg   r_   rE   r   r2   r3   rG   ru   rv   )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   rg   r2   r3   r   ru   rv   )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 )Nro   r€   ÚDEFrœ   )rE   Újoinrq   )r   r   r   r   rr   ü  s    zTestExamples.test_chainc             C   s    |   d t ddg¡¡d¡ d S )Nro   r€   rU  rœ   )rE   rV  rq   rs   )r   r   r   r   rt   ÿ  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;   rP   )r   r   r   )r   r   rP   )r   r   rP   )r   r   rP   )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/   rZ   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 )NrZ   r   )r   r   r   r   r6     r7   z-TestExamples.test_dropwhile.<locals>.<lambda>r   r;   rQ   )rE   r   r<  )r   r   r   r   r=    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BBBÚ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   rP   rZ   rY   r\   )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;   rQ   r[   )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   rP   r/   )rZ   r   rP   r   r\   iè  )rE   r   rb   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   r9  '  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   r\  ú-)r  )r   r   )r   rù   )r   r]  )r   r]  )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 DCrP   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )rE   r   r   rb   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   r\  zAx Ay Bx By Cx Cy Dx Dyr   rP   )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§   rb   r   r_  r   )r   r   r   r   r  :  s
    zTestExamples.test_productc             C   s    |   ttdddddg¡ d S )Nr/   rP   )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   rZ   )rP   r   )r/   rP   r   r\   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 )NrZ   r   )r   r   r   r   r6   I  r7   z-TestExamples.test_takewhile.<locals>.<lambda>r   r;   rQ   )rE   r   r:  )r   r   r   r   r;  H  s    zTestExamples.test_takewhileN)r    r!   r"   ri   rR  rT  rr   rt   r   r®   rÂ   rÏ   rØ   r=  ró   rö   rø   r)  r9  rû   r^  rµ   r  r%  r`  r;  r   r   r   r   rQ  Þ  s.   
rQ  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Ñ   r|   r   rG   r   r	   Ú	enumerate)
r)   r
   r4   rÐ   rD   rÑ   rB   Znextir   rk   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   rX  r;   rY  rZ  r   r[  r/   rP   r   r/  )rE   r   r$   r|   r   rÄ   rG   )r   rB   rN   r   r   r   Útest_islice_reciped  s      z1TestPurePythonRoughEquivalents.test_islice_recipeN)r    r!   r"   Ústaticmethodr$   rd  r   r   r   r   ra  L  s   ra  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   rP   )ri  r_   )r   rL   r   r   r   ri   }  s    zTestGC.test_accumulatec             C   s   g }|   t||¡ d S )N)ri  rq   )r   rL   r   r   r   rr     s    zTestGC.test_chainc             C   s   g }|   t |g¡|¡ d S )N)ri  rq   rs   )r   rL   r   r   r   rt     s    zTestGC.test_chain_from_iterablec             C   s"   g }|   tdd|dgd|¡ d S )Nr   r   rP   )ri  r   )r   rL   r   r   r   r     s    zTestGC.test_combinationsc             C   s"   g }|   tdd|dgd|¡ d S )Nr   r   rP   )ri  r   )r   rL   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   )ri  rÁ   )r   rL   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ü   ri  rÄ   )r   rL   rj  r   r   r   rÏ     s    zTestGC.test_countc             C   s   g }|   t|gd |¡ d S )Nr   )ri  rÖ   )r   rL   r   r   r   rØ     s    zTestGC.test_cyclec             C   s    g }|   ttd||g|¡ d S )Nr   )ri  r<  rô   )r   rL   r   r   r   r=    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>)ri  r¬   )r   rL   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   )ri  rº   )r   rL   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>)ri  r÷   )r   rL   r   r   r   rø   ±  s    zTestGC.test_filterfalsec             C   s&   g }|   t|gd |gd |¡ d S )Nr   rP   )ri  r	   )r   rL   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   rP   )r  )ri  r  )r   rL   rM   r   r   r   r^  ¹  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   )ri  rb   )r   rL   r   r   r   r)  ¿  s    zTestGC.test_mapc             C   s    g }|   t|gd d |¡ d S )Nr   )ri  r$   )r   rL   r   r   r   r9  Ã  s    zTestGC.test_islicec             C   s"   g }|   tdd|dgd|¡ d S )Nr   r   rP   )ri  r   )r   rL   r   r   r   rµ   Ç  s    zTestGC.test_permutationsc             C   s$   g }|   tdd|dgdd|¡ d S )Nr   r   rP   )rŠ   )ri  r§   )r   rL   r   r   r   r  Ë  s    zTestGC.test_productc             C   s   g }|   t||¡ d S )N)ri  rŠ   )r   rL   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   )ri  r+  )r   rL   r   r   r   r,  Ó  s    zTestGC.test_starmapc             C   s"   g }|   ttdd||g|¡ d S )Nr   r   )ri  r:  rô   )r   rL   r   r   r   r;  ×  s    zTestGC.test_takewhileN)r    r!   r"   ri  ri   rr   rt   r   r®   rÂ   rÏ   rØ   r=  ró   rm  rö   rø   rû   r^  r)  r9  rµ   r  r%  r,  r;  r   r   r   r   rf  v  s.   rf  c             c   s   x| D ]
}|V  qW dS )zRegular generatorNr   )Úseqnr   r   r   r   ÚRÛ  s    
ro  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)rn  )r   rn  r   r   r   r  â  s    z
G.__init__c             C   s
   | j | S )N)rn  )r   r   r   r   r   Ú__getitem__ä  s    zG.__getitem__N)r    r!   r"   r#   r  rq  r   r   r   r   rp  à  s   rp  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   )rn  r   )r   rn  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   rn  r   )r   r«   r   r   r   r   î  s
     z
I.__next__N)r    r!   r"   r#   r  r   r   r   r   r   r   rr  ç  s   rr  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   )rn  r   )r   rn  r   r   r   r  ö  s    zIg.__init__c             c   s   x| j D ]
}|V  qW d S )N)rn  )r   r0  r   r   r   r   ù  s    zIg.__iter__N)r    r!   r"   r#   r  r   r   r   r   r   rs  ô  s   rs  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   )rn  r   )r   rn  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   rn  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   )rn  r   )r   rn  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   rt    s   rt  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   )rn  r   )r   rn  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 )NrP   r   r   )r   r   r   r   r     s    z
E.__next__N)r    r!   r"   r#   r  r   r   r   r   r   r   ru    s   ru  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   rn  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   rv    s   rv  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>)rq   rb   ro  rs  rp  )rn  r   r   r   ÚL#  s    rw  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   rP   r;   rZ   rQ   r/   rR   )r   rp  rr  rs  rw  ro  rE   r   r_   rv  rc   rA   r  rt  ÚZeroDivisionErrorru  )r   r4   r,   r   râ   r   r   r   ri   *  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Ú123ro   iè  )Údog333333ó?iÐ  i  rZ   )r   rp  rr  rs  rv  rw  ro  rE   r   rq   rc   rA   r  rt  ry  ru  )r   r4   râ   r   r   r   rr   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 )	Nrz  ro   iè  )r{  g333333ó?iÐ  i  rZ   r   )r   r   rp  rr  rs  rv  rw  ro  rE   r   rÁ   rŠ   rc   rA   r  rt  ry  ru  )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 )Nrz  ro   iè  )r{  g333333ó?iÐ  i  rZ   )r   rc   rA   r§   r  rt  ry  ru  )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 )	Nrz  ro   iè  )r{  g333333ó?iÐ  i  rZ   rP   )r   rp  rr  rs  rv  rw  ro  r   r   r$   rÖ   rE   rc   rA   r  rt  ry  ru  )r   r4   râ   ZtgtlenÚ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è  )rY   rÝ   iÐ  i  rZ   c             S   s   g | ]\}}|qS r   r   )r8   rª   Zsbr   r   r   r9   [  s    z8TestVariousIteratorArgs.test_groupby.<locals>.<listcomp>)r   rp  rr  rs  rv  rw  ro  rE   r¬   r   rc   rA   r  rt  ry  ru  )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è  )rY   rÝ   iÐ  i  rZ   c             S   s   g | ]}t |r|qS r   )r   )r8   r   r   r   r   r9   d  s    z7TestVariousIteratorArgs.test_filter.<locals>.<listcomp>)r   rp  rr  rs  rv  rw  ro  rE   r   rº   r   rc   rA   r  rt  ry  ru  )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è  )rY   rÝ   iÐ  i  rZ   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   rp  rr  rs  rv  rw  ro  rE   r   r÷   r   rc   rA   r  rt  ry  ru  )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 )Nrz  ro   iè  )r{  g333333ó?iÐ  i  rZ   )r   rp  rr  rs  rv  rw  ro  rE   r   r	   r   rc   rA   r  rt  ry  ru  )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 )Nrz  ro   iè  )r{  g333333ó?iÐ  i  rZ   )r   rp  rr  rs  rv  rw  ro  rE   r   r  r	   rc   rA   r  rt  ry  ru  )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Ó   )rY   rÝ   rÒ   r/  rZ   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   rp  rr  rs  rv  rw  ro  rE   r   rb   r   r'   r'  rc   rA   r  rt  ry  ru  )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12345ro   iè  )r{  g333333ó?iÐ  i  rZ   r   r   r/   )r   rp  rr  rs  rv  rw  ro  rE   r   r$   rc   rA   r  rt  ry  ru  )r   r4   râ   r   r   r   r9    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Ó   )rY   rÝ   rÒ   r/  rZ   c             S   s   g | ]}|| qS r   r   )r8   r   r   r   r   r9     s    z8TestVariousIteratorArgs.test_starmap.<locals>.<listcomp>)r   rp  rr  rs  rv  rw  ro  r   rE   r   r+  r'   r'  rc   rA   r  rt  ry  ru  )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è  )rY   rÝ   iÐ  i  rZ   )r   rp  rr  rs  rv  rw  ro  r   rî   rE   r   r:  rc   rA   r  rt  ry  ru  )r   r4   râ   Útgtrñ   r   r   r   r;  ¡  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è  )rY   rÝ   iÐ  i  rZ   )r   rp  rr  rs  rv  rw  ro  r   rî   rE   r   r<  rc   rA   r  rt  ry  ru  )r   r4   râ   r  rñ   r   r   r   r=  ­  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 )	Nrz  ro   iè  )r{  g333333ó?iÐ  i  rZ   r   )r   rp  rr  rs  rv  rw  ro  rB  rE   r   rc   rA   r  rt  ry  ru  )r   r4   râ   Zit1Zit2r   r   r   rK  ¹  s     z TestVariousIteratorArgs.test_teeN)r    r!   r"   ri   rr   rÂ   r  rØ   ró   rö   rø   rû   r	  r)  r9  r,  r;  r=  rK  r   r   r   r   rx  (  s    						
rx  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'   r  rŠ   )r   r   r   r   r&  Ê  s    z2LengthTransparency.test_repeat_with_negative_timesN)r    r!   r"   r%  r&  r   r   r   r   r  Ã  s   r  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)rz   Úzr   r   râ   Ù  s    
z@RegressionTests.test_sf_793826.<locals>.mutatingtuple.<locals>.g)r   rb   r	   r   rG   )Útuple1rz   Útuple2râ   ÚitemsÚgenr   )rz   r  r   ÚmutatingtupleÕ  s    
z5RegressionTests.test_sf_793826.<locals>.mutatingtuplec                s   | a tt  d d < d S )N)ÚTr   )r¹   )r  r   r   rz   ä  s    z)RegressionTests.test_sf_793826.<locals>.f)r   r   rP   )r;   rZ   rQ   )r   r  rE   )r   r  rz   Úsecondr   )r  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 )NrP   r   r;   )rî   )r   )r  r   r   Úgen2ú  s    
z,RegressionTests.test_sf_950057.<locals>.gen2Fr   r   T)rc   r  r   rq   rE   rÖ   )r   r  r  r   )r  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 )rq   rs   r   rc   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 )NrZ   rQ   )r   )r   )rM   r   r   rz     s    z,RegressionTests.test_issue30347_1.<locals>.fr/   )r¬   r   r   )r   rz   rª   r   )rM   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"   r  r  r  r  r  r   r   r   r   r  Ð  s
   r  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÷   rq   rb   r+  r$   r:  r<  rÖ   rÁ   rA   Ú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3PirX   Z12r   )
rW   rW   rW   rW   rW   rW   rW   rW   rW   rW   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Ú3Pnirp   rP   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š  rp   rP   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4Pnirp   r;   rP   r    rZ   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   rZ   )r:   rx  rf  r  r  r  rQ  ra  r  r   Úrun_unittestÚhasattrrÈ   Úgcr   r   Úcollectr­  r  Úrun_doctestÚmodulesr    )ÚverboseZtest_classesr°  Zcountsr   r   r   r   Ú	test_main	  s    


rµ  Ú__main__T)rŽ  )N)>ÚunittestÚtestr   Ú	itertoolsr3  Údecimalr   Z	fractionsr   r'   r  ru   r1   Ú	functoolsr   rÈ   r  ÚMAX_Py_ssize_trÅ   Zminsizer   r   r   r   r   r   r   r   r   r&   r*   r+   r-   r.   r0   r   rg   rw   ÚTestCaser:   rQ  ra  rf  ro  rp  rr  rs  r  rt  ru  rv  rw  rx  r  r  r  Úcpython_onlyr  r¬  Z__test__rµ  r    r   r   r   r   Ú<module>   s   
           n*e	
	 [
  _

