Home | History | Annotate | Download | only in test
      1 import unittest
      2 from test import test_support
      3 from itertools import *
      4 import weakref
      5 from decimal import Decimal
      6 from fractions import Fraction
      7 import sys
      8 import operator
      9 import random
     10 import copy
     11 import pickle
     12 from functools import reduce
     13 maxsize = test_support.MAX_Py_ssize_t
     14 minsize = -maxsize-1
     15 
     16 def onearg(x):
     17     'Test function of one argument'
     18     return 2*x
     19 
     20 def errfunc(*args):
     21     'Test function that raises an error'
     22     raise ValueError
     23 
     24 def gen3():
     25     'Non-restartable source sequence'
     26     for i in (0, 1, 2):
     27         yield i
     28 
     29 def isEven(x):
     30     'Test predicate'
     31     return x%2==0
     32 
     33 def isOdd(x):
     34     'Test predicate'
     35     return x%2==1
     36 
     37 class StopNow:
     38     'Class emulating an empty iterable.'
     39     def __iter__(self):
     40         return self
     41     def next(self):
     42         raise StopIteration
     43 
     44 def take(n, seq):
     45     'Convenience function for partially consuming a long of infinite iterable'
     46     return list(islice(seq, n))
     47 
     48 def prod(iterable):
     49     return reduce(operator.mul, iterable, 1)
     50 
     51 def fact(n):
     52     'Factorial'
     53     return prod(range(1, n+1))
     54 
     55 class TestBasicOps(unittest.TestCase):
     56     def test_chain(self):
     57 
     58         def chain2(*iterables):
     59             'Pure python version in the docs'
     60             for it in iterables:
     61                 for element in it:
     62                     yield element
     63 
     64         for c in (chain, chain2):
     65             self.assertEqual(list(c('abc', 'def')), list('abcdef'))
     66             self.assertEqual(list(c('abc')), list('abc'))
     67             self.assertEqual(list(c('')), [])
     68             self.assertEqual(take(4, c('abc', 'def')), list('abcd'))
     69             self.assertRaises(TypeError, list,c(2, 3))
     70 
     71     def test_chain_from_iterable(self):
     72         self.assertEqual(list(chain.from_iterable(['abc', 'def'])), list('abcdef'))
     73         self.assertEqual(list(chain.from_iterable(['abc'])), list('abc'))
     74         self.assertEqual(list(chain.from_iterable([''])), [])
     75         self.assertEqual(take(4, chain.from_iterable(['abc', 'def'])), list('abcd'))
     76         self.assertRaises(TypeError, list, chain.from_iterable([2, 3]))
     77 
     78     def test_combinations(self):
     79         self.assertRaises(TypeError, combinations, 'abc')       # missing r argument
     80         self.assertRaises(TypeError, combinations, 'abc', 2, 1) # too many arguments
     81         self.assertRaises(TypeError, combinations, None)        # pool is not iterable
     82         self.assertRaises(ValueError, combinations, 'abc', -2)  # r is negative
     83         self.assertEqual(list(combinations('abc', 32)), [])     # r > n
     84         self.assertEqual(list(combinations(range(4), 3)),
     85                                            [(0,1,2), (0,1,3), (0,2,3), (1,2,3)])
     86 
     87         def combinations1(iterable, r):
     88             'Pure python version shown in the docs'
     89             pool = tuple(iterable)
     90             n = len(pool)
     91             if r > n:
     92                 return
     93             indices = range(r)
     94             yield tuple(pool[i] for i in indices)
     95             while 1:
     96                 for i in reversed(range(r)):
     97                     if indices[i] != i + n - r:
     98                         break
     99                 else:
    100                     return
    101                 indices[i] += 1
    102                 for j in range(i+1, r):
    103                     indices[j] = indices[j-1] + 1
    104                 yield tuple(pool[i] for i in indices)
    105 
    106         def combinations2(iterable, r):
    107             'Pure python version shown in the docs'
    108             pool = tuple(iterable)
    109             n = len(pool)
    110             for indices in permutations(range(n), r):
    111                 if sorted(indices) == list(indices):
    112                     yield tuple(pool[i] for i in indices)
    113 
    114         def combinations3(iterable, r):
    115             'Pure python version from cwr()'
    116             pool = tuple(iterable)
    117             n = len(pool)
    118             for indices in combinations_with_replacement(range(n), r):
    119                 if len(set(indices)) == r:
    120                     yield tuple(pool[i] for i in indices)
    121 
    122         for n in range(7):
    123             values = [5*x-12 for x in range(n)]
    124             for r in range(n+2):
    125                 result = list(combinations(values, r))
    126                 self.assertEqual(len(result), 0 if r>n else fact(n) // fact(r) // fact(n-r)) # right number of combs
    127                 self.assertEqual(len(result), len(set(result)))         # no repeats
    128                 self.assertEqual(result, sorted(result))                # lexicographic order
    129                 for c in result:
    130                     self.assertEqual(len(c), r)                         # r-length combinations
    131                     self.assertEqual(len(set(c)), r)                    # no duplicate elements
    132                     self.assertEqual(list(c), sorted(c))                # keep original ordering
    133                     self.assertTrue(all(e in values for e in c))           # elements taken from input iterable
    134                     self.assertEqual(list(c),
    135                                      [e for e in values if e in c])      # comb is a subsequence of the input iterable
    136                 self.assertEqual(result, list(combinations1(values, r))) # matches first pure python version
    137                 self.assertEqual(result, list(combinations2(values, r))) # matches second pure python version
    138                 self.assertEqual(result, list(combinations3(values, r))) # matches second pure python version
    139 
    140     @test_support.bigaddrspacetest
    141     def test_combinations_overflow(self):
    142         with self.assertRaises((OverflowError, MemoryError)):
    143             combinations("AA", 2**29)
    144 
    145     @test_support.impl_detail("tuple reuse is specific to CPython")
    146     def test_combinations_tuple_reuse(self):
    147         self.assertEqual(len(set(map(id, combinations('abcde', 3)))), 1)
    148         self.assertNotEqual(len(set(map(id, list(combinations('abcde', 3))))), 1)
    149 
    150     def test_combinations_with_replacement(self):
    151         cwr = combinations_with_replacement
    152         self.assertRaises(TypeError, cwr, 'abc')       # missing r argument
    153         self.assertRaises(TypeError, cwr, 'abc', 2, 1) # too many arguments
    154         self.assertRaises(TypeError, cwr, None)        # pool is not iterable
    155         self.assertRaises(ValueError, cwr, 'abc', -2)  # r is negative
    156         self.assertEqual(list(cwr('ABC', 2)),
    157                          [('A','A'), ('A','B'), ('A','C'), ('B','B'), ('B','C'), ('C','C')])
    158 
    159         def cwr1(iterable, r):
    160             'Pure python version shown in the docs'
    161             # number items returned:  (n+r-1)! / r! / (n-1)! when n>0
    162             pool = tuple(iterable)
    163             n = len(pool)
    164             if not n and r:
    165                 return
    166             indices = [0] * r
    167             yield tuple(pool[i] for i in indices)
    168             while 1:
    169                 for i in reversed(range(r)):
    170                     if indices[i] != n - 1:
    171                         break
    172                 else:
    173                     return
    174                 indices[i:] = [indices[i] + 1] * (r - i)
    175                 yield tuple(pool[i] for i in indices)
    176 
    177         def cwr2(iterable, r):
    178             'Pure python version shown in the docs'
    179             pool = tuple(iterable)
    180             n = len(pool)
    181             for indices in product(range(n), repeat=r):
    182                 if sorted(indices) == list(indices):
    183                     yield tuple(pool[i] for i in indices)
    184 
    185         def numcombs(n, r):
    186             if not n:
    187                 return 0 if r else 1
    188             return fact(n+r-1) // fact(r) // fact(n-1)
    189 
    190         for n in range(7):
    191             values = [5*x-12 for x in range(n)]
    192             for r in range(n+2):
    193                 result = list(cwr(values, r))
    194 
    195                 self.assertEqual(len(result), numcombs(n, r))           # right number of combs
    196                 self.assertEqual(len(result), len(set(result)))         # no repeats
    197                 self.assertEqual(result, sorted(result))                # lexicographic order
    198 
    199                 regular_combs = list(combinations(values, r))           # compare to combs without replacement
    200                 if n == 0 or r <= 1:
    201                     self.assertEqual(result, regular_combs)            # cases that should be identical
    202                 else:
    203                     self.assertTrue(set(result) >= set(regular_combs))     # rest should be supersets of regular combs
    204 
    205                 for c in result:
    206                     self.assertEqual(len(c), r)                         # r-length combinations
    207                     noruns = [k for k,v in groupby(c)]                  # combo without consecutive repeats
    208                     self.assertEqual(len(noruns), len(set(noruns)))     # no repeats other than consecutive
    209                     self.assertEqual(list(c), sorted(c))                # keep original ordering
    210                     self.assertTrue(all(e in values for e in c))           # elements taken from input iterable
    211                     self.assertEqual(noruns,
    212                                      [e for e in values if e in c])     # comb is a subsequence of the input iterable
    213                 self.assertEqual(result, list(cwr1(values, r)))         # matches first pure python version
    214                 self.assertEqual(result, list(cwr2(values, r)))         # matches second pure python version
    215 
    216     @test_support.bigaddrspacetest
    217     def test_combinations_with_replacement_overflow(self):
    218         with self.assertRaises((OverflowError, MemoryError)):
    219             combinations_with_replacement("AA", 2**30)
    220 
    221     @test_support.impl_detail("tuple reuse is specific to CPython")
    222     def test_combinations_with_replacement_tuple_reuse(self):
    223         cwr = combinations_with_replacement
    224         self.assertEqual(len(set(map(id, cwr('abcde', 3)))), 1)
    225         self.assertNotEqual(len(set(map(id, list(cwr('abcde', 3))))), 1)
    226 
    227     def test_permutations(self):
    228         self.assertRaises(TypeError, permutations)              # too few arguments
    229         self.assertRaises(TypeError, permutations, 'abc', 2, 1) # too many arguments
    230         self.assertRaises(TypeError, permutations, None)        # pool is not iterable
    231         self.assertRaises(ValueError, permutations, 'abc', -2)  # r is negative
    232         self.assertEqual(list(permutations('abc', 32)), [])     # r > n
    233         self.assertRaises(TypeError, permutations, 'abc', 's')  # r is not an int or None
    234         self.assertEqual(list(permutations(range(3), 2)),
    235                                            [(0,1), (0,2), (1,0), (1,2), (2,0), (2,1)])
    236 
    237         def permutations1(iterable, r=None):
    238             'Pure python version shown in the docs'
    239             pool = tuple(iterable)
    240             n = len(pool)
    241             r = n if r is None else r
    242             if r > n:
    243                 return
    244             indices = range(n)
    245             cycles = range(n, n-r, -1)
    246             yield tuple(pool[i] for i in indices[:r])
    247             while n:
    248                 for i in reversed(range(r)):
    249                     cycles[i] -= 1
    250                     if cycles[i] == 0:
    251                         indices[i:] = indices[i+1:] + indices[i:i+1]
    252                         cycles[i] = n - i
    253                     else:
    254                         j = cycles[i]
    255                         indices[i], indices[-j] = indices[-j], indices[i]
    256                         yield tuple(pool[i] for i in indices[:r])
    257                         break
    258                 else:
    259                     return
    260 
    261         def permutations2(iterable, r=None):
    262             'Pure python version shown in the docs'
    263             pool = tuple(iterable)
    264             n = len(pool)
    265             r = n if r is None else r
    266             for indices in product(range(n), repeat=r):
    267                 if len(set(indices)) == r:
    268                     yield tuple(pool[i] for i in indices)
    269 
    270         for n in range(7):
    271             values = [5*x-12 for x in range(n)]
    272             for r in range(n+2):
    273                 result = list(permutations(values, r))
    274                 self.assertEqual(len(result), 0 if r>n else fact(n) // fact(n-r))      # right number of perms
    275                 self.assertEqual(len(result), len(set(result)))         # no repeats
    276                 self.assertEqual(result, sorted(result))                # lexicographic order
    277                 for p in result:
    278                     self.assertEqual(len(p), r)                         # r-length permutations
    279                     self.assertEqual(len(set(p)), r)                    # no duplicate elements
    280                     self.assertTrue(all(e in values for e in p))           # elements taken from input iterable
    281                 self.assertEqual(result, list(permutations1(values, r))) # matches first pure python version
    282                 self.assertEqual(result, list(permutations2(values, r))) # matches second pure python version
    283                 if r == n:
    284                     self.assertEqual(result, list(permutations(values, None))) # test r as None
    285                     self.assertEqual(result, list(permutations(values)))       # test default r
    286 
    287     @test_support.bigaddrspacetest
    288     def test_permutations_overflow(self):
    289         with self.assertRaises((OverflowError, MemoryError)):
    290             permutations("A", 2**30)
    291 
    292     @test_support.impl_detail("tuple reuse is specific to CPython")
    293     def test_permutations_tuple_reuse(self):
    294         self.assertEqual(len(set(map(id, permutations('abcde', 3)))), 1)
    295         self.assertNotEqual(len(set(map(id, list(permutations('abcde', 3))))), 1)
    296 
    297     def test_combinatorics(self):
    298         # Test relationships between product(), permutations(),
    299         # combinations() and combinations_with_replacement().
    300 
    301         for n in range(6):
    302             s = 'ABCDEFG'[:n]
    303             for r in range(8):
    304                 prod = list(product(s, repeat=r))
    305                 cwr = list(combinations_with_replacement(s, r))
    306                 perm = list(permutations(s, r))
    307                 comb = list(combinations(s, r))
    308 
    309                 # Check size
    310                 self.assertEqual(len(prod), n**r)
    311                 self.assertEqual(len(cwr), (fact(n+r-1) // fact(r) // fact(n-1)) if n else (not r))
    312                 self.assertEqual(len(perm), 0 if r>n else fact(n) // fact(n-r))
    313                 self.assertEqual(len(comb), 0 if r>n else fact(n) // fact(r) // fact(n-r))
    314 
    315                 # Check lexicographic order without repeated tuples
    316                 self.assertEqual(prod, sorted(set(prod)))
    317                 self.assertEqual(cwr, sorted(set(cwr)))
    318                 self.assertEqual(perm, sorted(set(perm)))
    319                 self.assertEqual(comb, sorted(set(comb)))
    320 
    321                 # Check interrelationships
    322                 self.assertEqual(cwr, [t for t in prod if sorted(t)==list(t)]) # cwr: prods which are sorted
    323                 self.assertEqual(perm, [t for t in prod if len(set(t))==r])    # perm: prods with no dups
    324                 self.assertEqual(comb, [t for t in perm if sorted(t)==list(t)]) # comb: perms that are sorted
    325                 self.assertEqual(comb, [t for t in cwr if len(set(t))==r])      # comb: cwrs without dups
    326                 self.assertEqual(comb, filter(set(cwr).__contains__, perm))     # comb: perm that is a cwr
    327                 self.assertEqual(comb, filter(set(perm).__contains__, cwr))     # comb: cwr that is a perm
    328                 self.assertEqual(comb, sorted(set(cwr) & set(perm)))            # comb: both a cwr and a perm
    329 
    330     def test_compress(self):
    331         self.assertEqual(list(compress(data='ABCDEF', selectors=[1,0,1,0,1,1])), list('ACEF'))
    332         self.assertEqual(list(compress('ABCDEF', [1,0,1,0,1,1])), list('ACEF'))
    333         self.assertEqual(list(compress('ABCDEF', [0,0,0,0,0,0])), list(''))
    334         self.assertEqual(list(compress('ABCDEF', [1,1,1,1,1,1])), list('ABCDEF'))
    335         self.assertEqual(list(compress('ABCDEF', [1,0,1])), list('AC'))
    336         self.assertEqual(list(compress('ABC', [0,1,1,1,1,1])), list('BC'))
    337         n = 10000
    338         data = chain.from_iterable(repeat(range(6), n))
    339         selectors = chain.from_iterable(repeat((0, 1)))
    340         self.assertEqual(list(compress(data, selectors)), [1,3,5] * n)
    341         self.assertRaises(TypeError, compress, None, range(6))      # 1st arg not iterable
    342         self.assertRaises(TypeError, compress, range(6), None)      # 2nd arg not iterable
    343         self.assertRaises(TypeError, compress, range(6))            # too few args
    344         self.assertRaises(TypeError, compress, range(6), None)      # too many args
    345 
    346     def test_count(self):
    347         self.assertEqual(zip('abc',count()), [('a', 0), ('b', 1), ('c', 2)])
    348         self.assertEqual(zip('abc',count(3)), [('a', 3), ('b', 4), ('c', 5)])
    349         self.assertEqual(take(2, zip('abc',count(3))), [('a', 3), ('b', 4)])
    350         self.assertEqual(take(2, zip('abc',count(-1))), [('a', -1), ('b', 0)])
    351         self.assertEqual(take(2, zip('abc',count(-3))), [('a', -3), ('b', -2)])
    352         self.assertRaises(TypeError, count, 2, 3, 4)
    353         self.assertRaises(TypeError, count, 'a')
    354         self.assertEqual(take(10, count(maxsize-5)), range(maxsize-5, maxsize+5))
    355         self.assertEqual(take(10, count(-maxsize-5)), range(-maxsize-5, -maxsize+5))
    356         self.assertEqual(take(3, count(3.25)), [3.25, 4.25, 5.25])
    357         self.assertEqual(take(3, count(3.25-4j)), [3.25-4j, 4.25-4j, 5.25-4j])
    358         self.assertEqual(take(3, count(Decimal('1.1'))),
    359                          [Decimal('1.1'), Decimal('2.1'), Decimal('3.1')])
    360         self.assertEqual(take(3, count(Fraction(2, 3))),
    361                          [Fraction(2, 3), Fraction(5, 3), Fraction(8, 3)])
    362         BIGINT = 1<<1000
    363         self.assertEqual(take(3, count(BIGINT)), [BIGINT, BIGINT+1, BIGINT+2])
    364         c = count(3)
    365         self.assertEqual(repr(c), 'count(3)')
    366         c.next()
    367         self.assertEqual(repr(c), 'count(4)')
    368         c = count(-9)
    369         self.assertEqual(repr(c), 'count(-9)')
    370         c.next()
    371         self.assertEqual(next(c), -8)
    372         self.assertEqual(repr(count(10.25)), 'count(10.25)')
    373         self.assertEqual(repr(count(10.0)), 'count(10.0)')
    374         self.assertEqual(type(next(count(10.0))), float)
    375         for i in (-sys.maxint-5, -sys.maxint+5 ,-10, -1, 0, 10, sys.maxint-5, sys.maxint+5):
    376             # Test repr (ignoring the L in longs)
    377             r1 = repr(count(i)).replace('L', '')
    378             r2 = 'count(%r)'.__mod__(i).replace('L', '')
    379             self.assertEqual(r1, r2)
    380 
    381         # check copy, deepcopy, pickle
    382         for value in -3, 3, sys.maxint-5, sys.maxint+5:
    383             c = count(value)
    384             self.assertEqual(next(copy.copy(c)), value)
    385             self.assertEqual(next(copy.deepcopy(c)), value)
    386             for proto in range(pickle.HIGHEST_PROTOCOL + 1):
    387                 self.assertEqual(next(pickle.loads(pickle.dumps(c, proto))), value)
    388 
    389     def test_count_with_stride(self):
    390         self.assertEqual(zip('abc',count(2,3)), [('a', 2), ('b', 5), ('c', 8)])
    391         self.assertEqual(zip('abc',count(start=2,step=3)),
    392                          [('a', 2), ('b', 5), ('c', 8)])
    393         self.assertEqual(zip('abc',count(step=-1)),
    394                          [('a', 0), ('b', -1), ('c', -2)])
    395         self.assertRaises(TypeError, count, 'a', 'b')
    396         self.assertEqual(zip('abc',count(2,0)), [('a', 2), ('b', 2), ('c', 2)])
    397         self.assertEqual(zip('abc',count(2,1)), [('a', 2), ('b', 3), ('c', 4)])
    398         self.assertEqual(zip('abc',count(2,3)), [('a', 2), ('b', 5), ('c', 8)])
    399         self.assertEqual(zip('abc',count(2,1L)), [('a', 2), ('b', 3), ('c', 4)])
    400         self.assertEqual(zip('abc',count(2,3L)), [('a', 2), ('b', 5), ('c', 8)])
    401         self.assertEqual(take(20, count(maxsize-15, 3)), take(20, range(maxsize-15, maxsize+100, 3)))
    402         self.assertEqual(take(20, count(-maxsize-15, 3)), take(20, range(-maxsize-15,-maxsize+100, 3)))
    403         self.assertEqual(take(3, count(10, maxsize+5)),
    404                          range(10, 10+3*(maxsize+5), maxsize+5))
    405         self.assertEqual(take(3, count(2, 1.25)), [2, 3.25, 4.5])
    406         self.assertEqual(take(3, count(2, 3.25-4j)), [2, 5.25-4j, 8.5-8j])
    407         self.assertEqual(take(3, count(Decimal('1.1'), Decimal('.1'))),
    408                          [Decimal('1.1'), Decimal('1.2'), Decimal('1.3')])
    409         self.assertEqual(take(3, count(Fraction(2,3), Fraction(1,7))),
    410                          [Fraction(2,3), Fraction(17,21), Fraction(20,21)])
    411         BIGINT = 1<<1000
    412         self.assertEqual(take(3, count(step=BIGINT)), [0, BIGINT, 2*BIGINT])
    413         self.assertEqual(repr(take(3, count(10, 2.5))), repr([10, 12.5, 15.0]))
    414         c = count(3, 5)
    415         self.assertEqual(repr(c), 'count(3, 5)')
    416         c.next()
    417         self.assertEqual(repr(c), 'count(8, 5)')
    418         c = count(-9, 0)
    419         self.assertEqual(repr(c), 'count(-9, 0)')
    420         c.next()
    421         self.assertEqual(repr(c), 'count(-9, 0)')
    422         c = count(-9, -3)
    423         self.assertEqual(repr(c), 'count(-9, -3)')
    424         c.next()
    425         self.assertEqual(repr(c), 'count(-12, -3)')
    426         self.assertEqual(repr(c), 'count(-12, -3)')
    427         self.assertEqual(repr(count(10.5, 1.25)), 'count(10.5, 1.25)')
    428         self.assertEqual(repr(count(10.5, 1)), 'count(10.5)')           # suppress step=1 when it's an int
    429         self.assertEqual(repr(count(10.5, 1.00)), 'count(10.5, 1.0)')   # do show float values lilke 1.0
    430         self.assertEqual(repr(count(10, 1.00)), 'count(10, 1.0)')
    431         c = count(10, 1.0)
    432         self.assertEqual(type(next(c)), int)
    433         self.assertEqual(type(next(c)), float)
    434         for i in (-sys.maxint-5, -sys.maxint+5 ,-10, -1, 0, 10, sys.maxint-5, sys.maxint+5):
    435             for j in  (-sys.maxint-5, -sys.maxint+5 ,-10, -1, 0, 1, 10, sys.maxint-5, sys.maxint+5):
    436                 # Test repr (ignoring the L in longs)
    437                 r1 = repr(count(i, j)).replace('L', '')
    438                 if j == 1:
    439                     r2 = ('count(%r)' % i).replace('L', '')
    440                 else:
    441                     r2 = ('count(%r, %r)' % (i, j)).replace('L', '')
    442                 self.assertEqual(r1, r2)
    443 
    444     def test_cycle(self):
    445         self.assertEqual(take(10, cycle('abc')), list('abcabcabca'))
    446         self.assertEqual(list(cycle('')), [])
    447         self.assertRaises(TypeError, cycle)
    448         self.assertRaises(TypeError, cycle, 5)
    449         self.assertEqual(list(islice(cycle(gen3()),10)), [0,1,2,0,1,2,0,1,2,0])
    450 
    451     def test_groupby(self):
    452         # Check whether it accepts arguments correctly
    453         self.assertEqual([], list(groupby([])))
    454         self.assertEqual([], list(groupby([], key=id)))
    455         self.assertRaises(TypeError, list, groupby('abc', []))
    456         self.assertRaises(TypeError, groupby, None)
    457         self.assertRaises(TypeError, groupby, 'abc', lambda x:x, 10)
    458 
    459         # Check normal input
    460         s = [(0, 10, 20), (0, 11,21), (0,12,21), (1,13,21), (1,14,22),
    461              (2,15,22), (3,16,23), (3,17,23)]
    462         dup = []
    463         for k, g in groupby(s, lambda r:r[0]):
    464             for elem in g:
    465                 self.assertEqual(k, elem[0])
    466                 dup.append(elem)
    467         self.assertEqual(s, dup)
    468 
    469         # Check nested case
    470         dup = []
    471         for k, g in groupby(s, lambda r:r[0]):
    472             for ik, ig in groupby(g, lambda r:r[2]):
    473                 for elem in ig:
    474                     self.assertEqual(k, elem[0])
    475                     self.assertEqual(ik, elem[2])
    476                     dup.append(elem)
    477         self.assertEqual(s, dup)
    478 
    479         # Check case where inner iterator is not used
    480         keys = [k for k, g in groupby(s, lambda r:r[0])]
    481         expectedkeys = set([r[0] for r in s])
    482         self.assertEqual(set(keys), expectedkeys)
    483         self.assertEqual(len(keys), len(expectedkeys))
    484 
    485         # Exercise pipes and filters style
    486         s = 'abracadabra'
    487         # sort s | uniq
    488         r = [k for k, g in groupby(sorted(s))]
    489         self.assertEqual(r, ['a', 'b', 'c', 'd', 'r'])
    490         # sort s | uniq -d
    491         r = [k for k, g in groupby(sorted(s)) if list(islice(g,1,2))]
    492         self.assertEqual(r, ['a', 'b', 'r'])
    493         # sort s | uniq -c
    494         r = [(len(list(g)), k) for k, g in groupby(sorted(s))]
    495         self.assertEqual(r, [(5, 'a'), (2, 'b'), (1, 'c'), (1, 'd'), (2, 'r')])
    496         # sort s | uniq -c | sort -rn | head -3
    497         r = sorted([(len(list(g)) , k) for k, g in groupby(sorted(s))], reverse=True)[:3]
    498         self.assertEqual(r, [(5, 'a'), (2, 'r'), (2, 'b')])
    499 
    500         # iter.next failure
    501         class ExpectedError(Exception):
    502             pass
    503         def delayed_raise(n=0):
    504             for i in range(n):
    505                 yield 'yo'
    506             raise ExpectedError
    507         def gulp(iterable, keyp=None, func=list):
    508             return [func(g) for k, g in groupby(iterable, keyp)]
    509 
    510         # iter.next failure on outer object
    511         self.assertRaises(ExpectedError, gulp, delayed_raise(0))
    512         # iter.next failure on inner object
    513         self.assertRaises(ExpectedError, gulp, delayed_raise(1))
    514 
    515         # __cmp__ failure
    516         class DummyCmp:
    517             def __cmp__(self, dst):
    518                 raise ExpectedError
    519         s = [DummyCmp(), DummyCmp(), None]
    520 
    521         # __cmp__ failure on outer object
    522         self.assertRaises(ExpectedError, gulp, s, func=id)
    523         # __cmp__ failure on inner object
    524         self.assertRaises(ExpectedError, gulp, s)
    525 
    526         # keyfunc failure
    527         def keyfunc(obj):
    528             if keyfunc.skip > 0:
    529                 keyfunc.skip -= 1
    530                 return obj
    531             else:
    532                 raise ExpectedError
    533 
    534         # keyfunc failure on outer object
    535         keyfunc.skip = 0
    536         self.assertRaises(ExpectedError, gulp, [None], keyfunc)
    537         keyfunc.skip = 1
    538         self.assertRaises(ExpectedError, gulp, [None, None], keyfunc)
    539 
    540     def test_ifilter(self):
    541         self.assertEqual(list(ifilter(isEven, range(6))), [0,2,4])
    542         self.assertEqual(list(ifilter(None, [0,1,0,2,0])), [1,2])
    543         self.assertEqual(list(ifilter(bool, [0,1,0,2,0])), [1,2])
    544         self.assertEqual(take(4, ifilter(isEven, count())), [0,2,4,6])
    545         self.assertRaises(TypeError, ifilter)
    546         self.assertRaises(TypeError, ifilter, lambda x:x)
    547         self.assertRaises(TypeError, ifilter, lambda x:x, range(6), 7)
    548         self.assertRaises(TypeError, ifilter, isEven, 3)
    549         self.assertRaises(TypeError, ifilter(range(6), range(6)).next)
    550 
    551     def test_ifilterfalse(self):
    552         self.assertEqual(list(ifilterfalse(isEven, range(6))), [1,3,5])
    553         self.assertEqual(list(ifilterfalse(None, [0,1,0,2,0])), [0,0,0])
    554         self.assertEqual(list(ifilterfalse(bool, [0,1,0,2,0])), [0,0,0])
    555         self.assertEqual(take(4, ifilterfalse(isEven, count())), [1,3,5,7])
    556         self.assertRaises(TypeError, ifilterfalse)
    557         self.assertRaises(TypeError, ifilterfalse, lambda x:x)
    558         self.assertRaises(TypeError, ifilterfalse, lambda x:x, range(6), 7)
    559         self.assertRaises(TypeError, ifilterfalse, isEven, 3)
    560         self.assertRaises(TypeError, ifilterfalse(range(6), range(6)).next)
    561 
    562     def test_izip(self):
    563         ans = [(x,y) for x, y in izip('abc',count())]
    564         self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)])
    565         self.assertEqual(list(izip('abc', range(6))), zip('abc', range(6)))
    566         self.assertEqual(list(izip('abcdef', range(3))), zip('abcdef', range(3)))
    567         self.assertEqual(take(3,izip('abcdef', count())), zip('abcdef', range(3)))
    568         self.assertEqual(list(izip('abcdef')), zip('abcdef'))
    569         self.assertEqual(list(izip()), zip())
    570         self.assertRaises(TypeError, izip, 3)
    571         self.assertRaises(TypeError, izip, range(3), 3)
    572         self.assertEqual([tuple(list(pair)) for pair in izip('abc', 'def')],
    573                          zip('abc', 'def'))
    574         self.assertEqual([pair for pair in izip('abc', 'def')],
    575                          zip('abc', 'def'))
    576 
    577     @test_support.impl_detail("tuple reuse is specific to CPython")
    578     def test_izip_tuple_reuse(self):
    579         ids = map(id, izip('abc', 'def'))
    580         self.assertEqual(min(ids), max(ids))
    581         ids = map(id, list(izip('abc', 'def')))
    582         self.assertEqual(len(dict.fromkeys(ids)), len(ids))
    583 
    584     def test_iziplongest(self):
    585         for args in [
    586                 ['abc', range(6)],
    587                 [range(6), 'abc'],
    588                 [range(1000), range(2000,2100), range(3000,3050)],
    589                 [range(1000), range(0), range(3000,3050), range(1200), range(1500)],
    590                 [range(1000), range(0), range(3000,3050), range(1200), range(1500), range(0)],
    591             ]:
    592             # target = map(None, *args) <- this raises a py3k warning
    593             # this is the replacement:
    594             target = [tuple([arg[i] if i < len(arg) else None for arg in args])
    595                       for i in range(max(map(len, args)))]
    596             self.assertEqual(list(izip_longest(*args)), target)
    597             self.assertEqual(list(izip_longest(*args, **{})), target)
    598             target = [tuple((e is None and 'X' or e) for e in t) for t in target]   # Replace None fills with 'X'
    599             self.assertEqual(list(izip_longest(*args, **dict(fillvalue='X'))), target)
    600 
    601         self.assertEqual(take(3,izip_longest('abcdef', count())), zip('abcdef', range(3))) # take 3 from infinite input
    602 
    603         self.assertEqual(list(izip_longest()), zip())
    604         self.assertEqual(list(izip_longest([])), zip([]))
    605         self.assertEqual(list(izip_longest('abcdef')), zip('abcdef'))
    606 
    607         self.assertEqual(list(izip_longest('abc', 'defg', **{})),
    608                          zip(list('abc') + [None], 'defg'))  # empty keyword dict
    609         self.assertRaises(TypeError, izip_longest, 3)
    610         self.assertRaises(TypeError, izip_longest, range(3), 3)
    611 
    612         for stmt in [
    613             "izip_longest('abc', fv=1)",
    614             "izip_longest('abc', fillvalue=1, bogus_keyword=None)",
    615         ]:
    616             try:
    617                 eval(stmt, globals(), locals())
    618             except TypeError:
    619                 pass
    620             else:
    621                 self.fail('Did not raise Type in:  ' + stmt)
    622 
    623         self.assertEqual([tuple(list(pair)) for pair in izip_longest('abc', 'def')],
    624                          zip('abc', 'def'))
    625         self.assertEqual([pair for pair in izip_longest('abc', 'def')],
    626                          zip('abc', 'def'))
    627 
    628     @test_support.impl_detail("tuple reuse is specific to CPython")
    629     def test_izip_longest_tuple_reuse(self):
    630         ids = map(id, izip_longest('abc', 'def'))
    631         self.assertEqual(min(ids), max(ids))
    632         ids = map(id, list(izip_longest('abc', 'def')))
    633         self.assertEqual(len(dict.fromkeys(ids)), len(ids))
    634 
    635     def test_bug_7244(self):
    636 
    637         class Repeater(object):
    638             # this class is similar to itertools.repeat
    639             def __init__(self, o, t, e):
    640                 self.o = o
    641                 self.t = int(t)
    642                 self.e = e
    643             def __iter__(self): # its iterator is itself
    644                 return self
    645             def next(self):
    646                 if self.t > 0:
    647                     self.t -= 1
    648                     return self.o
    649                 else:
    650                     raise self.e
    651 
    652         # Formerly this code in would fail in debug mode
    653         # with Undetected Error and Stop Iteration
    654         r1 = Repeater(1, 3, StopIteration)
    655         r2 = Repeater(2, 4, StopIteration)
    656         def run(r1, r2):
    657             result = []
    658             for i, j in izip_longest(r1, r2, fillvalue=0):
    659                 with test_support.captured_output('stdout'):
    660                     print (i, j)
    661                 result.append((i, j))
    662             return result
    663         self.assertEqual(run(r1, r2), [(1,2), (1,2), (1,2), (0,2)])
    664 
    665         # Formerly, the RuntimeError would be lost
    666         # and StopIteration would stop as expected
    667         r1 = Repeater(1, 3, RuntimeError)
    668         r2 = Repeater(2, 4, StopIteration)
    669         it = izip_longest(r1, r2, fillvalue=0)
    670         self.assertEqual(next(it), (1, 2))
    671         self.assertEqual(next(it), (1, 2))
    672         self.assertEqual(next(it), (1, 2))
    673         self.assertRaises(RuntimeError, next, it)
    674 
    675     def test_product(self):
    676         for args, result in [
    677             ([], [()]),                     # zero iterables
    678             (['ab'], [('a',), ('b',)]),     # one iterable
    679             ([range(2), range(3)], [(0,0), (0,1), (0,2), (1,0), (1,1), (1,2)]),     # two iterables
    680             ([range(0), range(2), range(3)], []),           # first iterable with zero length
    681             ([range(2), range(0), range(3)], []),           # middle iterable with zero length
    682             ([range(2), range(3), range(0)], []),           # last iterable with zero length
    683             ]:
    684             self.assertEqual(list(product(*args)), result)
    685             for r in range(4):
    686                 self.assertEqual(list(product(*(args*r))),
    687                                  list(product(*args, **dict(repeat=r))))
    688         self.assertEqual(len(list(product(*[range(7)]*6))), 7**6)
    689         self.assertRaises(TypeError, product, range(6), None)
    690 
    691         def product1(*args, **kwds):
    692             pools = map(tuple, args) * kwds.get('repeat', 1)
    693             n = len(pools)
    694             if n == 0:
    695                 yield ()
    696                 return
    697             if any(len(pool) == 0 for pool in pools):
    698                 return
    699             indices = [0] * n
    700             yield tuple(pool[i] for pool, i in zip(pools, indices))
    701             while 1:
    702                 for i in reversed(range(n)):  # right to left
    703                     if indices[i] == len(pools[i]) - 1:
    704                         continue
    705                     indices[i] += 1
    706                     for j in range(i+1, n):
    707                         indices[j] = 0
    708                     yield tuple(pool[i] for pool, i in zip(pools, indices))
    709                     break
    710                 else:
    711                     return
    712 
    713         def product2(*args, **kwds):
    714             'Pure python version used in docs'
    715             pools = map(tuple, args) * kwds.get('repeat', 1)
    716             result = [[]]
    717             for pool in pools:
    718                 result = [x+[y] for x in result for y in pool]
    719             for prod in result:
    720                 yield tuple(prod)
    721 
    722         argtypes = ['', 'abc', '', xrange(0), xrange(4), dict(a=1, b=2, c=3),
    723                     set('abcdefg'), range(11), tuple(range(13))]
    724         for i in range(100):
    725             args = [random.choice(argtypes) for j in range(random.randrange(5))]
    726             expected_len = prod(map(len, args))
    727             self.assertEqual(len(list(product(*args))), expected_len)
    728             self.assertEqual(list(product(*args)), list(product1(*args)))
    729             self.assertEqual(list(product(*args)), list(product2(*args)))
    730             args = map(iter, args)
    731             self.assertEqual(len(list(product(*args))), expected_len)
    732 
    733     @test_support.bigaddrspacetest
    734     def test_product_overflow(self):
    735         with self.assertRaises((OverflowError, MemoryError)):
    736             product(*(['ab']*2**5), repeat=2**25)
    737 
    738     @test_support.impl_detail("tuple reuse is specific to CPython")
    739     def test_product_tuple_reuse(self):
    740         self.assertEqual(len(set(map(id, product('abc', 'def')))), 1)
    741         self.assertNotEqual(len(set(map(id, list(product('abc', 'def'))))), 1)
    742 
    743     def test_repeat(self):
    744         self.assertEqual(list(repeat(object='a', times=3)), ['a', 'a', 'a'])
    745         self.assertEqual(list(repeat(object='a', times=0)), [])
    746         self.assertEqual(list(repeat(object='a', times=-1)), [])
    747         self.assertEqual(list(repeat(object='a', times=-2)), [])
    748         self.assertEqual(zip(xrange(3),repeat('a')),
    749                          [(0, 'a'), (1, 'a'), (2, 'a')])
    750         self.assertEqual(list(repeat('a', 3)), ['a', 'a', 'a'])
    751         self.assertEqual(take(3, repeat('a')), ['a', 'a', 'a'])
    752         self.assertEqual(list(repeat('a', 0)), [])
    753         self.assertEqual(list(repeat('a', -3)), [])
    754         self.assertRaises(TypeError, repeat)
    755         self.assertRaises(TypeError, repeat, None, 3, 4)
    756         self.assertRaises(TypeError, repeat, None, 'a')
    757         r = repeat(1+0j)
    758         self.assertEqual(repr(r), 'repeat((1+0j))')
    759         r = repeat(1+0j, 5)
    760         self.assertEqual(repr(r), 'repeat((1+0j), 5)')
    761         list(r)
    762         self.assertEqual(repr(r), 'repeat((1+0j), 0)')
    763 
    764     def test_repeat_with_negative_times(self):
    765         self.assertEqual(repr(repeat('a', -1)), "repeat('a', 0)")
    766         self.assertEqual(repr(repeat('a', -2)), "repeat('a', 0)")
    767         self.assertEqual(repr(repeat('a', times=-1)), "repeat('a', 0)")
    768         self.assertEqual(repr(repeat('a', times=-2)), "repeat('a', 0)")
    769 
    770     def test_imap(self):
    771         self.assertEqual(list(imap(operator.pow, range(3), range(1,7))),
    772                          [0**1, 1**2, 2**3])
    773         self.assertEqual(list(imap(None, 'abc', range(5))),
    774                          [('a',0),('b',1),('c',2)])
    775         self.assertEqual(list(imap(None, 'abc', count())),
    776                          [('a',0),('b',1),('c',2)])
    777         self.assertEqual(take(2,imap(None, 'abc', count())),
    778                          [('a',0),('b',1)])
    779         self.assertEqual(list(imap(operator.pow, [])), [])
    780         self.assertRaises(TypeError, imap)
    781         self.assertRaises(TypeError, imap, operator.neg)
    782         self.assertRaises(TypeError, imap(10, range(5)).next)
    783         self.assertRaises(ValueError, imap(errfunc, [4], [5]).next)
    784         self.assertRaises(TypeError, imap(onearg, [4], [5]).next)
    785 
    786     def test_starmap(self):
    787         self.assertEqual(list(starmap(operator.pow, zip(range(3), range(1,7)))),
    788                          [0**1, 1**2, 2**3])
    789         self.assertEqual(take(3, starmap(operator.pow, izip(count(), count(1)))),
    790                          [0**1, 1**2, 2**3])
    791         self.assertEqual(list(starmap(operator.pow, [])), [])
    792         self.assertEqual(list(starmap(operator.pow, [iter([4,5])])), [4**5])
    793         self.assertRaises(TypeError, list, starmap(operator.pow, [None]))
    794         self.assertRaises(TypeError, starmap)
    795         self.assertRaises(TypeError, starmap, operator.pow, [(4,5)], 'extra')
    796         self.assertRaises(TypeError, starmap(10, [(4,5)]).next)
    797         self.assertRaises(ValueError, starmap(errfunc, [(4,5)]).next)
    798         self.assertRaises(TypeError, starmap(onearg, [(4,5)]).next)
    799 
    800     def test_islice(self):
    801         for args in [          # islice(args) should agree with range(args)
    802                 (10, 20, 3),
    803                 (10, 3, 20),
    804                 (10, 20),
    805                 (10, 3),
    806                 (20,)
    807                 ]:
    808             self.assertEqual(list(islice(xrange(100), *args)), range(*args))
    809 
    810         for args, tgtargs in [  # Stop when seqn is exhausted
    811                 ((10, 110, 3), ((10, 100, 3))),
    812                 ((10, 110), ((10, 100))),
    813                 ((110,), (100,))
    814                 ]:
    815             self.assertEqual(list(islice(xrange(100), *args)), range(*tgtargs))
    816 
    817         # Test stop=None
    818         self.assertEqual(list(islice(xrange(10), None)), range(10))
    819         self.assertEqual(list(islice(xrange(10), None, None)), range(10))
    820         self.assertEqual(list(islice(xrange(10), None, None, None)), range(10))
    821         self.assertEqual(list(islice(xrange(10), 2, None)), range(2, 10))
    822         self.assertEqual(list(islice(xrange(10), 1, None, 2)), range(1, 10, 2))
    823 
    824         # Test number of items consumed     SF #1171417
    825         it = iter(range(10))
    826         self.assertEqual(list(islice(it, 3)), range(3))
    827         self.assertEqual(list(it), range(3, 10))
    828 
    829         # Test invalid arguments
    830         self.assertRaises(TypeError, islice, xrange(10))
    831         self.assertRaises(TypeError, islice, xrange(10), 1, 2, 3, 4)
    832         self.assertRaises(ValueError, islice, xrange(10), -5, 10, 1)
    833         self.assertRaises(ValueError, islice, xrange(10), 1, -5, -1)
    834         self.assertRaises(ValueError, islice, xrange(10), 1, 10, -1)
    835         self.assertRaises(ValueError, islice, xrange(10), 1, 10, 0)
    836         self.assertRaises(ValueError, islice, xrange(10), 'a')
    837         self.assertRaises(ValueError, islice, xrange(10), 'a', 1)
    838         self.assertRaises(ValueError, islice, xrange(10), 1, 'a')
    839         self.assertRaises(ValueError, islice, xrange(10), 'a', 1, 1)
    840         self.assertRaises(ValueError, islice, xrange(10), 1, 'a', 1)
    841         self.assertEqual(len(list(islice(count(), 1, 10, maxsize))), 1)
    842 
    843         # Issue #10323:  Less islice in a predictable state
    844         c = count()
    845         self.assertEqual(list(islice(c, 1, 3, 50)), [1])
    846         self.assertEqual(next(c), 3)
    847 
    848         # Issue #21321: check source iterator is not referenced
    849         # from islice() after the latter has been exhausted
    850         it = (x for x in (1, 2))
    851         wr = weakref.ref(it)
    852         it = islice(it, 1)
    853         self.assertIsNotNone(wr())
    854         list(it) # exhaust the iterator
    855         test_support.gc_collect()
    856         self.assertIsNone(wr())
    857 
    858     def test_takewhile(self):
    859         data = [1, 3, 5, 20, 2, 4, 6, 8]
    860         underten = lambda x: x<10
    861         self.assertEqual(list(takewhile(underten, data)), [1, 3, 5])
    862         self.assertEqual(list(takewhile(underten, [])), [])
    863         self.assertRaises(TypeError, takewhile)
    864         self.assertRaises(TypeError, takewhile, operator.pow)
    865         self.assertRaises(TypeError, takewhile, operator.pow, [(4,5)], 'extra')
    866         self.assertRaises(TypeError, takewhile(10, [(4,5)]).next)
    867         self.assertRaises(ValueError, takewhile(errfunc, [(4,5)]).next)
    868         t = takewhile(bool, [1, 1, 1, 0, 0, 0])
    869         self.assertEqual(list(t), [1, 1, 1])
    870         self.assertRaises(StopIteration, t.next)
    871 
    872     def test_dropwhile(self):
    873         data = [1, 3, 5, 20, 2, 4, 6, 8]
    874         underten = lambda x: x<10
    875         self.assertEqual(list(dropwhile(underten, data)), [20, 2, 4, 6, 8])
    876         self.assertEqual(list(dropwhile(underten, [])), [])
    877         self.assertRaises(TypeError, dropwhile)
    878         self.assertRaises(TypeError, dropwhile, operator.pow)
    879         self.assertRaises(TypeError, dropwhile, operator.pow, [(4,5)], 'extra')
    880         self.assertRaises(TypeError, dropwhile(10, [(4,5)]).next)
    881         self.assertRaises(ValueError, dropwhile(errfunc, [(4,5)]).next)
    882 
    883     def test_tee(self):
    884         n = 200
    885         def irange(n):
    886             for i in xrange(n):
    887                 yield i
    888 
    889         a, b = tee([])        # test empty iterator
    890         self.assertEqual(list(a), [])
    891         self.assertEqual(list(b), [])
    892 
    893         a, b = tee(irange(n)) # test 100% interleaved
    894         self.assertEqual(zip(a,b), zip(range(n),range(n)))
    895 
    896         a, b = tee(irange(n)) # test 0% interleaved
    897         self.assertEqual(list(a), range(n))
    898         self.assertEqual(list(b), range(n))
    899 
    900         a, b = tee(irange(n)) # test dealloc of leading iterator
    901         for i in xrange(100):
    902             self.assertEqual(a.next(), i)
    903         del a
    904         self.assertEqual(list(b), range(n))
    905 
    906         a, b = tee(irange(n)) # test dealloc of trailing iterator
    907         for i in xrange(100):
    908             self.assertEqual(a.next(), i)
    909         del b
    910         self.assertEqual(list(a), range(100, n))
    911 
    912         for j in xrange(5):   # test randomly interleaved
    913             order = [0]*n + [1]*n
    914             random.shuffle(order)
    915             lists = ([], [])
    916             its = tee(irange(n))
    917             for i in order:
    918                 value = its[i].next()
    919                 lists[i].append(value)
    920             self.assertEqual(lists[0], range(n))
    921             self.assertEqual(lists[1], range(n))
    922 
    923         # test argument format checking
    924         self.assertRaises(TypeError, tee)
    925         self.assertRaises(TypeError, tee, 3)
    926         self.assertRaises(TypeError, tee, [1,2], 'x')
    927         self.assertRaises(TypeError, tee, [1,2], 3, 'x')
    928 
    929         # tee object should be instantiable
    930         a, b = tee('abc')
    931         c = type(a)('def')
    932         self.assertEqual(list(c), list('def'))
    933 
    934         # test long-lagged and multi-way split
    935         a, b, c = tee(xrange(2000), 3)
    936         for i in xrange(100):
    937             self.assertEqual(a.next(), i)
    938         self.assertEqual(list(b), range(2000))
    939         self.assertEqual([c.next(), c.next()], range(2))
    940         self.assertEqual(list(a), range(100,2000))
    941         self.assertEqual(list(c), range(2,2000))
    942 
    943         # test values of n
    944         self.assertRaises(TypeError, tee, 'abc', 'invalid')
    945         self.assertRaises(ValueError, tee, [], -1)
    946         for n in xrange(5):
    947             result = tee('abc', n)
    948             self.assertEqual(type(result), tuple)
    949             self.assertEqual(len(result), n)
    950             self.assertEqual(map(list, result), [list('abc')]*n)
    951 
    952         # tee pass-through to copyable iterator
    953         a, b = tee('abc')
    954         c, d = tee(a)
    955         self.assertTrue(a is c)
    956 
    957         # test tee_new
    958         t1, t2 = tee('abc')
    959         tnew = type(t1)
    960         self.assertRaises(TypeError, tnew)
    961         self.assertRaises(TypeError, tnew, 10)
    962         t3 = tnew(t1)
    963         self.assertTrue(list(t1) == list(t2) == list(t3) == list('abc'))
    964 
    965         # test that tee objects are weak referencable
    966         a, b = tee(xrange(10))
    967         p = weakref.proxy(a)
    968         self.assertEqual(getattr(p, '__class__'), type(b))
    969         del a
    970         self.assertRaises(ReferenceError, getattr, p, '__class__')
    971 
    972     # Issue 13454: Crash when deleting backward iterator from tee()
    973     def test_tee_del_backward(self):
    974         forward, backward = tee(repeat(None, 20000000))
    975         try:
    976             any(forward)  # exhaust the iterator
    977             del backward
    978         except:
    979             del forward, backward
    980             raise
    981 
    982     def test_StopIteration(self):
    983         self.assertRaises(StopIteration, izip().next)
    984 
    985         for f in (chain, cycle, izip, groupby):
    986             self.assertRaises(StopIteration, f([]).next)
    987             self.assertRaises(StopIteration, f(StopNow()).next)
    988 
    989         self.assertRaises(StopIteration, islice([], None).next)
    990         self.assertRaises(StopIteration, islice(StopNow(), None).next)
    991 
    992         p, q = tee([])
    993         self.assertRaises(StopIteration, p.next)
    994         self.assertRaises(StopIteration, q.next)
    995         p, q = tee(StopNow())
    996         self.assertRaises(StopIteration, p.next)
    997         self.assertRaises(StopIteration, q.next)
    998 
    999         self.assertRaises(StopIteration, repeat(None, 0).next)
   1000 
   1001         for f in (ifilter, ifilterfalse, imap, takewhile, dropwhile, starmap):
   1002             self.assertRaises(StopIteration, f(lambda x:x, []).next)
   1003             self.assertRaises(StopIteration, f(lambda x:x, StopNow()).next)
   1004 
   1005 class TestExamples(unittest.TestCase):
   1006 
   1007     def test_chain(self):
   1008         self.assertEqual(''.join(chain('ABC', 'DEF')), 'ABCDEF')
   1009 
   1010     def test_chain_from_iterable(self):
   1011         self.assertEqual(''.join(chain.from_iterable(['ABC', 'DEF'])), 'ABCDEF')
   1012 
   1013     def test_combinations(self):
   1014         self.assertEqual(list(combinations('ABCD', 2)),
   1015                          [('A','B'), ('A','C'), ('A','D'), ('B','C'), ('B','D'), ('C','D')])
   1016         self.assertEqual(list(combinations(range(4), 3)),
   1017                          [(0,1,2), (0,1,3), (0,2,3), (1,2,3)])
   1018 
   1019     def test_combinations_with_replacement(self):
   1020         self.assertEqual(list(combinations_with_replacement('ABC', 2)),
   1021                          [('A','A'), ('A','B'), ('A','C'), ('B','B'), ('B','C'), ('C','C')])
   1022 
   1023     def test_compress(self):
   1024         self.assertEqual(list(compress('ABCDEF', [1,0,1,0,1,1])), list('ACEF'))
   1025 
   1026     def test_count(self):
   1027         self.assertEqual(list(islice(count(10), 5)), [10, 11, 12, 13, 14])
   1028 
   1029     def test_cycle(self):
   1030         self.assertEqual(list(islice(cycle('ABCD'), 12)), list('ABCDABCDABCD'))
   1031 
   1032     def test_dropwhile(self):
   1033         self.assertEqual(list(dropwhile(lambda x: x<5, [1,4,6,4,1])), [6,4,1])
   1034 
   1035     def test_groupby(self):
   1036         self.assertEqual([k for k, g in groupby('AAAABBBCCDAABBB')],
   1037                          list('ABCDAB'))
   1038         self.assertEqual([(list(g)) for k, g in groupby('AAAABBBCCD')],
   1039                          [list('AAAA'), list('BBB'), list('CC'), list('D')])
   1040 
   1041     def test_ifilter(self):
   1042         self.assertEqual(list(ifilter(lambda x: x%2, range(10))), [1,3,5,7,9])
   1043 
   1044     def test_ifilterfalse(self):
   1045         self.assertEqual(list(ifilterfalse(lambda x: x%2, range(10))), [0,2,4,6,8])
   1046 
   1047     def test_imap(self):
   1048         self.assertEqual(list(imap(pow, (2,3,10), (5,2,3))), [32, 9, 1000])
   1049 
   1050     def test_islice(self):
   1051         self.assertEqual(list(islice('ABCDEFG', 2)), list('AB'))
   1052         self.assertEqual(list(islice('ABCDEFG', 2, 4)), list('CD'))
   1053         self.assertEqual(list(islice('ABCDEFG', 2, None)), list('CDEFG'))
   1054         self.assertEqual(list(islice('ABCDEFG', 0, None, 2)), list('ACEG'))
   1055 
   1056     def test_izip(self):
   1057         self.assertEqual(list(izip('ABCD', 'xy')), [('A', 'x'), ('B', 'y')])
   1058 
   1059     def test_izip_longest(self):
   1060         self.assertEqual(list(izip_longest('ABCD', 'xy', fillvalue='-')),
   1061                          [('A', 'x'), ('B', 'y'), ('C', '-'), ('D', '-')])
   1062 
   1063     def test_permutations(self):
   1064         self.assertEqual(list(permutations('ABCD', 2)),
   1065                          map(tuple, 'AB AC AD BA BC BD CA CB CD DA DB DC'.split()))
   1066         self.assertEqual(list(permutations(range(3))),
   1067                          [(0,1,2), (0,2,1), (1,0,2), (1,2,0), (2,0,1), (2,1,0)])
   1068 
   1069     def test_product(self):
   1070         self.assertEqual(list(product('ABCD', 'xy')),
   1071                          map(tuple, 'Ax Ay Bx By Cx Cy Dx Dy'.split()))
   1072         self.assertEqual(list(product(range(2), repeat=3)),
   1073                         [(0,0,0), (0,0,1), (0,1,0), (0,1,1),
   1074                          (1,0,0), (1,0,1), (1,1,0), (1,1,1)])
   1075 
   1076     def test_repeat(self):
   1077         self.assertEqual(list(repeat(10, 3)), [10, 10, 10])
   1078 
   1079     def test_stapmap(self):
   1080         self.assertEqual(list(starmap(pow, [(2,5), (3,2), (10,3)])),
   1081                          [32, 9, 1000])
   1082 
   1083     def test_takewhile(self):
   1084         self.assertEqual(list(takewhile(lambda x: x<5, [1,4,6,4,1])), [1,4])
   1085 
   1086 
   1087 class TestGC(unittest.TestCase):
   1088 
   1089     def makecycle(self, iterator, container):
   1090         container.append(iterator)
   1091         iterator.next()
   1092         del container, iterator
   1093 
   1094     def test_chain(self):
   1095         a = []
   1096         self.makecycle(chain(a), a)
   1097 
   1098     def test_chain_from_iterable(self):
   1099         a = []
   1100         self.makecycle(chain.from_iterable([a]), a)
   1101 
   1102     def test_combinations(self):
   1103         a = []
   1104         self.makecycle(combinations([1,2,a,3], 3), a)
   1105 
   1106     def test_combinations_with_replacement(self):
   1107         a = []
   1108         self.makecycle(combinations_with_replacement([1,2,a,3], 3), a)
   1109 
   1110     def test_compress(self):
   1111         a = []
   1112         self.makecycle(compress('ABCDEF', [1,0,1,0,1,0]), a)
   1113 
   1114     def test_count(self):
   1115         a = []
   1116         Int = type('Int', (int,), dict(x=a))
   1117         self.makecycle(count(Int(0), Int(1)), a)
   1118 
   1119     def test_cycle(self):
   1120         a = []
   1121         self.makecycle(cycle([a]*2), a)
   1122 
   1123     def test_dropwhile(self):
   1124         a = []
   1125         self.makecycle(dropwhile(bool, [0, a, a]), a)
   1126 
   1127     def test_groupby(self):
   1128         a = []
   1129         self.makecycle(groupby([a]*2, lambda x:x), a)
   1130 
   1131     def test_issue2246(self):
   1132         # Issue 2246 -- the _grouper iterator was not included in GC
   1133         n = 10
   1134         keyfunc = lambda x: x
   1135         for i, j in groupby(xrange(n), key=keyfunc):
   1136             keyfunc.__dict__.setdefault('x',[]).append(j)
   1137 
   1138     def test_ifilter(self):
   1139         a = []
   1140         self.makecycle(ifilter(lambda x:True, [a]*2), a)
   1141 
   1142     def test_ifilterfalse(self):
   1143         a = []
   1144         self.makecycle(ifilterfalse(lambda x:False, a), a)
   1145 
   1146     def test_izip(self):
   1147         a = []
   1148         self.makecycle(izip([a]*2, [a]*3), a)
   1149 
   1150     def test_izip_longest(self):
   1151         a = []
   1152         self.makecycle(izip_longest([a]*2, [a]*3), a)
   1153         b = [a, None]
   1154         self.makecycle(izip_longest([a]*2, [a]*3, fillvalue=b), a)
   1155 
   1156     def test_imap(self):
   1157         a = []
   1158         self.makecycle(imap(lambda x:x, [a]*2), a)
   1159 
   1160     def test_islice(self):
   1161         a = []
   1162         self.makecycle(islice([a]*2, None), a)
   1163 
   1164     def test_permutations(self):
   1165         a = []
   1166         self.makecycle(permutations([1,2,a,3], 3), a)
   1167 
   1168     def test_product(self):
   1169         a = []
   1170         self.makecycle(product([1,2,a,3], repeat=3), a)
   1171 
   1172     def test_repeat(self):
   1173         a = []
   1174         self.makecycle(repeat(a), a)
   1175 
   1176     def test_starmap(self):
   1177         a = []
   1178         self.makecycle(starmap(lambda *t: t, [(a,a)]*2), a)
   1179 
   1180     def test_takewhile(self):
   1181         a = []
   1182         self.makecycle(takewhile(bool, [1, 0, a, a]), a)
   1183 
   1184 def R(seqn):
   1185     'Regular generator'
   1186     for i in seqn:
   1187         yield i
   1188 
   1189 class G:
   1190     'Sequence using __getitem__'
   1191     def __init__(self, seqn):
   1192         self.seqn = seqn
   1193     def __getitem__(self, i):
   1194         return self.seqn[i]
   1195 
   1196 class I:
   1197     'Sequence using iterator protocol'
   1198     def __init__(self, seqn):
   1199         self.seqn = seqn
   1200         self.i = 0
   1201     def __iter__(self):
   1202         return self
   1203     def next(self):
   1204         if self.i >= len(self.seqn): raise StopIteration
   1205         v = self.seqn[self.i]
   1206         self.i += 1
   1207         return v
   1208 
   1209 class Ig:
   1210     'Sequence using iterator protocol defined with a generator'
   1211     def __init__(self, seqn):
   1212         self.seqn = seqn
   1213         self.i = 0
   1214     def __iter__(self):
   1215         for val in self.seqn:
   1216             yield val
   1217 
   1218 class X:
   1219     'Missing __getitem__ and __iter__'
   1220     def __init__(self, seqn):
   1221         self.seqn = seqn
   1222         self.i = 0
   1223     def next(self):
   1224         if self.i >= len(self.seqn): raise StopIteration
   1225         v = self.seqn[self.i]
   1226         self.i += 1
   1227         return v
   1228 
   1229 class N:
   1230     'Iterator missing next()'
   1231     def __init__(self, seqn):
   1232         self.seqn = seqn
   1233         self.i = 0
   1234     def __iter__(self):
   1235         return self
   1236 
   1237 class E:
   1238     'Test propagation of exceptions'
   1239     def __init__(self, seqn):
   1240         self.seqn = seqn
   1241         self.i = 0
   1242     def __iter__(self):
   1243         return self
   1244     def next(self):
   1245         3 // 0
   1246 
   1247 class S:
   1248     'Test immediate stop'
   1249     def __init__(self, seqn):
   1250         pass
   1251     def __iter__(self):
   1252         return self
   1253     def next(self):
   1254         raise StopIteration
   1255 
   1256 def L(seqn):
   1257     'Test multiple tiers of iterators'
   1258     return chain(imap(lambda x:x, R(Ig(G(seqn)))))
   1259 
   1260 
   1261 class TestVariousIteratorArgs(unittest.TestCase):
   1262 
   1263     def test_chain(self):
   1264         for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
   1265             for g in (G, I, Ig, S, L, R):
   1266                 self.assertEqual(list(chain(g(s))), list(g(s)))
   1267                 self.assertEqual(list(chain(g(s), g(s))), list(g(s))+list(g(s)))
   1268             self.assertRaises(TypeError, list, chain(X(s)))
   1269             self.assertRaises(TypeError, list, chain(N(s)))
   1270             self.assertRaises(ZeroDivisionError, list, chain(E(s)))
   1271 
   1272     def test_compress(self):
   1273         for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
   1274             n = len(s)
   1275             for g in (G, I, Ig, S, L, R):
   1276                 self.assertEqual(list(compress(g(s), repeat(1))), list(g(s)))
   1277             self.assertRaises(TypeError, compress, X(s), repeat(1))
   1278             self.assertRaises(TypeError, list, compress(N(s), repeat(1)))
   1279             self.assertRaises(ZeroDivisionError, list, compress(E(s), repeat(1)))
   1280 
   1281     def test_product(self):
   1282         for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
   1283             self.assertRaises(TypeError, product, X(s))
   1284             self.assertRaises(TypeError, product, N(s))
   1285             self.assertRaises(ZeroDivisionError, product, E(s))
   1286 
   1287     def test_cycle(self):
   1288         for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
   1289             for g in (G, I, Ig, S, L, R):
   1290                 tgtlen = len(s) * 3
   1291                 expected = list(g(s))*3
   1292                 actual = list(islice(cycle(g(s)), tgtlen))
   1293                 self.assertEqual(actual, expected)
   1294             self.assertRaises(TypeError, cycle, X(s))
   1295             self.assertRaises(TypeError, list, cycle(N(s)))
   1296             self.assertRaises(ZeroDivisionError, list, cycle(E(s)))
   1297 
   1298     def test_groupby(self):
   1299         for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)):
   1300             for g in (G, I, Ig, S, L, R):
   1301                 self.assertEqual([k for k, sb in groupby(g(s))], list(g(s)))
   1302             self.assertRaises(TypeError, groupby, X(s))
   1303             self.assertRaises(TypeError, list, groupby(N(s)))
   1304             self.assertRaises(ZeroDivisionError, list, groupby(E(s)))
   1305 
   1306     def test_ifilter(self):
   1307         for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)):
   1308             for g in (G, I, Ig, S, L, R):
   1309                 self.assertEqual(list(ifilter(isEven, g(s))), filter(isEven, g(s)))
   1310             self.assertRaises(TypeError, ifilter, isEven, X(s))
   1311             self.assertRaises(TypeError, list, ifilter(isEven, N(s)))
   1312             self.assertRaises(ZeroDivisionError, list, ifilter(isEven, E(s)))
   1313 
   1314     def test_ifilterfalse(self):
   1315         for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)):
   1316             for g in (G, I, Ig, S, L, R):
   1317                 self.assertEqual(list(ifilterfalse(isEven, g(s))), filter(isOdd, g(s)))
   1318             self.assertRaises(TypeError, ifilterfalse, isEven, X(s))
   1319             self.assertRaises(TypeError, list, ifilterfalse(isEven, N(s)))
   1320             self.assertRaises(ZeroDivisionError, list, ifilterfalse(isEven, E(s)))
   1321 
   1322     def test_izip(self):
   1323         for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
   1324             for g in (G, I, Ig, S, L, R):
   1325                 self.assertEqual(list(izip(g(s))), zip(g(s)))
   1326                 self.assertEqual(list(izip(g(s), g(s))), zip(g(s), g(s)))
   1327             self.assertRaises(TypeError, izip, X(s))
   1328             self.assertRaises(TypeError, list, izip(N(s)))
   1329             self.assertRaises(ZeroDivisionError, list, izip(E(s)))
   1330 
   1331     def test_iziplongest(self):
   1332         for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
   1333             for g in (G, I, Ig, S, L, R):
   1334                 self.assertEqual(list(izip_longest(g(s))), zip(g(s)))
   1335                 self.assertEqual(list(izip_longest(g(s), g(s))), zip(g(s), g(s)))
   1336             self.assertRaises(TypeError, izip_longest, X(s))
   1337             self.assertRaises(TypeError, list, izip_longest(N(s)))
   1338             self.assertRaises(ZeroDivisionError, list, izip_longest(E(s)))
   1339 
   1340     def test_imap(self):
   1341         for s in (range(10), range(0), range(100), (7,11), xrange(20,50,5)):
   1342             for g in (G, I, Ig, S, L, R):
   1343                 self.assertEqual(list(imap(onearg, g(s))), map(onearg, g(s)))
   1344                 self.assertEqual(list(imap(operator.pow, g(s), g(s))), map(operator.pow, g(s), g(s)))
   1345             self.assertRaises(TypeError, imap, onearg, X(s))
   1346             self.assertRaises(TypeError, list, imap(onearg, N(s)))
   1347             self.assertRaises(ZeroDivisionError, list, imap(onearg, E(s)))
   1348 
   1349     def test_islice(self):
   1350         for s in ("12345", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
   1351             for g in (G, I, Ig, S, L, R):
   1352                 self.assertEqual(list(islice(g(s),1,None,2)), list(g(s))[1::2])
   1353             self.assertRaises(TypeError, islice, X(s), 10)
   1354             self.assertRaises(TypeError, list, islice(N(s), 10))
   1355             self.assertRaises(ZeroDivisionError, list, islice(E(s), 10))
   1356 
   1357     def test_starmap(self):
   1358         for s in (range(10), range(0), range(100), (7,11), xrange(20,50,5)):
   1359             for g in (G, I, Ig, S, L, R):
   1360                 ss = zip(s, s)
   1361                 self.assertEqual(list(starmap(operator.pow, g(ss))), map(operator.pow, g(s), g(s)))
   1362             self.assertRaises(TypeError, starmap, operator.pow, X(ss))
   1363             self.assertRaises(TypeError, list, starmap(operator.pow, N(ss)))
   1364             self.assertRaises(ZeroDivisionError, list, starmap(operator.pow, E(ss)))
   1365 
   1366     def test_takewhile(self):
   1367         for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)):
   1368             for g in (G, I, Ig, S, L, R):
   1369                 tgt = []
   1370                 for elem in g(s):
   1371                     if not isEven(elem): break
   1372                     tgt.append(elem)
   1373                 self.assertEqual(list(takewhile(isEven, g(s))), tgt)
   1374             self.assertRaises(TypeError, takewhile, isEven, X(s))
   1375             self.assertRaises(TypeError, list, takewhile(isEven, N(s)))
   1376             self.assertRaises(ZeroDivisionError, list, takewhile(isEven, E(s)))
   1377 
   1378     def test_dropwhile(self):
   1379         for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)):
   1380             for g in (G, I, Ig, S, L, R):
   1381                 tgt = []
   1382                 for elem in g(s):
   1383                     if not tgt and isOdd(elem): continue
   1384                     tgt.append(elem)
   1385                 self.assertEqual(list(dropwhile(isOdd, g(s))), tgt)
   1386             self.assertRaises(TypeError, dropwhile, isOdd, X(s))
   1387             self.assertRaises(TypeError, list, dropwhile(isOdd, N(s)))
   1388             self.assertRaises(ZeroDivisionError, list, dropwhile(isOdd, E(s)))
   1389 
   1390     def test_tee(self):
   1391         for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
   1392             for g in (G, I, Ig, S, L, R):
   1393                 it1, it2 = tee(g(s))
   1394                 self.assertEqual(list(it1), list(g(s)))
   1395                 self.assertEqual(list(it2), list(g(s)))
   1396             self.assertRaises(TypeError, tee, X(s))
   1397             self.assertRaises(TypeError, list, tee(N(s))[0])
   1398             self.assertRaises(ZeroDivisionError, list, tee(E(s))[0])
   1399 
   1400 class LengthTransparency(unittest.TestCase):
   1401 
   1402     def test_repeat(self):
   1403         from test.test_iterlen import len
   1404         self.assertEqual(len(repeat(None, 50)), 50)
   1405         self.assertRaises(TypeError, len, repeat(None))
   1406 
   1407 class RegressionTests(unittest.TestCase):
   1408 
   1409     def test_sf_793826(self):
   1410         # Fix Armin Rigo's successful efforts to wreak havoc
   1411 
   1412         def mutatingtuple(tuple1, f, tuple2):
   1413             # this builds a tuple t which is a copy of tuple1,
   1414             # then calls f(t), then mutates t to be equal to tuple2
   1415             # (needs len(tuple1) == len(tuple2)).
   1416             def g(value, first=[1]):
   1417                 if first:
   1418                     del first[:]
   1419                     f(z.next())
   1420                 return value
   1421             items = list(tuple2)
   1422             items[1:1] = list(tuple1)
   1423             gen = imap(g, items)
   1424             z = izip(*[gen]*len(tuple1))
   1425             z.next()
   1426 
   1427         def f(t):
   1428             global T
   1429             T = t
   1430             first[:] = list(T)
   1431 
   1432         first = []
   1433         mutatingtuple((1,2,3), f, (4,5,6))
   1434         second = list(T)
   1435         self.assertEqual(first, second)
   1436 
   1437 
   1438     def test_sf_950057(self):
   1439         # Make sure that chain() and cycle() catch exceptions immediately
   1440         # rather than when shifting between input sources
   1441 
   1442         def gen1():
   1443             hist.append(0)
   1444             yield 1
   1445             hist.append(1)
   1446             raise AssertionError
   1447             hist.append(2)
   1448 
   1449         def gen2(x):
   1450             hist.append(3)
   1451             yield 2
   1452             hist.append(4)
   1453             if x:
   1454                 raise StopIteration
   1455 
   1456         hist = []
   1457         self.assertRaises(AssertionError, list, chain(gen1(), gen2(False)))
   1458         self.assertEqual(hist, [0,1])
   1459 
   1460         hist = []
   1461         self.assertRaises(AssertionError, list, chain(gen1(), gen2(True)))
   1462         self.assertEqual(hist, [0,1])
   1463 
   1464         hist = []
   1465         self.assertRaises(AssertionError, list, cycle(gen1()))
   1466         self.assertEqual(hist, [0,1])
   1467 
   1468 class SubclassWithKwargsTest(unittest.TestCase):
   1469     def test_keywords_in_subclass(self):
   1470         # count is not subclassable...
   1471         for cls in (repeat, izip, ifilter, ifilterfalse, chain, imap,
   1472                     starmap, islice, takewhile, dropwhile, cycle, compress):
   1473             class Subclass(cls):
   1474                 def __init__(self, newarg=None, *args):
   1475                     cls.__init__(self, *args)
   1476             try:
   1477                 Subclass(newarg=1)
   1478             except TypeError, err:
   1479                 # we expect type errors because of wrong argument count
   1480                 self.assertNotIn("does not take keyword arguments", err.args[0])
   1481 
   1482 
   1483 libreftest = """ Doctest for examples in the library reference: libitertools.tex
   1484 
   1485 
   1486 >>> amounts = [120.15, 764.05, 823.14]
   1487 >>> for checknum, amount in izip(count(1200), amounts):
   1488 ...     print 'Check %d is for $%.2f' % (checknum, amount)
   1489 ...
   1490 Check 1200 is for $120.15
   1491 Check 1201 is for $764.05
   1492 Check 1202 is for $823.14
   1493 
   1494 >>> import operator
   1495 >>> for cube in imap(operator.pow, xrange(1,4), repeat(3)):
   1496 ...    print cube
   1497 ...
   1498 1
   1499 8
   1500 27
   1501 
   1502 >>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele']
   1503 >>> for name in islice(reportlines, 3, None, 2):
   1504 ...    print name.title()
   1505 ...
   1506 Alex
   1507 Laura
   1508 Martin
   1509 Walter
   1510 Samuele
   1511 
   1512 >>> from operator import itemgetter
   1513 >>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3)
   1514 >>> di = sorted(sorted(d.iteritems()), key=itemgetter(1))
   1515 >>> for k, g in groupby(di, itemgetter(1)):
   1516 ...     print k, map(itemgetter(0), g)
   1517 ...
   1518 1 ['a', 'c', 'e']
   1519 2 ['b', 'd', 'f']
   1520 3 ['g']
   1521 
   1522 # Find runs of consecutive numbers using groupby.  The key to the solution
   1523 # is differencing with a range so that consecutive numbers all appear in
   1524 # same group.
   1525 >>> data = [ 1,  4,5,6, 10, 15,16,17,18, 22, 25,26,27,28]
   1526 >>> for k, g in groupby(enumerate(data), lambda t:t[0]-t[1]):
   1527 ...     print map(operator.itemgetter(1), g)
   1528 ...
   1529 [1]
   1530 [4, 5, 6]
   1531 [10]
   1532 [15, 16, 17, 18]
   1533 [22]
   1534 [25, 26, 27, 28]
   1535 
   1536 >>> def take(n, iterable):
   1537 ...     "Return first n items of the iterable as a list"
   1538 ...     return list(islice(iterable, n))
   1539 
   1540 >>> def enumerate(iterable, start=0):
   1541 ...     return izip(count(start), iterable)
   1542 
   1543 >>> def tabulate(function, start=0):
   1544 ...     "Return function(0), function(1), ..."
   1545 ...     return imap(function, count(start))
   1546 
   1547 >>> def nth(iterable, n, default=None):
   1548 ...     "Returns the nth item or a default value"
   1549 ...     return next(islice(iterable, n, None), default)
   1550 
   1551 >>> def all_equal(iterable):
   1552 ...     "Returns True if all the elements are equal to each other"
   1553 ...     g = groupby(iterable)
   1554 ...     return next(g, True) and not next(g, False)
   1555 
   1556 >>> def quantify(iterable, pred=bool):
   1557 ...     "Count how many times the predicate is true"
   1558 ...     return sum(imap(pred, iterable))
   1559 
   1560 >>> def padnone(iterable):
   1561 ...     "Returns the sequence elements and then returns None indefinitely"
   1562 ...     return chain(iterable, repeat(None))
   1563 
   1564 >>> def ncycles(iterable, n):
   1565 ...     "Returns the sequence elements n times"
   1566 ...     return chain(*repeat(iterable, n))
   1567 
   1568 >>> def dotproduct(vec1, vec2):
   1569 ...     return sum(imap(operator.mul, vec1, vec2))
   1570 
   1571 >>> def flatten(listOfLists):
   1572 ...     return list(chain.from_iterable(listOfLists))
   1573 
   1574 >>> def repeatfunc(func, times=None, *args):
   1575 ...     "Repeat calls to func with specified arguments."
   1576 ...     "   Example:  repeatfunc(random.random)"
   1577 ...     if times is None:
   1578 ...         return starmap(func, repeat(args))
   1579 ...     else:
   1580 ...         return starmap(func, repeat(args, times))
   1581 
   1582 >>> def pairwise(iterable):
   1583 ...     "s -> (s0,s1), (s1,s2), (s2, s3), ..."
   1584 ...     a, b = tee(iterable)
   1585 ...     for elem in b:
   1586 ...         break
   1587 ...     return izip(a, b)
   1588 
   1589 >>> def grouper(n, iterable, fillvalue=None):
   1590 ...     "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
   1591 ...     args = [iter(iterable)] * n
   1592 ...     return izip_longest(fillvalue=fillvalue, *args)
   1593 
   1594 >>> def roundrobin(*iterables):
   1595 ...     "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
   1596 ...     # Recipe credited to George Sakkis
   1597 ...     pending = len(iterables)
   1598 ...     nexts = cycle(iter(it).next for it in iterables)
   1599 ...     while pending:
   1600 ...         try:
   1601 ...             for next in nexts:
   1602 ...                 yield next()
   1603 ...         except StopIteration:
   1604 ...             pending -= 1
   1605 ...             nexts = cycle(islice(nexts, pending))
   1606 
   1607 >>> def powerset(iterable):
   1608 ...     "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
   1609 ...     s = list(iterable)
   1610 ...     return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
   1611 
   1612 >>> def unique_everseen(iterable, key=None):
   1613 ...     "List unique elements, preserving order. Remember all elements ever seen."
   1614 ...     # unique_everseen('AAAABBBCCDAABBB') --> A B C D
   1615 ...     # unique_everseen('ABBCcAD', str.lower) --> A B C D
   1616 ...     seen = set()
   1617 ...     seen_add = seen.add
   1618 ...     if key is None:
   1619 ...         for element in iterable:
   1620 ...             if element not in seen:
   1621 ...                 seen_add(element)
   1622 ...                 yield element
   1623 ...     else:
   1624 ...         for element in iterable:
   1625 ...             k = key(element)
   1626 ...             if k not in seen:
   1627 ...                 seen_add(k)
   1628 ...                 yield element
   1629 
   1630 >>> def unique_justseen(iterable, key=None):
   1631 ...     "List unique elements, preserving order. Remember only the element just seen."
   1632 ...     # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B
   1633 ...     # unique_justseen('ABBCcAD', str.lower) --> A B C A D
   1634 ...     return imap(next, imap(itemgetter(1), groupby(iterable, key)))
   1635 
   1636 This is not part of the examples but it tests to make sure the definitions
   1637 perform as purported.
   1638 
   1639 >>> take(10, count())
   1640 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
   1641 
   1642 >>> list(enumerate('abc'))
   1643 [(0, 'a'), (1, 'b'), (2, 'c')]
   1644 
   1645 >>> list(islice(tabulate(lambda x: 2*x), 4))
   1646 [0, 2, 4, 6]
   1647 
   1648 >>> nth('abcde', 3)
   1649 'd'
   1650 
   1651 >>> nth('abcde', 9) is None
   1652 True
   1653 
   1654 >>> [all_equal(s) for s in ('', 'A', 'AAAA', 'AAAB', 'AAABA')]
   1655 [True, True, True, False, False]
   1656 
   1657 >>> quantify(xrange(99), lambda x: x%2==0)
   1658 50
   1659 
   1660 >>> a = [[1, 2, 3], [4, 5, 6]]
   1661 >>> flatten(a)
   1662 [1, 2, 3, 4, 5, 6]
   1663 
   1664 >>> list(repeatfunc(pow, 5, 2, 3))
   1665 [8, 8, 8, 8, 8]
   1666 
   1667 >>> import random
   1668 >>> take(5, imap(int, repeatfunc(random.random)))
   1669 [0, 0, 0, 0, 0]
   1670 
   1671 >>> list(pairwise('abcd'))
   1672 [('a', 'b'), ('b', 'c'), ('c', 'd')]
   1673 
   1674 >>> list(pairwise([]))
   1675 []
   1676 
   1677 >>> list(pairwise('a'))
   1678 []
   1679 
   1680 >>> list(islice(padnone('abc'), 0, 6))
   1681 ['a', 'b', 'c', None, None, None]
   1682 
   1683 >>> list(ncycles('abc', 3))
   1684 ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
   1685 
   1686 >>> dotproduct([1,2,3], [4,5,6])
   1687 32
   1688 
   1689 >>> list(grouper(3, 'abcdefg', 'x'))
   1690 [('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')]
   1691 
   1692 >>> list(roundrobin('abc', 'd', 'ef'))
   1693 ['a', 'd', 'e', 'b', 'f', 'c']
   1694 
   1695 >>> list(powerset([1,2,3]))
   1696 [(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
   1697 
   1698 >>> all(len(list(powerset(range(n)))) == 2**n for n in range(18))
   1699 True
   1700 
   1701 >>> list(powerset('abcde')) == sorted(sorted(set(powerset('abcde'))), key=len)
   1702 True
   1703 
   1704 >>> list(unique_everseen('AAAABBBCCDAABBB'))
   1705 ['A', 'B', 'C', 'D']
   1706 
   1707 >>> list(unique_everseen('ABBCcAD', str.lower))
   1708 ['A', 'B', 'C', 'D']
   1709 
   1710 >>> list(unique_justseen('AAAABBBCCDAABBB'))
   1711 ['A', 'B', 'C', 'D', 'A', 'B']
   1712 
   1713 >>> list(unique_justseen('ABBCcAD', str.lower))
   1714 ['A', 'B', 'C', 'A', 'D']
   1715 
   1716 """
   1717 
   1718 __test__ = {'libreftest' : libreftest}
   1719 
   1720 def test_main(verbose=None):
   1721     test_classes = (TestBasicOps, TestVariousIteratorArgs, TestGC,
   1722                     RegressionTests, LengthTransparency,
   1723                     SubclassWithKwargsTest, TestExamples)
   1724     test_support.run_unittest(*test_classes)
   1725 
   1726     # verify reference counting
   1727     if verbose and hasattr(sys, "gettotalrefcount"):
   1728         import gc
   1729         counts = [None] * 5
   1730         for i in xrange(len(counts)):
   1731             test_support.run_unittest(*test_classes)
   1732             gc.collect()
   1733             counts[i] = sys.gettotalrefcount()
   1734         print counts
   1735 
   1736     # doctest the examples in the library reference
   1737     test_support.run_doctest(sys.modules[__name__], verbose)
   1738 
   1739 if __name__ == "__main__":
   1740     test_main(verbose=True)
   1741