Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_itertools.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | 3e27d606d625 |
| children | 6768b2350c57 |
| rev | line source |
|---|---|
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1 import unittest |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2 from test import test_support |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
3 from itertools import * |
|
33777
e4b7c923ed5f
Fix and test weak referencing of itertools.tee objects.
Raymond Hettinger <python@rcn.com>
parents:
33624
diff
changeset
|
4 from weakref import proxy |
|
51547
df69bccdd363
Add an extra testcase.
Raymond Hettinger <python@rcn.com>
parents:
51541
diff
changeset
|
5 from decimal import Decimal |
| 51549 | 6 from fractions import Fraction |
|
27747
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27574
diff
changeset
|
7 import sys |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
8 import operator |
| 30764 | 9 import random |
|
41589
37d82bee7a16
Fix those parts in the testsuite that assumed that sys.maxint would cause overflow on x64. Now the testsuite is well behaved on that platform.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
41133
diff
changeset
|
10 maxsize = test_support.MAX_Py_ssize_t |
|
37d82bee7a16
Fix those parts in the testsuite that assumed that sys.maxint would cause overflow on x64. Now the testsuite is well behaved on that platform.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
41133
diff
changeset
|
11 minsize = -maxsize-1 |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
12 |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
13 def onearg(x): |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
14 'Test function of one argument' |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
15 return 2*x |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
16 |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
17 def errfunc(*args): |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
18 'Test function that raises an error' |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
19 raise ValueError |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
20 |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
21 def gen3(): |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
22 'Non-restartable source sequence' |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
23 for i in (0, 1, 2): |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
24 yield i |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
25 |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
26 def isEven(x): |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
27 'Test predicate' |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
28 return x%2==0 |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
29 |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
30 def isOdd(x): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
31 'Test predicate' |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
32 return x%2==1 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
33 |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
34 class StopNow: |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
35 'Class emulating an empty iterable.' |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
36 def __iter__(self): |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
37 return self |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
38 def next(self): |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
39 raise StopIteration |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
40 |
| 29690 | 41 def take(n, seq): |
| 42 'Convenience function for partially consuming a long of infinite iterable' | |
| 43 return list(islice(seq, n)) | |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
44 |
|
45651
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
45 def prod(iterable): |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
46 return reduce(operator.mul, iterable, 1) |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
47 |
|
45539
ca45eb71f2d2
Add itertools.combinations().
Raymond Hettinger <python@rcn.com>
parents:
45524
diff
changeset
|
48 def fact(n): |
|
ca45eb71f2d2
Add itertools.combinations().
Raymond Hettinger <python@rcn.com>
parents:
45524
diff
changeset
|
49 'Factorial' |
|
45651
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
50 return prod(range(1, n+1)) |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
51 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
52 class TestBasicOps(unittest.TestCase): |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27769
diff
changeset
|
53 def test_chain(self): |
| 45698 | 54 |
| 55 def chain2(*iterables): | |
| 56 'Pure python version in the docs' | |
| 57 for it in iterables: | |
| 58 for element in it: | |
| 59 yield element | |
| 60 | |
| 61 for c in (chain, chain2): | |
| 62 self.assertEqual(list(c('abc', 'def')), list('abcdef')) | |
| 63 self.assertEqual(list(c('abc')), list('abc')) | |
| 64 self.assertEqual(list(c('')), []) | |
| 65 self.assertEqual(take(4, c('abc', 'def')), list('abcd')) | |
| 66 self.assertRaises(TypeError, list,c(2, 3)) | |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27769
diff
changeset
|
67 |
|
45563
7da36b49c9e5
Add alternate constructor for itertools.chain().
Raymond Hettinger <python@rcn.com>
parents:
45562
diff
changeset
|
68 def test_chain_from_iterable(self): |
|
7da36b49c9e5
Add alternate constructor for itertools.chain().
Raymond Hettinger <python@rcn.com>
parents:
45562
diff
changeset
|
69 self.assertEqual(list(chain.from_iterable(['abc', 'def'])), list('abcdef')) |
|
7da36b49c9e5
Add alternate constructor for itertools.chain().
Raymond Hettinger <python@rcn.com>
parents:
45562
diff
changeset
|
70 self.assertEqual(list(chain.from_iterable(['abc'])), list('abc')) |
|
7da36b49c9e5
Add alternate constructor for itertools.chain().
Raymond Hettinger <python@rcn.com>
parents:
45562
diff
changeset
|
71 self.assertEqual(list(chain.from_iterable([''])), []) |
|
7da36b49c9e5
Add alternate constructor for itertools.chain().
Raymond Hettinger <python@rcn.com>
parents:
45562
diff
changeset
|
72 self.assertEqual(take(4, chain.from_iterable(['abc', 'def'])), list('abcd')) |
|
7da36b49c9e5
Add alternate constructor for itertools.chain().
Raymond Hettinger <python@rcn.com>
parents:
45562
diff
changeset
|
73 self.assertRaises(TypeError, list, chain.from_iterable([2, 3])) |
|
7da36b49c9e5
Add alternate constructor for itertools.chain().
Raymond Hettinger <python@rcn.com>
parents:
45562
diff
changeset
|
74 |
|
45539
ca45eb71f2d2
Add itertools.combinations().
Raymond Hettinger <python@rcn.com>
parents:
45524
diff
changeset
|
75 def test_combinations(self): |
|
50631
76fa1de2742a
Forward port r68394 for issue 4816.
Raymond Hettinger <python@rcn.com>
parents:
50467
diff
changeset
|
76 self.assertRaises(TypeError, combinations, 'abc') # missing r argument |
|
45539
ca45eb71f2d2
Add itertools.combinations().
Raymond Hettinger <python@rcn.com>
parents:
45524
diff
changeset
|
77 self.assertRaises(TypeError, combinations, 'abc', 2, 1) # too many arguments |
|
45651
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
78 self.assertRaises(TypeError, combinations, None) # pool is not iterable |
|
45539
ca45eb71f2d2
Add itertools.combinations().
Raymond Hettinger <python@rcn.com>
parents:
45524
diff
changeset
|
79 self.assertRaises(ValueError, combinations, 'abc', -2) # r is negative |
|
50631
76fa1de2742a
Forward port r68394 for issue 4816.
Raymond Hettinger <python@rcn.com>
parents:
50467
diff
changeset
|
80 self.assertEqual(list(combinations('abc', 32)), []) # r > n |
|
45539
ca45eb71f2d2
Add itertools.combinations().
Raymond Hettinger <python@rcn.com>
parents:
45524
diff
changeset
|
81 self.assertEqual(list(combinations(range(4), 3)), |
|
ca45eb71f2d2
Add itertools.combinations().
Raymond Hettinger <python@rcn.com>
parents:
45524
diff
changeset
|
82 [(0,1,2), (0,1,3), (0,2,3), (1,2,3)]) |
|
45651
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
83 |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
84 def combinations1(iterable, r): |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
85 'Pure python version shown in the docs' |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
86 pool = tuple(iterable) |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
87 n = len(pool) |
|
50631
76fa1de2742a
Forward port r68394 for issue 4816.
Raymond Hettinger <python@rcn.com>
parents:
50467
diff
changeset
|
88 if r > n: |
|
76fa1de2742a
Forward port r68394 for issue 4816.
Raymond Hettinger <python@rcn.com>
parents:
50467
diff
changeset
|
89 return |
|
45651
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
90 indices = range(r) |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
91 yield tuple(pool[i] for i in indices) |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
92 while 1: |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
93 for i in reversed(range(r)): |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
94 if indices[i] != i + n - r: |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
95 break |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
96 else: |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
97 return |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
98 indices[i] += 1 |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
99 for j in range(i+1, r): |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
100 indices[j] = indices[j-1] + 1 |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
101 yield tuple(pool[i] for i in indices) |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
102 |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
103 def combinations2(iterable, r): |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
104 'Pure python version shown in the docs' |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
105 pool = tuple(iterable) |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
106 n = len(pool) |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
107 for indices in permutations(range(n), r): |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
108 if sorted(indices) == list(indices): |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
109 yield tuple(pool[i] for i in indices) |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
110 |
|
51106
d170fdf4bdfb
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51097
diff
changeset
|
111 def combinations3(iterable, r): |
|
d170fdf4bdfb
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51097
diff
changeset
|
112 'Pure python version from cwr()' |
|
d170fdf4bdfb
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51097
diff
changeset
|
113 pool = tuple(iterable) |
|
d170fdf4bdfb
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51097
diff
changeset
|
114 n = len(pool) |
|
d170fdf4bdfb
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51097
diff
changeset
|
115 for indices in combinations_with_replacement(range(n), r): |
|
d170fdf4bdfb
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51097
diff
changeset
|
116 if len(set(indices)) == r: |
|
d170fdf4bdfb
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51097
diff
changeset
|
117 yield tuple(pool[i] for i in indices) |
|
d170fdf4bdfb
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51097
diff
changeset
|
118 |
|
45651
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
119 for n in range(7): |
|
45539
ca45eb71f2d2
Add itertools.combinations().
Raymond Hettinger <python@rcn.com>
parents:
45524
diff
changeset
|
120 values = [5*x-12 for x in range(n)] |
|
50631
76fa1de2742a
Forward port r68394 for issue 4816.
Raymond Hettinger <python@rcn.com>
parents:
50467
diff
changeset
|
121 for r in range(n+2): |
|
45539
ca45eb71f2d2
Add itertools.combinations().
Raymond Hettinger <python@rcn.com>
parents:
45524
diff
changeset
|
122 result = list(combinations(values, r)) |
|
50631
76fa1de2742a
Forward port r68394 for issue 4816.
Raymond Hettinger <python@rcn.com>
parents:
50467
diff
changeset
|
123 self.assertEqual(len(result), 0 if r>n else fact(n) / fact(r) / fact(n-r)) # right number of combs |
|
45539
ca45eb71f2d2
Add itertools.combinations().
Raymond Hettinger <python@rcn.com>
parents:
45524
diff
changeset
|
124 self.assertEqual(len(result), len(set(result))) # no repeats |
|
ca45eb71f2d2
Add itertools.combinations().
Raymond Hettinger <python@rcn.com>
parents:
45524
diff
changeset
|
125 self.assertEqual(result, sorted(result)) # lexicographic order |
|
ca45eb71f2d2
Add itertools.combinations().
Raymond Hettinger <python@rcn.com>
parents:
45524
diff
changeset
|
126 for c in result: |
|
ca45eb71f2d2
Add itertools.combinations().
Raymond Hettinger <python@rcn.com>
parents:
45524
diff
changeset
|
127 self.assertEqual(len(c), r) # r-length combinations |
|
ca45eb71f2d2
Add itertools.combinations().
Raymond Hettinger <python@rcn.com>
parents:
45524
diff
changeset
|
128 self.assertEqual(len(set(c)), r) # no duplicate elements |
|
ca45eb71f2d2
Add itertools.combinations().
Raymond Hettinger <python@rcn.com>
parents:
45524
diff
changeset
|
129 self.assertEqual(list(c), sorted(c)) # keep original ordering |
|
ca45eb71f2d2
Add itertools.combinations().
Raymond Hettinger <python@rcn.com>
parents:
45524
diff
changeset
|
130 self.assert_(all(e in values for e in c)) # elements taken from input iterable |
|
45675
c197e18449b4
C implementation of itertools.permutations().
Raymond Hettinger <python@rcn.com>
parents:
45651
diff
changeset
|
131 self.assertEqual(list(c), |
|
c197e18449b4
C implementation of itertools.permutations().
Raymond Hettinger <python@rcn.com>
parents:
45651
diff
changeset
|
132 [e for e in values if e in c]) # comb is a subsequence of the input iterable |
|
45651
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
133 self.assertEqual(result, list(combinations1(values, r))) # matches first pure python version |
|
50631
76fa1de2742a
Forward port r68394 for issue 4816.
Raymond Hettinger <python@rcn.com>
parents:
50467
diff
changeset
|
134 self.assertEqual(result, list(combinations2(values, r))) # matches second pure python version |
|
51106
d170fdf4bdfb
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51097
diff
changeset
|
135 self.assertEqual(result, list(combinations3(values, r))) # matches second pure python version |
|
45651
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
136 |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
137 # Test implementation detail: tuple re-use |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
138 self.assertEqual(len(set(map(id, combinations('abcde', 3)))), 1) |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
139 self.assertNotEqual(len(set(map(id, list(combinations('abcde', 3))))), 1) |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
140 |
|
51097
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
141 def test_combinations_with_replacement(self): |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
142 cwr = combinations_with_replacement |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
143 self.assertRaises(TypeError, cwr, 'abc') # missing r argument |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
144 self.assertRaises(TypeError, cwr, 'abc', 2, 1) # too many arguments |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
145 self.assertRaises(TypeError, cwr, None) # pool is not iterable |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
146 self.assertRaises(ValueError, cwr, 'abc', -2) # r is negative |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
147 self.assertEqual(list(cwr('ABC', 2)), |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
148 [('A','A'), ('A','B'), ('A','C'), ('B','B'), ('B','C'), ('C','C')]) |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
149 |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
150 def cwr1(iterable, r): |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
151 'Pure python version shown in the docs' |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
152 # number items returned: (n+r-1)! / r! / (n-1)! when n>0 |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
153 pool = tuple(iterable) |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
154 n = len(pool) |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
155 if not n and r: |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
156 return |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
157 indices = [0] * r |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
158 yield tuple(pool[i] for i in indices) |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
159 while 1: |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
160 for i in reversed(range(r)): |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
161 if indices[i] != n - 1: |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
162 break |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
163 else: |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
164 return |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
165 indices[i:] = [indices[i] + 1] * (r - i) |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
166 yield tuple(pool[i] for i in indices) |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
167 |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
168 def cwr2(iterable, r): |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
169 'Pure python version shown in the docs' |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
170 pool = tuple(iterable) |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
171 n = len(pool) |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
172 for indices in product(range(n), repeat=r): |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
173 if sorted(indices) == list(indices): |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
174 yield tuple(pool[i] for i in indices) |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
175 |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
176 def numcombs(n, r): |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
177 if not n: |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
178 return 0 if r else 1 |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
179 return fact(n+r-1) / fact(r)/ fact(n-1) |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
180 |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
181 for n in range(7): |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
182 values = [5*x-12 for x in range(n)] |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
183 for r in range(n+2): |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
184 result = list(cwr(values, r)) |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
185 |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
186 self.assertEqual(len(result), numcombs(n, r)) # right number of combs |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
187 self.assertEqual(len(result), len(set(result))) # no repeats |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
188 self.assertEqual(result, sorted(result)) # lexicographic order |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
189 |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
190 regular_combs = list(combinations(values, r)) # compare to combs without replacement |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
191 if n == 0 or r <= 1: |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
192 self.assertEquals(result, regular_combs) # cases that should be identical |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
193 else: |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
194 self.assert_(set(result) >= set(regular_combs)) # rest should be supersets of regular combs |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
195 |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
196 for c in result: |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
197 self.assertEqual(len(c), r) # r-length combinations |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
198 noruns = [k for k,v in groupby(c)] # combo without consecutive repeats |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
199 self.assertEqual(len(noruns), len(set(noruns))) # no repeats other than consecutive |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
200 self.assertEqual(list(c), sorted(c)) # keep original ordering |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
201 self.assert_(all(e in values for e in c)) # elements taken from input iterable |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
202 self.assertEqual(noruns, |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
203 [e for e in values if e in c]) # comb is a subsequence of the input iterable |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
204 self.assertEqual(result, list(cwr1(values, r))) # matches first pure python version |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
205 self.assertEqual(result, list(cwr2(values, r))) # matches second pure python version |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
206 |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
207 # Test implementation detail: tuple re-use |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
208 self.assertEqual(len(set(map(id, cwr('abcde', 3)))), 1) |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
209 self.assertNotEqual(len(set(map(id, list(cwr('abcde', 3))))), 1) |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
210 |
|
45651
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
211 def test_permutations(self): |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
212 self.assertRaises(TypeError, permutations) # too few arguments |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
213 self.assertRaises(TypeError, permutations, 'abc', 2, 1) # too many arguments |
|
45675
c197e18449b4
C implementation of itertools.permutations().
Raymond Hettinger <python@rcn.com>
parents:
45651
diff
changeset
|
214 self.assertRaises(TypeError, permutations, None) # pool is not iterable |
|
c197e18449b4
C implementation of itertools.permutations().
Raymond Hettinger <python@rcn.com>
parents:
45651
diff
changeset
|
215 self.assertRaises(ValueError, permutations, 'abc', -2) # r is negative |
|
50631
76fa1de2742a
Forward port r68394 for issue 4816.
Raymond Hettinger <python@rcn.com>
parents:
50467
diff
changeset
|
216 self.assertEqual(list(permutations('abc', 32)), []) # r > n |
|
45675
c197e18449b4
C implementation of itertools.permutations().
Raymond Hettinger <python@rcn.com>
parents:
45651
diff
changeset
|
217 self.assertRaises(TypeError, permutations, 'abc', 's') # r is not an int or None |
|
45651
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
218 self.assertEqual(list(permutations(range(3), 2)), |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
219 [(0,1), (0,2), (1,0), (1,2), (2,0), (2,1)]) |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
220 |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
221 def permutations1(iterable, r=None): |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
222 'Pure python version shown in the docs' |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
223 pool = tuple(iterable) |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
224 n = len(pool) |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
225 r = n if r is None else r |
|
50631
76fa1de2742a
Forward port r68394 for issue 4816.
Raymond Hettinger <python@rcn.com>
parents:
50467
diff
changeset
|
226 if r > n: |
|
76fa1de2742a
Forward port r68394 for issue 4816.
Raymond Hettinger <python@rcn.com>
parents:
50467
diff
changeset
|
227 return |
|
45651
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
228 indices = range(n) |
| 46017 | 229 cycles = range(n, n-r, -1) |
|
45651
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
230 yield tuple(pool[i] for i in indices[:r]) |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
231 while n: |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
232 for i in reversed(range(r)): |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
233 cycles[i] -= 1 |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
234 if cycles[i] == 0: |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
235 indices[i:] = indices[i+1:] + indices[i:i+1] |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
236 cycles[i] = n - i |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
237 else: |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
238 j = cycles[i] |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
239 indices[i], indices[-j] = indices[-j], indices[i] |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
240 yield tuple(pool[i] for i in indices[:r]) |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
241 break |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
242 else: |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
243 return |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
244 |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
245 def permutations2(iterable, r=None): |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
246 'Pure python version shown in the docs' |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
247 pool = tuple(iterable) |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
248 n = len(pool) |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
249 r = n if r is None else r |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
250 for indices in product(range(n), repeat=r): |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
251 if len(set(indices)) == r: |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
252 yield tuple(pool[i] for i in indices) |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
253 |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
254 for n in range(7): |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
255 values = [5*x-12 for x in range(n)] |
|
50631
76fa1de2742a
Forward port r68394 for issue 4816.
Raymond Hettinger <python@rcn.com>
parents:
50467
diff
changeset
|
256 for r in range(n+2): |
|
45651
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
257 result = list(permutations(values, r)) |
|
50631
76fa1de2742a
Forward port r68394 for issue 4816.
Raymond Hettinger <python@rcn.com>
parents:
50467
diff
changeset
|
258 self.assertEqual(len(result), 0 if r>n else fact(n) / fact(n-r)) # right number of perms |
|
45651
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
259 self.assertEqual(len(result), len(set(result))) # no repeats |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
260 self.assertEqual(result, sorted(result)) # lexicographic order |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
261 for p in result: |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
262 self.assertEqual(len(p), r) # r-length permutations |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
263 self.assertEqual(len(set(p)), r) # no duplicate elements |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
264 self.assert_(all(e in values for e in p)) # elements taken from input iterable |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
265 self.assertEqual(result, list(permutations1(values, r))) # matches first pure python version |
|
50631
76fa1de2742a
Forward port r68394 for issue 4816.
Raymond Hettinger <python@rcn.com>
parents:
50467
diff
changeset
|
266 self.assertEqual(result, list(permutations2(values, r))) # matches second pure python version |
|
45651
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
267 if r == n: |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
268 self.assertEqual(result, list(permutations(values, None))) # test r as None |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
269 self.assertEqual(result, list(permutations(values))) # test default r |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
270 |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
271 # Test implementation detail: tuple re-use |
|
45675
c197e18449b4
C implementation of itertools.permutations().
Raymond Hettinger <python@rcn.com>
parents:
45651
diff
changeset
|
272 self.assertEqual(len(set(map(id, permutations('abcde', 3)))), 1) |
|
45651
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
273 self.assertNotEqual(len(set(map(id, list(permutations('abcde', 3))))), 1) |
|
45539
ca45eb71f2d2
Add itertools.combinations().
Raymond Hettinger <python@rcn.com>
parents:
45524
diff
changeset
|
274 |
|
51106
d170fdf4bdfb
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51097
diff
changeset
|
275 def test_combinatorics(self): |
|
d170fdf4bdfb
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51097
diff
changeset
|
276 # Test relationships between product(), permutations(), |
|
d170fdf4bdfb
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51097
diff
changeset
|
277 # combinations() and combinations_with_replacement(). |
|
d170fdf4bdfb
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51097
diff
changeset
|
278 |
|
51114
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
279 for n in range(6): |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
280 s = 'ABCDEFG'[:n] |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
281 for r in range(8): |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
282 prod = list(product(s, repeat=r)) |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
283 cwr = list(combinations_with_replacement(s, r)) |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
284 perm = list(permutations(s, r)) |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
285 comb = list(combinations(s, r)) |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
286 |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
287 # Check size |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
288 self.assertEquals(len(prod), n**r) |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
289 self.assertEquals(len(cwr), (fact(n+r-1) / fact(r)/ fact(n-1)) if n else (not r)) |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
290 self.assertEquals(len(perm), 0 if r>n else fact(n) / fact(n-r)) |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
291 self.assertEquals(len(comb), 0 if r>n else fact(n) / fact(r) / fact(n-r)) |
|
51106
d170fdf4bdfb
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51097
diff
changeset
|
292 |
|
51114
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
293 # Check lexicographic order without repeated tuples |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
294 self.assertEquals(prod, sorted(set(prod))) |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
295 self.assertEquals(cwr, sorted(set(cwr))) |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
296 self.assertEquals(perm, sorted(set(perm))) |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
297 self.assertEquals(comb, sorted(set(comb))) |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
298 |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
299 # Check interrelationships |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
300 self.assertEquals(cwr, [t for t in prod if sorted(t)==list(t)]) # cwr: prods which are sorted |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
301 self.assertEquals(perm, [t for t in prod if len(set(t))==r]) # perm: prods with no dups |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
302 self.assertEqual(comb, [t for t in perm if sorted(t)==list(t)]) # comb: perms that are sorted |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
303 self.assertEqual(comb, [t for t in cwr if len(set(t))==r]) # comb: cwrs without dups |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
304 self.assertEqual(comb, filter(set(cwr).__contains__, perm)) # comb: perm that is a cwr |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
305 self.assertEqual(comb, filter(set(perm).__contains__, cwr)) # comb: cwr that is a perm |
|
2254cd3200f0
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51108
diff
changeset
|
306 self.assertEqual(comb, sorted(set(cwr) & set(perm))) # comb: both a cwr and a perm |
|
51106
d170fdf4bdfb
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51097
diff
changeset
|
307 |
|
51045
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
308 def test_compress(self): |
|
51715
748dfe9bb857
Add keyword arg support to itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51660
diff
changeset
|
309 self.assertEqual(list(compress(data='ABCDEF', selectors=[1,0,1,0,1,1])), list('ACEF')) |
|
51045
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
310 self.assertEqual(list(compress('ABCDEF', [1,0,1,0,1,1])), list('ACEF')) |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
311 self.assertEqual(list(compress('ABCDEF', [0,0,0,0,0,0])), list('')) |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
312 self.assertEqual(list(compress('ABCDEF', [1,1,1,1,1,1])), list('ABCDEF')) |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
313 self.assertEqual(list(compress('ABCDEF', [1,0,1])), list('AC')) |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
314 self.assertEqual(list(compress('ABC', [0,1,1,1,1,1])), list('BC')) |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
315 n = 10000 |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
316 data = chain.from_iterable(repeat(range(6), n)) |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
317 selectors = chain.from_iterable(repeat((0, 1))) |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
318 self.assertEqual(list(compress(data, selectors)), [1,3,5] * n) |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
319 self.assertRaises(TypeError, compress, None, range(6)) # 1st arg not iterable |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
320 self.assertRaises(TypeError, compress, range(6), None) # 2nd arg not iterable |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
321 self.assertRaises(TypeError, compress, range(6)) # too few args |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
322 self.assertRaises(TypeError, compress, range(6), None) # too many args |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
323 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
324 def test_count(self): |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
325 self.assertEqual(zip('abc',count()), [('a', 0), ('b', 1), ('c', 2)]) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
326 self.assertEqual(zip('abc',count(3)), [('a', 3), ('b', 4), ('c', 5)]) |
| 29690 | 327 self.assertEqual(take(2, zip('abc',count(3))), [('a', 3), ('b', 4)]) |
|
43458
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
41589
diff
changeset
|
328 self.assertEqual(take(2, zip('abc',count(-1))), [('a', -1), ('b', 0)]) |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
41589
diff
changeset
|
329 self.assertEqual(take(2, zip('abc',count(-3))), [('a', -3), ('b', -2)]) |
|
51541
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
330 self.assertRaises(TypeError, count, 2, 3, 4) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
331 self.assertRaises(TypeError, count, 'a') |
|
43458
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
41589
diff
changeset
|
332 self.assertEqual(list(islice(count(maxsize-5), 10)), range(maxsize-5, maxsize+5)) |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
41589
diff
changeset
|
333 self.assertEqual(list(islice(count(-maxsize-5), 10)), range(-maxsize-5, -maxsize+5)) |
|
33616
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
334 c = count(3) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
335 self.assertEqual(repr(c), 'count(3)') |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
336 c.next() |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
337 self.assertEqual(repr(c), 'count(4)') |
|
39866
69530aa21b8a
* regression bug, count_next was coercing a Py_ssize_t to an unsigned Py_size_t
Jack Diederich <jackdied@gmail.com>
parents:
39720
diff
changeset
|
338 c = count(-9) |
|
69530aa21b8a
* regression bug, count_next was coercing a Py_ssize_t to an unsigned Py_size_t
Jack Diederich <jackdied@gmail.com>
parents:
39720
diff
changeset
|
339 self.assertEqual(repr(c), 'count(-9)') |
|
69530aa21b8a
* regression bug, count_next was coercing a Py_ssize_t to an unsigned Py_size_t
Jack Diederich <jackdied@gmail.com>
parents:
39720
diff
changeset
|
340 c.next() |
|
51541
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
341 self.assertEqual(repr(count(10.25)), 'count(10.25)') |
|
39866
69530aa21b8a
* regression bug, count_next was coercing a Py_ssize_t to an unsigned Py_size_t
Jack Diederich <jackdied@gmail.com>
parents:
39720
diff
changeset
|
342 self.assertEqual(c.next(), -8) |
|
43458
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
41589
diff
changeset
|
343 for i in (-sys.maxint-5, -sys.maxint+5 ,-10, -1, 0, 10, sys.maxint-5, sys.maxint+5): |
|
43543
fb310b8620ec
Fix test of count.__repr__() to ignore the 'L' if the count is a long
Raymond Hettinger <python@rcn.com>
parents:
43458
diff
changeset
|
344 # Test repr (ignoring the L in longs) |
|
fb310b8620ec
Fix test of count.__repr__() to ignore the 'L' if the count is a long
Raymond Hettinger <python@rcn.com>
parents:
43458
diff
changeset
|
345 r1 = repr(count(i)).replace('L', '') |
|
fb310b8620ec
Fix test of count.__repr__() to ignore the 'L' if the count is a long
Raymond Hettinger <python@rcn.com>
parents:
43458
diff
changeset
|
346 r2 = 'count(%r)'.__mod__(i).replace('L', '') |
|
fb310b8620ec
Fix test of count.__repr__() to ignore the 'L' if the count is a long
Raymond Hettinger <python@rcn.com>
parents:
43458
diff
changeset
|
347 self.assertEqual(r1, r2) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
348 |
|
51541
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
349 def test_count_with_stride(self): |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
350 self.assertEqual(zip('abc',count(2,3)), [('a', 2), ('b', 5), ('c', 8)]) |
|
51622
c5607cf76801
Add keyword argument support to itertools.count().
Raymond Hettinger <python@rcn.com>
parents:
51549
diff
changeset
|
351 self.assertEqual(zip('abc',count(start=2,step=3)), |
|
c5607cf76801
Add keyword argument support to itertools.count().
Raymond Hettinger <python@rcn.com>
parents:
51549
diff
changeset
|
352 [('a', 2), ('b', 5), ('c', 8)]) |
|
51772
3e27d606d625
Fix keyword arguments for itertools.count().
Raymond Hettinger <python@rcn.com>
parents:
51724
diff
changeset
|
353 self.assertEqual(zip('abc',count(step=-1)), |
|
3e27d606d625
Fix keyword arguments for itertools.count().
Raymond Hettinger <python@rcn.com>
parents:
51724
diff
changeset
|
354 [('a', 0), ('b', -1), ('c', -2)]) |
|
51541
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
355 self.assertEqual(zip('abc',count(2,0)), [('a', 2), ('b', 2), ('c', 2)]) |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
356 self.assertEqual(zip('abc',count(2,1)), [('a', 2), ('b', 3), ('c', 4)]) |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
357 self.assertEqual(take(20, count(maxsize-15, 3)), take(20, range(maxsize-15, maxsize+100, 3))) |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
358 self.assertEqual(take(20, count(-maxsize-15, 3)), take(20, range(-maxsize-15,-maxsize+100, 3))) |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
359 self.assertEqual(take(3, count(2, 3.25-4j)), [2, 5.25-4j, 8.5-8j]) |
|
51547
df69bccdd363
Add an extra testcase.
Raymond Hettinger <python@rcn.com>
parents:
51541
diff
changeset
|
360 self.assertEqual(take(3, count(Decimal('1.1'), Decimal('.1'))), |
|
df69bccdd363
Add an extra testcase.
Raymond Hettinger <python@rcn.com>
parents:
51541
diff
changeset
|
361 [Decimal('1.1'), Decimal('1.2'), Decimal('1.3')]) |
| 51549 | 362 self.assertEqual(take(3, count(Fraction(2,3), Fraction(1,7))), |
| 363 [Fraction(2,3), Fraction(17,21), Fraction(20,21)]) | |
|
51541
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
364 self.assertEqual(repr(take(3, count(10, 2.5))), repr([10, 12.5, 15.0])) |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
365 c = count(3, 5) |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
366 self.assertEqual(repr(c), 'count(3, 5)') |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
367 c.next() |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
368 self.assertEqual(repr(c), 'count(8, 5)') |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
369 c = count(-9, 0) |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
370 self.assertEqual(repr(c), 'count(-9, 0)') |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
371 c.next() |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
372 self.assertEqual(repr(c), 'count(-9, 0)') |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
373 c = count(-9, -3) |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
374 self.assertEqual(repr(c), 'count(-9, -3)') |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
375 c.next() |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
376 self.assertEqual(repr(c), 'count(-12, -3)') |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
377 self.assertEqual(repr(c), 'count(-12, -3)') |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
378 self.assertEqual(repr(count(10.5, 1.25)), 'count(10.5, 1.25)') |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
379 self.assertEqual(repr(count(10.5, 1)), 'count(10.5)') # suppress step=1 when it's an int |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
380 self.assertEqual(repr(count(10.5, 1.00)), 'count(10.5, 1.0)') # do show float values lilke 1.0 |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
381 for i in (-sys.maxint-5, -sys.maxint+5 ,-10, -1, 0, 10, sys.maxint-5, sys.maxint+5): |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
382 for j in (-sys.maxint-5, -sys.maxint+5 ,-10, -1, 0, 1, 10, sys.maxint-5, sys.maxint+5): |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
383 # Test repr (ignoring the L in longs) |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
384 r1 = repr(count(i, j)).replace('L', '') |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
385 if j == 1: |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
386 r2 = ('count(%r)' % i).replace('L', '') |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
387 else: |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
388 r2 = ('count(%r, %r)' % (i, j)).replace('L', '') |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
389 self.assertEqual(r1, r2) |
|
cbd53ff49557
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51312
diff
changeset
|
390 |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27769
diff
changeset
|
391 def test_cycle(self): |
| 29690 | 392 self.assertEqual(take(10, cycle('abc')), list('abcabcabca')) |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27769
diff
changeset
|
393 self.assertEqual(list(cycle('')), []) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
394 self.assertRaises(TypeError, cycle) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
395 self.assertRaises(TypeError, cycle, 5) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
396 self.assertEqual(list(islice(cycle(gen3()),10)), [0,1,2,0,1,2,0,1,2,0]) |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27769
diff
changeset
|
397 |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
398 def test_groupby(self): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
399 # Check whether it accepts arguments correctly |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
400 self.assertEqual([], list(groupby([]))) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
401 self.assertEqual([], list(groupby([], key=id))) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
402 self.assertRaises(TypeError, list, groupby('abc', [])) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
403 self.assertRaises(TypeError, groupby, None) |
|
33616
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
404 self.assertRaises(TypeError, groupby, 'abc', lambda x:x, 10) |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
405 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
406 # Check normal input |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
407 s = [(0, 10, 20), (0, 11,21), (0,12,21), (1,13,21), (1,14,22), |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
408 (2,15,22), (3,16,23), (3,17,23)] |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
409 dup = [] |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
410 for k, g in groupby(s, lambda r:r[0]): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
411 for elem in g: |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
412 self.assertEqual(k, elem[0]) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
413 dup.append(elem) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
414 self.assertEqual(s, dup) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
415 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
416 # Check nested case |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
417 dup = [] |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
418 for k, g in groupby(s, lambda r:r[0]): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
419 for ik, ig in groupby(g, lambda r:r[2]): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
420 for elem in ig: |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
421 self.assertEqual(k, elem[0]) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
422 self.assertEqual(ik, elem[2]) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
423 dup.append(elem) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
424 self.assertEqual(s, dup) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
425 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
426 # Check case where inner iterator is not used |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
427 keys = [k for k, g in groupby(s, lambda r:r[0])] |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
428 expectedkeys = set([r[0] for r in s]) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
429 self.assertEqual(set(keys), expectedkeys) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
430 self.assertEqual(len(keys), len(expectedkeys)) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
431 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
432 # Exercise pipes and filters style |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
433 s = 'abracadabra' |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
434 # sort s | uniq |
|
31210
c06b570adf12
Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents:
31136
diff
changeset
|
435 r = [k for k, g in groupby(sorted(s))] |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
436 self.assertEqual(r, ['a', 'b', 'c', 'd', 'r']) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
437 # sort s | uniq -d |
|
31210
c06b570adf12
Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents:
31136
diff
changeset
|
438 r = [k for k, g in groupby(sorted(s)) if list(islice(g,1,2))] |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
439 self.assertEqual(r, ['a', 'b', 'r']) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
440 # sort s | uniq -c |
|
31210
c06b570adf12
Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents:
31136
diff
changeset
|
441 r = [(len(list(g)), k) for k, g in groupby(sorted(s))] |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
442 self.assertEqual(r, [(5, 'a'), (2, 'b'), (1, 'c'), (1, 'd'), (2, 'r')]) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
443 # sort s | uniq -c | sort -rn | head -3 |
|
31210
c06b570adf12
Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents:
31136
diff
changeset
|
444 r = sorted([(len(list(g)) , k) for k, g in groupby(sorted(s))], reverse=True)[:3] |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
445 self.assertEqual(r, [(5, 'a'), (2, 'r'), (2, 'b')]) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
446 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
447 # iter.next failure |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
448 class ExpectedError(Exception): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
449 pass |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
450 def delayed_raise(n=0): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
451 for i in range(n): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
452 yield 'yo' |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
453 raise ExpectedError |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
454 def gulp(iterable, keyp=None, func=list): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
455 return [func(g) for k, g in groupby(iterable, keyp)] |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
456 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
457 # iter.next failure on outer object |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
458 self.assertRaises(ExpectedError, gulp, delayed_raise(0)) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
459 # iter.next failure on inner object |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
460 self.assertRaises(ExpectedError, gulp, delayed_raise(1)) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
461 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
462 # __cmp__ failure |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
463 class DummyCmp: |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
464 def __cmp__(self, dst): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
465 raise ExpectedError |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
466 s = [DummyCmp(), DummyCmp(), None] |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
467 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
468 # __cmp__ failure on outer object |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
469 self.assertRaises(ExpectedError, gulp, s, func=id) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
470 # __cmp__ failure on inner object |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
471 self.assertRaises(ExpectedError, gulp, s) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
472 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
473 # keyfunc failure |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
474 def keyfunc(obj): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
475 if keyfunc.skip > 0: |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
476 keyfunc.skip -= 1 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
477 return obj |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
478 else: |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
479 raise ExpectedError |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
480 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
481 # keyfunc failure on outer object |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
482 keyfunc.skip = 0 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
483 self.assertRaises(ExpectedError, gulp, [None], keyfunc) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
484 keyfunc.skip = 1 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
485 self.assertRaises(ExpectedError, gulp, [None, None], keyfunc) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
486 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
487 def test_ifilter(self): |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
488 self.assertEqual(list(ifilter(isEven, range(6))), [0,2,4]) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
489 self.assertEqual(list(ifilter(None, [0,1,0,2,0])), [1,2]) |
|
45524
e81e45f8002e
Make sure the itertools filter functions give the same performance for func=bool as func=None.
Raymond Hettinger <python@rcn.com>
parents:
45438
diff
changeset
|
490 self.assertEqual(list(ifilter(bool, [0,1,0,2,0])), [1,2]) |
| 29690 | 491 self.assertEqual(take(4, ifilter(isEven, count())), [0,2,4,6]) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
492 self.assertRaises(TypeError, ifilter) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
493 self.assertRaises(TypeError, ifilter, lambda x:x) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
494 self.assertRaises(TypeError, ifilter, lambda x:x, range(6), 7) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
495 self.assertRaises(TypeError, ifilter, isEven, 3) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
496 self.assertRaises(TypeError, ifilter(range(6), range(6)).next) |
| 27769 | 497 |
| 498 def test_ifilterfalse(self): | |
| 499 self.assertEqual(list(ifilterfalse(isEven, range(6))), [1,3,5]) | |
| 500 self.assertEqual(list(ifilterfalse(None, [0,1,0,2,0])), [0,0,0]) | |
|
45524
e81e45f8002e
Make sure the itertools filter functions give the same performance for func=bool as func=None.
Raymond Hettinger <python@rcn.com>
parents:
45438
diff
changeset
|
501 self.assertEqual(list(ifilterfalse(bool, [0,1,0,2,0])), [0,0,0]) |
| 29690 | 502 self.assertEqual(take(4, ifilterfalse(isEven, count())), [1,3,5,7]) |
| 27769 | 503 self.assertRaises(TypeError, ifilterfalse) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
504 self.assertRaises(TypeError, ifilterfalse, lambda x:x) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
505 self.assertRaises(TypeError, ifilterfalse, lambda x:x, range(6), 7) |
| 27769 | 506 self.assertRaises(TypeError, ifilterfalse, isEven, 3) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
507 self.assertRaises(TypeError, ifilterfalse(range(6), range(6)).next) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
508 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
509 def test_izip(self): |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
510 ans = [(x,y) for x, y in izip('abc',count())] |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
511 self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)]) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
512 self.assertEqual(list(izip('abc', range(6))), zip('abc', range(6))) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
513 self.assertEqual(list(izip('abcdef', range(3))), zip('abcdef', range(3))) |
| 29690 | 514 self.assertEqual(take(3,izip('abcdef', count())), zip('abcdef', range(3))) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
515 self.assertEqual(list(izip('abcdef')), zip('abcdef')) |
|
30093
0ac4396b1894
Modified itertools.izip() to match the behavior of __builtin__.zip()
Raymond Hettinger <python@rcn.com>
parents:
30092
diff
changeset
|
516 self.assertEqual(list(izip()), zip()) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
517 self.assertRaises(TypeError, izip, 3) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
518 self.assertRaises(TypeError, izip, range(3), 3) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
519 # Check tuple re-use (implementation detail) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
520 self.assertEqual([tuple(list(pair)) for pair in izip('abc', 'def')], |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
521 zip('abc', 'def')) |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
522 self.assertEqual([pair for pair in izip('abc', 'def')], |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
523 zip('abc', 'def')) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
524 ids = map(id, izip('abc', 'def')) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
525 self.assertEqual(min(ids), max(ids)) |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
526 ids = map(id, list(izip('abc', 'def'))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
527 self.assertEqual(len(dict.fromkeys(ids)), len(ids)) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
528 |
|
40877
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
529 def test_iziplongest(self): |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
530 for args in [ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
531 ['abc', range(6)], |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
532 [range(6), 'abc'], |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
533 [range(1000), range(2000,2100), range(3000,3050)], |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
534 [range(1000), range(0), range(3000,3050), range(1200), range(1500)], |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
535 [range(1000), range(0), range(3000,3050), range(1200), range(1500), range(0)], |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
536 ]: |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
537 target = map(None, *args) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
538 self.assertEqual(list(izip_longest(*args)), target) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
539 self.assertEqual(list(izip_longest(*args, **{})), target) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
540 target = [tuple((e is None and 'X' or e) for e in t) for t in target] # Replace None fills with 'X' |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
541 self.assertEqual(list(izip_longest(*args, **dict(fillvalue='X'))), target) |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
40877
diff
changeset
|
542 |
|
40877
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
543 self.assertEqual(take(3,izip_longest('abcdef', count())), zip('abcdef', range(3))) # take 3 from infinite input |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
544 |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
545 self.assertEqual(list(izip_longest()), zip()) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
546 self.assertEqual(list(izip_longest([])), zip([])) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
547 self.assertEqual(list(izip_longest('abcdef')), zip('abcdef')) |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
40877
diff
changeset
|
548 |
|
40877
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
549 self.assertEqual(list(izip_longest('abc', 'defg', **{})), map(None, 'abc', 'defg')) # empty keyword dict |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
550 self.assertRaises(TypeError, izip_longest, 3) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
551 self.assertRaises(TypeError, izip_longest, range(3), 3) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
552 |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
553 for stmt in [ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
554 "izip_longest('abc', fv=1)", |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
40877
diff
changeset
|
555 "izip_longest('abc', fillvalue=1, bogus_keyword=None)", |
|
40877
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
556 ]: |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
557 try: |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
558 eval(stmt, globals(), locals()) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
559 except TypeError: |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
560 pass |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
561 else: |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
562 self.fail('Did not raise Type in: ' + stmt) |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
40877
diff
changeset
|
563 |
|
40877
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
564 # Check tuple re-use (implementation detail) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
565 self.assertEqual([tuple(list(pair)) for pair in izip_longest('abc', 'def')], |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
566 zip('abc', 'def')) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
567 self.assertEqual([pair for pair in izip_longest('abc', 'def')], |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
568 zip('abc', 'def')) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
569 ids = map(id, izip_longest('abc', 'def')) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
570 self.assertEqual(min(ids), max(ids)) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
571 ids = map(id, list(izip_longest('abc', 'def'))) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
572 self.assertEqual(len(dict.fromkeys(ids)), len(ids)) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
573 |
|
45425
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
574 def test_product(self): |
|
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
575 for args, result in [ |
|
45651
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
576 ([], [()]), # zero iterables |
|
45425
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
577 (['ab'], [('a',), ('b',)]), # one iterable |
|
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
578 ([range(2), range(3)], [(0,0), (0,1), (0,2), (1,0), (1,1), (1,2)]), # two iterables |
|
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
579 ([range(0), range(2), range(3)], []), # first iterable with zero length |
|
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
580 ([range(2), range(0), range(3)], []), # middle iterable with zero length |
|
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
581 ([range(2), range(3), range(0)], []), # last iterable with zero length |
|
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
582 ]: |
|
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
583 self.assertEqual(list(product(*args)), result) |
|
45567
10962b3f77a9
Handle the repeat keyword argument for itertools.product().
Raymond Hettinger <python@rcn.com>
parents:
45563
diff
changeset
|
584 for r in range(4): |
|
10962b3f77a9
Handle the repeat keyword argument for itertools.product().
Raymond Hettinger <python@rcn.com>
parents:
45563
diff
changeset
|
585 self.assertEqual(list(product(*(args*r))), |
|
10962b3f77a9
Handle the repeat keyword argument for itertools.product().
Raymond Hettinger <python@rcn.com>
parents:
45563
diff
changeset
|
586 list(product(*args, **dict(repeat=r)))) |
|
45425
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
587 self.assertEqual(len(list(product(*[range(7)]*6))), 7**6) |
|
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
588 self.assertRaises(TypeError, product, range(6), None) |
|
45675
c197e18449b4
C implementation of itertools.permutations().
Raymond Hettinger <python@rcn.com>
parents:
45651
diff
changeset
|
589 |
|
45701
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
590 def product1(*args, **kwds): |
|
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
591 pools = map(tuple, args) * kwds.get('repeat', 1) |
|
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
592 n = len(pools) |
|
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
593 if n == 0: |
|
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
594 yield () |
|
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
595 return |
|
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
596 if any(len(pool) == 0 for pool in pools): |
|
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
597 return |
|
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
598 indices = [0] * n |
|
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
599 yield tuple(pool[i] for pool, i in zip(pools, indices)) |
|
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
600 while 1: |
|
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
601 for i in reversed(range(n)): # right to left |
|
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
602 if indices[i] == len(pools[i]) - 1: |
|
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
603 continue |
|
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
604 indices[i] += 1 |
|
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
605 for j in range(i+1, n): |
|
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
606 indices[j] = 0 |
|
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
607 yield tuple(pool[i] for pool, i in zip(pools, indices)) |
|
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
608 break |
|
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
609 else: |
|
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
610 return |
|
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
611 |
|
45675
c197e18449b4
C implementation of itertools.permutations().
Raymond Hettinger <python@rcn.com>
parents:
45651
diff
changeset
|
612 def product2(*args, **kwds): |
|
c197e18449b4
C implementation of itertools.permutations().
Raymond Hettinger <python@rcn.com>
parents:
45651
diff
changeset
|
613 'Pure python version used in docs' |
|
c197e18449b4
C implementation of itertools.permutations().
Raymond Hettinger <python@rcn.com>
parents:
45651
diff
changeset
|
614 pools = map(tuple, args) * kwds.get('repeat', 1) |
|
c197e18449b4
C implementation of itertools.permutations().
Raymond Hettinger <python@rcn.com>
parents:
45651
diff
changeset
|
615 result = [[]] |
|
c197e18449b4
C implementation of itertools.permutations().
Raymond Hettinger <python@rcn.com>
parents:
45651
diff
changeset
|
616 for pool in pools: |
|
c197e18449b4
C implementation of itertools.permutations().
Raymond Hettinger <python@rcn.com>
parents:
45651
diff
changeset
|
617 result = [x+[y] for x in result for y in pool] |
|
c197e18449b4
C implementation of itertools.permutations().
Raymond Hettinger <python@rcn.com>
parents:
45651
diff
changeset
|
618 for prod in result: |
|
c197e18449b4
C implementation of itertools.permutations().
Raymond Hettinger <python@rcn.com>
parents:
45651
diff
changeset
|
619 yield tuple(prod) |
|
c197e18449b4
C implementation of itertools.permutations().
Raymond Hettinger <python@rcn.com>
parents:
45651
diff
changeset
|
620 |
|
45425
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
621 argtypes = ['', 'abc', '', xrange(0), xrange(4), dict(a=1, b=2, c=3), |
|
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
622 set('abcdefg'), range(11), tuple(range(13))] |
|
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
623 for i in range(100): |
|
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
624 args = [random.choice(argtypes) for j in range(random.randrange(5))] |
|
45651
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
625 expected_len = prod(map(len, args)) |
|
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
626 self.assertEqual(len(list(product(*args))), expected_len) |
|
45701
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
627 self.assertEqual(list(product(*args)), list(product1(*args))) |
|
45675
c197e18449b4
C implementation of itertools.permutations().
Raymond Hettinger <python@rcn.com>
parents:
45651
diff
changeset
|
628 self.assertEqual(list(product(*args)), list(product2(*args))) |
|
45425
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
629 args = map(iter, args) |
|
45651
e44c745bec0b
Beef-up docs and tests for itertools. Fix-up end-case for product().
Raymond Hettinger <python@rcn.com>
parents:
45567
diff
changeset
|
630 self.assertEqual(len(list(product(*args))), expected_len) |
|
45425
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
631 |
|
45438
11a73fbbf86f
Improve the implementation of itertools.product()
Raymond Hettinger <python@rcn.com>
parents:
45425
diff
changeset
|
632 # Test implementation detail: tuple re-use |
|
11a73fbbf86f
Improve the implementation of itertools.product()
Raymond Hettinger <python@rcn.com>
parents:
45425
diff
changeset
|
633 self.assertEqual(len(set(map(id, product('abc', 'def')))), 1) |
|
11a73fbbf86f
Improve the implementation of itertools.product()
Raymond Hettinger <python@rcn.com>
parents:
45425
diff
changeset
|
634 self.assertNotEqual(len(set(map(id, list(product('abc', 'def'))))), 1) |
|
45425
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
635 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
636 def test_repeat(self): |
|
51718
d25fc0209295
Add keyword arg support to itertools.repeat().
Raymond Hettinger <python@rcn.com>
parents:
51715
diff
changeset
|
637 self.assertEqual(list(repeat(object='a', times=3)), ['a', 'a', 'a']) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
638 self.assertEqual(zip(xrange(3),repeat('a')), |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
639 [(0, 'a'), (1, 'a'), (2, 'a')]) |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27769
diff
changeset
|
640 self.assertEqual(list(repeat('a', 3)), ['a', 'a', 'a']) |
| 29690 | 641 self.assertEqual(take(3, repeat('a')), ['a', 'a', 'a']) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
642 self.assertEqual(list(repeat('a', 0)), []) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
643 self.assertEqual(list(repeat('a', -3)), []) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
644 self.assertRaises(TypeError, repeat) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
645 self.assertRaises(TypeError, repeat, None, 3, 4) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
646 self.assertRaises(TypeError, repeat, None, 'a') |
|
33616
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
647 r = repeat(1+0j) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
648 self.assertEqual(repr(r), 'repeat((1+0j))') |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
649 r = repeat(1+0j, 5) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
650 self.assertEqual(repr(r), 'repeat((1+0j), 5)') |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
651 list(r) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
652 self.assertEqual(repr(r), 'repeat((1+0j), 0)') |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
653 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
654 def test_imap(self): |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
655 self.assertEqual(list(imap(operator.pow, range(3), range(1,7))), |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
656 [0**1, 1**2, 2**3]) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
657 self.assertEqual(list(imap(None, 'abc', range(5))), |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
658 [('a',0),('b',1),('c',2)]) |
| 29690 | 659 self.assertEqual(list(imap(None, 'abc', count())), |
| 660 [('a',0),('b',1),('c',2)]) | |
| 661 self.assertEqual(take(2,imap(None, 'abc', count())), | |
| 662 [('a',0),('b',1)]) | |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
663 self.assertEqual(list(imap(operator.pow, [])), []) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
664 self.assertRaises(TypeError, imap) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
665 self.assertRaises(TypeError, imap, operator.neg) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
666 self.assertRaises(TypeError, imap(10, range(5)).next) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
667 self.assertRaises(ValueError, imap(errfunc, [4], [5]).next) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
668 self.assertRaises(TypeError, imap(onearg, [4], [5]).next) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
669 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
670 def test_starmap(self): |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
671 self.assertEqual(list(starmap(operator.pow, zip(range(3), range(1,7)))), |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
672 [0**1, 1**2, 2**3]) |
| 29690 | 673 self.assertEqual(take(3, starmap(operator.pow, izip(count(), count(1)))), |
| 674 [0**1, 1**2, 2**3]) | |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
675 self.assertEqual(list(starmap(operator.pow, [])), []) |
|
44717
d0f7963cf8b0
Make starmap() match its pure python definition and accept any itertable input (not just tuples).
Raymond Hettinger <python@rcn.com>
parents:
43543
diff
changeset
|
676 self.assertEqual(list(starmap(operator.pow, [iter([4,5])])), [4**5]) |
|
d0f7963cf8b0
Make starmap() match its pure python definition and accept any itertable input (not just tuples).
Raymond Hettinger <python@rcn.com>
parents:
43543
diff
changeset
|
677 self.assertRaises(TypeError, list, starmap(operator.pow, [None])) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
678 self.assertRaises(TypeError, starmap) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
679 self.assertRaises(TypeError, starmap, operator.pow, [(4,5)], 'extra') |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
680 self.assertRaises(TypeError, starmap(10, [(4,5)]).next) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
681 self.assertRaises(ValueError, starmap(errfunc, [(4,5)]).next) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
682 self.assertRaises(TypeError, starmap(onearg, [(4,5)]).next) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
683 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
684 def test_islice(self): |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
685 for args in [ # islice(args) should agree with range(args) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
686 (10, 20, 3), |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
687 (10, 3, 20), |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
688 (10, 20), |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
689 (10, 3), |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
690 (20,) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
691 ]: |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
692 self.assertEqual(list(islice(xrange(100), *args)), range(*args)) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
693 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
694 for args, tgtargs in [ # Stop when seqn is exhausted |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
695 ((10, 110, 3), ((10, 100, 3))), |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
696 ((10, 110), ((10, 100))), |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
697 ((110,), (100,)) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
698 ]: |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
699 self.assertEqual(list(islice(xrange(100), *args)), range(*tgtargs)) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
700 |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
701 # Test stop=None |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
702 self.assertEqual(list(islice(xrange(10), None)), range(10)) |
|
34108
1d1d6377acb1
Added optional None arguments to itertools.islice().
Raymond Hettinger <python@rcn.com>
parents:
33777
diff
changeset
|
703 self.assertEqual(list(islice(xrange(10), None, None)), range(10)) |
|
1d1d6377acb1
Added optional None arguments to itertools.islice().
Raymond Hettinger <python@rcn.com>
parents:
33777
diff
changeset
|
704 self.assertEqual(list(islice(xrange(10), None, None, None)), range(10)) |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
705 self.assertEqual(list(islice(xrange(10), 2, None)), range(2, 10)) |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
706 self.assertEqual(list(islice(xrange(10), 1, None, 2)), range(1, 10, 2)) |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
707 |
|
34814
a412638950a1
SF patch #1171417: bug fix for islice() in docs
Raymond Hettinger <python@rcn.com>
parents:
34738
diff
changeset
|
708 # Test number of items consumed SF #1171417 |
|
a412638950a1
SF patch #1171417: bug fix for islice() in docs
Raymond Hettinger <python@rcn.com>
parents:
34738
diff
changeset
|
709 it = iter(range(10)) |
|
a412638950a1
SF patch #1171417: bug fix for islice() in docs
Raymond Hettinger <python@rcn.com>
parents:
34738
diff
changeset
|
710 self.assertEqual(list(islice(it, 3)), range(3)) |
|
a412638950a1
SF patch #1171417: bug fix for islice() in docs
Raymond Hettinger <python@rcn.com>
parents:
34738
diff
changeset
|
711 self.assertEqual(list(it), range(3, 10)) |
|
a412638950a1
SF patch #1171417: bug fix for islice() in docs
Raymond Hettinger <python@rcn.com>
parents:
34738
diff
changeset
|
712 |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
713 # Test invalid arguments |
|
28930
18082b2d2ebd
The previous made the stop argument optional.
Raymond Hettinger <python@rcn.com>
parents:
28929
diff
changeset
|
714 self.assertRaises(TypeError, islice, xrange(10)) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
715 self.assertRaises(TypeError, islice, xrange(10), 1, 2, 3, 4) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
716 self.assertRaises(ValueError, islice, xrange(10), -5, 10, 1) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
717 self.assertRaises(ValueError, islice, xrange(10), 1, -5, -1) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
718 self.assertRaises(ValueError, islice, xrange(10), 1, 10, -1) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
719 self.assertRaises(ValueError, islice, xrange(10), 1, 10, 0) |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
720 self.assertRaises(ValueError, islice, xrange(10), 'a') |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
721 self.assertRaises(ValueError, islice, xrange(10), 'a', 1) |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
722 self.assertRaises(ValueError, islice, xrange(10), 1, 'a') |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
723 self.assertRaises(ValueError, islice, xrange(10), 'a', 1, 1) |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
724 self.assertRaises(ValueError, islice, xrange(10), 1, 'a', 1) |
|
41589
37d82bee7a16
Fix those parts in the testsuite that assumed that sys.maxint would cause overflow on x64. Now the testsuite is well behaved on that platform.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
41133
diff
changeset
|
725 self.assertEqual(len(list(islice(count(), 1, 10, maxsize))), 1) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
726 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
727 def test_takewhile(self): |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
728 data = [1, 3, 5, 20, 2, 4, 6, 8] |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
729 underten = lambda x: x<10 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
730 self.assertEqual(list(takewhile(underten, data)), [1, 3, 5]) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
731 self.assertEqual(list(takewhile(underten, [])), []) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
732 self.assertRaises(TypeError, takewhile) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
733 self.assertRaises(TypeError, takewhile, operator.pow) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
734 self.assertRaises(TypeError, takewhile, operator.pow, [(4,5)], 'extra') |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
735 self.assertRaises(TypeError, takewhile(10, [(4,5)]).next) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
736 self.assertRaises(ValueError, takewhile(errfunc, [(4,5)]).next) |
|
33616
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
737 t = takewhile(bool, [1, 1, 1, 0, 0, 0]) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
738 self.assertEqual(list(t), [1, 1, 1]) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
739 self.assertRaises(StopIteration, t.next) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
740 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
741 def test_dropwhile(self): |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
742 data = [1, 3, 5, 20, 2, 4, 6, 8] |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
743 underten = lambda x: x<10 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
744 self.assertEqual(list(dropwhile(underten, data)), [20, 2, 4, 6, 8]) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
745 self.assertEqual(list(dropwhile(underten, [])), []) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
746 self.assertRaises(TypeError, dropwhile) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
747 self.assertRaises(TypeError, dropwhile, operator.pow) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
748 self.assertRaises(TypeError, dropwhile, operator.pow, [(4,5)], 'extra') |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
749 self.assertRaises(TypeError, dropwhile(10, [(4,5)]).next) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
750 self.assertRaises(ValueError, dropwhile(errfunc, [(4,5)]).next) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
751 |
| 30764 | 752 def test_tee(self): |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
753 n = 200 |
| 30764 | 754 def irange(n): |
| 755 for i in xrange(n): | |
| 756 yield i | |
| 757 | |
| 758 a, b = tee([]) # test empty iterator | |
| 759 self.assertEqual(list(a), []) | |
| 760 self.assertEqual(list(b), []) | |
| 761 | |
| 762 a, b = tee(irange(n)) # test 100% interleaved | |
| 763 self.assertEqual(zip(a,b), zip(range(n),range(n))) | |
| 764 | |
| 765 a, b = tee(irange(n)) # test 0% interleaved | |
| 766 self.assertEqual(list(a), range(n)) | |
| 767 self.assertEqual(list(b), range(n)) | |
| 768 | |
| 769 a, b = tee(irange(n)) # test dealloc of leading iterator | |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
770 for i in xrange(100): |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
771 self.assertEqual(a.next(), i) |
| 30764 | 772 del a |
| 773 self.assertEqual(list(b), range(n)) | |
| 774 | |
| 775 a, b = tee(irange(n)) # test dealloc of trailing iterator | |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
776 for i in xrange(100): |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
777 self.assertEqual(a.next(), i) |
| 30764 | 778 del b |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
779 self.assertEqual(list(a), range(100, n)) |
| 30764 | 780 |
| 781 for j in xrange(5): # test randomly interleaved | |
| 782 order = [0]*n + [1]*n | |
| 783 random.shuffle(order) | |
| 784 lists = ([], []) | |
| 785 its = tee(irange(n)) | |
| 786 for i in order: | |
| 787 value = its[i].next() | |
| 788 lists[i].append(value) | |
| 789 self.assertEqual(lists[0], range(n)) | |
| 790 self.assertEqual(lists[1], range(n)) | |
| 791 | |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
792 # test argument format checking |
| 30764 | 793 self.assertRaises(TypeError, tee) |
| 794 self.assertRaises(TypeError, tee, 3) | |
| 795 self.assertRaises(TypeError, tee, [1,2], 'x') | |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
796 self.assertRaises(TypeError, tee, [1,2], 3, 'x') |
| 30764 | 797 |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
798 # tee object should be instantiable |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
799 a, b = tee('abc') |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
800 c = type(a)('def') |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
801 self.assertEqual(list(c), list('def')) |
|
30786
225d848a5f96
Minor improvements to itertools.tee():
Raymond Hettinger <python@rcn.com>
parents:
30764
diff
changeset
|
802 |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
803 # test long-lagged and multi-way split |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
804 a, b, c = tee(xrange(2000), 3) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
805 for i in xrange(100): |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
806 self.assertEqual(a.next(), i) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
807 self.assertEqual(list(b), range(2000)) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
808 self.assertEqual([c.next(), c.next()], range(2)) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
809 self.assertEqual(list(a), range(100,2000)) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
810 self.assertEqual(list(c), range(2,2000)) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
811 |
|
33616
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
812 # test values of n |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
813 self.assertRaises(TypeError, tee, 'abc', 'invalid') |
|
39720
1352ad9afcc0
Bug #1550714: fix SystemError from itertools.tee on negative value for n.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38464
diff
changeset
|
814 self.assertRaises(ValueError, tee, [], -1) |
|
33616
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
815 for n in xrange(5): |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
816 result = tee('abc', n) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
817 self.assertEqual(type(result), tuple) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
818 self.assertEqual(len(result), n) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
819 self.assertEqual(map(list, result), [list('abc')]*n) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
820 |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
821 # tee pass-through to copyable iterator |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
822 a, b = tee('abc') |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
823 c, d = tee(a) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
824 self.assert_(a is c) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
825 |
|
33616
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
826 # test tee_new |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
827 t1, t2 = tee('abc') |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
828 tnew = type(t1) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
829 self.assertRaises(TypeError, tnew) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
830 self.assertRaises(TypeError, tnew, 10) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
831 t3 = tnew(t1) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
832 self.assert_(list(t1) == list(t2) == list(t3) == list('abc')) |
|
30786
225d848a5f96
Minor improvements to itertools.tee():
Raymond Hettinger <python@rcn.com>
parents:
30764
diff
changeset
|
833 |
|
33777
e4b7c923ed5f
Fix and test weak referencing of itertools.tee objects.
Raymond Hettinger <python@rcn.com>
parents:
33624
diff
changeset
|
834 # test that tee objects are weak referencable |
|
e4b7c923ed5f
Fix and test weak referencing of itertools.tee objects.
Raymond Hettinger <python@rcn.com>
parents:
33624
diff
changeset
|
835 a, b = tee(xrange(10)) |
|
e4b7c923ed5f
Fix and test weak referencing of itertools.tee objects.
Raymond Hettinger <python@rcn.com>
parents:
33624
diff
changeset
|
836 p = proxy(a) |
|
e4b7c923ed5f
Fix and test weak referencing of itertools.tee objects.
Raymond Hettinger <python@rcn.com>
parents:
33624
diff
changeset
|
837 self.assertEqual(getattr(p, '__class__'), type(b)) |
|
e4b7c923ed5f
Fix and test weak referencing of itertools.tee objects.
Raymond Hettinger <python@rcn.com>
parents:
33624
diff
changeset
|
838 del a |
|
e4b7c923ed5f
Fix and test weak referencing of itertools.tee objects.
Raymond Hettinger <python@rcn.com>
parents:
33624
diff
changeset
|
839 self.assertRaises(ReferenceError, getattr, p, '__class__') |
|
e4b7c923ed5f
Fix and test weak referencing of itertools.tee objects.
Raymond Hettinger <python@rcn.com>
parents:
33624
diff
changeset
|
840 |
|
28936
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
841 def test_StopIteration(self): |
|
30093
0ac4396b1894
Modified itertools.izip() to match the behavior of __builtin__.zip()
Raymond Hettinger <python@rcn.com>
parents:
30092
diff
changeset
|
842 self.assertRaises(StopIteration, izip().next) |
|
0ac4396b1894
Modified itertools.izip() to match the behavior of __builtin__.zip()
Raymond Hettinger <python@rcn.com>
parents:
30092
diff
changeset
|
843 |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
844 for f in (chain, cycle, izip, groupby): |
|
28936
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
845 self.assertRaises(StopIteration, f([]).next) |
|
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
846 self.assertRaises(StopIteration, f(StopNow()).next) |
|
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
847 |
|
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
848 self.assertRaises(StopIteration, islice([], None).next) |
|
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
849 self.assertRaises(StopIteration, islice(StopNow(), None).next) |
|
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
850 |
| 30764 | 851 p, q = tee([]) |
| 852 self.assertRaises(StopIteration, p.next) | |
| 853 self.assertRaises(StopIteration, q.next) | |
| 854 p, q = tee(StopNow()) | |
| 855 self.assertRaises(StopIteration, p.next) | |
| 856 self.assertRaises(StopIteration, q.next) | |
| 857 | |
|
28936
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
858 self.assertRaises(StopIteration, repeat(None, 0).next) |
|
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
859 |
|
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
860 for f in (ifilter, ifilterfalse, imap, takewhile, dropwhile, starmap): |
|
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
861 self.assertRaises(StopIteration, f(lambda x:x, []).next) |
|
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
862 self.assertRaises(StopIteration, f(lambda x:x, StopNow()).next) |
|
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
863 |
| 45698 | 864 class TestExamples(unittest.TestCase): |
| 865 | |
| 866 def test_chain(self): | |
| 867 self.assertEqual(''.join(chain('ABC', 'DEF')), 'ABCDEF') | |
| 868 | |
| 869 def test_chain_from_iterable(self): | |
| 870 self.assertEqual(''.join(chain.from_iterable(['ABC', 'DEF'])), 'ABCDEF') | |
| 871 | |
| 872 def test_combinations(self): | |
| 873 self.assertEqual(list(combinations('ABCD', 2)), | |
| 874 [('A','B'), ('A','C'), ('A','D'), ('B','C'), ('B','D'), ('C','D')]) | |
| 875 self.assertEqual(list(combinations(range(4), 3)), | |
| 876 [(0,1,2), (0,1,3), (0,2,3), (1,2,3)]) | |
| 877 | |
|
51097
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
878 def test_combinations_with_replacement(self): |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
879 self.assertEqual(list(combinations_with_replacement('ABC', 2)), |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
880 [('A','A'), ('A','B'), ('A','C'), ('B','B'), ('B','C'), ('C','C')]) |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
881 |
|
51045
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
882 def test_compress(self): |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
883 self.assertEqual(list(compress('ABCDEF', [1,0,1,0,1,1])), list('ACEF')) |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
884 |
| 45698 | 885 def test_count(self): |
| 886 self.assertEqual(list(islice(count(10), 5)), [10, 11, 12, 13, 14]) | |
| 887 | |
| 888 def test_cycle(self): | |
| 889 self.assertEqual(list(islice(cycle('ABCD'), 12)), list('ABCDABCDABCD')) | |
| 890 | |
| 891 def test_dropwhile(self): | |
| 892 self.assertEqual(list(dropwhile(lambda x: x<5, [1,4,6,4,1])), [6,4,1]) | |
| 893 | |
| 894 def test_groupby(self): | |
| 895 self.assertEqual([k for k, g in groupby('AAAABBBCCDAABBB')], | |
| 896 list('ABCDAB')) | |
| 897 self.assertEqual([(list(g)) for k, g in groupby('AAAABBBCCD')], | |
| 898 [list('AAAA'), list('BBB'), list('CC'), list('D')]) | |
| 899 | |
| 900 def test_ifilter(self): | |
| 901 self.assertEqual(list(ifilter(lambda x: x%2, range(10))), [1,3,5,7,9]) | |
| 902 | |
| 903 def test_ifilterfalse(self): | |
| 904 self.assertEqual(list(ifilterfalse(lambda x: x%2, range(10))), [0,2,4,6,8]) | |
| 905 | |
| 906 def test_imap(self): | |
| 907 self.assertEqual(list(imap(pow, (2,3,10), (5,2,3))), [32, 9, 1000]) | |
| 908 | |
| 909 def test_islice(self): | |
| 910 self.assertEqual(list(islice('ABCDEFG', 2)), list('AB')) | |
| 911 self.assertEqual(list(islice('ABCDEFG', 2, 4)), list('CD')) | |
| 912 self.assertEqual(list(islice('ABCDEFG', 2, None)), list('CDEFG')) | |
| 913 self.assertEqual(list(islice('ABCDEFG', 0, None, 2)), list('ACEG')) | |
| 914 | |
| 915 def test_izip(self): | |
| 916 self.assertEqual(list(izip('ABCD', 'xy')), [('A', 'x'), ('B', 'y')]) | |
| 917 | |
| 918 def test_izip_longest(self): | |
| 919 self.assertEqual(list(izip_longest('ABCD', 'xy', fillvalue='-')), | |
| 920 [('A', 'x'), ('B', 'y'), ('C', '-'), ('D', '-')]) | |
| 921 | |
| 922 def test_permutations(self): | |
| 923 self.assertEqual(list(permutations('ABCD', 2)), | |
| 924 map(tuple, 'AB AC AD BA BC BD CA CB CD DA DB DC'.split())) | |
| 925 self.assertEqual(list(permutations(range(3))), | |
| 926 [(0,1,2), (0,2,1), (1,0,2), (1,2,0), (2,0,1), (2,1,0)]) | |
| 927 | |
| 928 def test_product(self): | |
| 929 self.assertEqual(list(product('ABCD', 'xy')), | |
| 930 map(tuple, 'Ax Ay Bx By Cx Cy Dx Dy'.split())) | |
| 931 self.assertEqual(list(product(range(2), repeat=3)), | |
| 932 [(0,0,0), (0,0,1), (0,1,0), (0,1,1), | |
| 933 (1,0,0), (1,0,1), (1,1,0), (1,1,1)]) | |
| 934 | |
| 935 def test_repeat(self): | |
| 936 self.assertEqual(list(repeat(10, 3)), [10, 10, 10]) | |
| 937 | |
| 938 def test_stapmap(self): | |
| 939 self.assertEqual(list(starmap(pow, [(2,5), (3,2), (10,3)])), | |
| 940 [32, 9, 1000]) | |
| 941 | |
| 942 def test_takewhile(self): | |
| 943 self.assertEqual(list(takewhile(lambda x: x<5, [1,4,6,4,1])), [1,4]) | |
| 944 | |
| 945 | |
| 30764 | 946 class TestGC(unittest.TestCase): |
| 947 | |
| 948 def makecycle(self, iterator, container): | |
| 949 container.append(iterator) | |
| 950 iterator.next() | |
| 951 del container, iterator | |
| 952 | |
| 953 def test_chain(self): | |
| 954 a = [] | |
| 955 self.makecycle(chain(a), a) | |
| 956 | |
| 45698 | 957 def test_chain_from_iterable(self): |
| 958 a = [] | |
| 959 self.makecycle(chain.from_iterable([a]), a) | |
| 960 | |
| 961 def test_combinations(self): | |
| 962 a = [] | |
| 963 self.makecycle(combinations([1,2,a,3], 3), a) | |
| 964 | |
|
51097
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
965 def test_combinations_with_replacement(self): |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
966 a = [] |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
967 self.makecycle(combinations_with_replacement([1,2,a,3], 3), a) |
|
b7ce675febb9
Promote combinations_with_replacement() from a recipe to a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
51046
diff
changeset
|
968 |
|
51045
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
969 def test_compress(self): |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
970 a = [] |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
971 self.makecycle(compress('ABCDEF', [1,0,1,0,1,0]), a) |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
972 |
|
51660
0ebbcf5988db
Add GC support to count() objects. Backport candidate.
Raymond Hettinger <python@rcn.com>
parents:
51622
diff
changeset
|
973 def test_count(self): |
|
0ebbcf5988db
Add GC support to count() objects. Backport candidate.
Raymond Hettinger <python@rcn.com>
parents:
51622
diff
changeset
|
974 a = [] |
|
0ebbcf5988db
Add GC support to count() objects. Backport candidate.
Raymond Hettinger <python@rcn.com>
parents:
51622
diff
changeset
|
975 Int = type('Int', (int,), dict(x=a)) |
|
0ebbcf5988db
Add GC support to count() objects. Backport candidate.
Raymond Hettinger <python@rcn.com>
parents:
51622
diff
changeset
|
976 self.makecycle(count(Int(0), Int(1)), a) |
|
0ebbcf5988db
Add GC support to count() objects. Backport candidate.
Raymond Hettinger <python@rcn.com>
parents:
51622
diff
changeset
|
977 |
| 30764 | 978 def test_cycle(self): |
| 979 a = [] | |
| 980 self.makecycle(cycle([a]*2), a) | |
| 981 | |
|
33624
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
982 def test_dropwhile(self): |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
983 a = [] |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
984 self.makecycle(dropwhile(bool, [0, a, a]), a) |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
985 |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
986 def test_groupby(self): |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
987 a = [] |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
988 self.makecycle(groupby([a]*2, lambda x:x), a) |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
989 |
|
45699
0be9b1cf1b16
Issue 2246: itertools grouper object did not participate in GC (should be backported).
Raymond Hettinger <python@rcn.com>
parents:
45698
diff
changeset
|
990 def test_issue2246(self): |
|
0be9b1cf1b16
Issue 2246: itertools grouper object did not participate in GC (should be backported).
Raymond Hettinger <python@rcn.com>
parents:
45698
diff
changeset
|
991 # Issue 2246 -- the _grouper iterator was not included in GC |
|
0be9b1cf1b16
Issue 2246: itertools grouper object did not participate in GC (should be backported).
Raymond Hettinger <python@rcn.com>
parents:
45698
diff
changeset
|
992 n = 10 |
|
0be9b1cf1b16
Issue 2246: itertools grouper object did not participate in GC (should be backported).
Raymond Hettinger <python@rcn.com>
parents:
45698
diff
changeset
|
993 keyfunc = lambda x: x |
|
0be9b1cf1b16
Issue 2246: itertools grouper object did not participate in GC (should be backported).
Raymond Hettinger <python@rcn.com>
parents:
45698
diff
changeset
|
994 for i, j in groupby(xrange(n), key=keyfunc): |
|
0be9b1cf1b16
Issue 2246: itertools grouper object did not participate in GC (should be backported).
Raymond Hettinger <python@rcn.com>
parents:
45698
diff
changeset
|
995 keyfunc.__dict__.setdefault('x',[]).append(j) |
|
0be9b1cf1b16
Issue 2246: itertools grouper object did not participate in GC (should be backported).
Raymond Hettinger <python@rcn.com>
parents:
45698
diff
changeset
|
996 |
| 30764 | 997 def test_ifilter(self): |
| 998 a = [] | |
| 999 self.makecycle(ifilter(lambda x:True, [a]*2), a) | |
| 1000 | |
| 1001 def test_ifilterfalse(self): | |
| 1002 a = [] | |
| 1003 self.makecycle(ifilterfalse(lambda x:False, a), a) | |
| 1004 | |
| 1005 def test_izip(self): | |
| 1006 a = [] | |
| 1007 self.makecycle(izip([a]*2, [a]*3), a) | |
| 1008 | |
| 45698 | 1009 def test_izip_longest(self): |
| 1010 a = [] | |
| 1011 self.makecycle(izip_longest([a]*2, [a]*3), a) | |
| 1012 b = [a, None] | |
| 1013 self.makecycle(izip_longest([a]*2, [a]*3, fillvalue=b), a) | |
| 1014 | |
| 30764 | 1015 def test_imap(self): |
| 1016 a = [] | |
| 1017 self.makecycle(imap(lambda x:x, [a]*2), a) | |
| 1018 | |
| 1019 def test_islice(self): | |
| 1020 a = [] | |
| 1021 self.makecycle(islice([a]*2, None), a) | |
| 1022 | |
| 45698 | 1023 def test_permutations(self): |
| 1024 a = [] | |
| 1025 self.makecycle(permutations([1,2,a,3], 3), a) | |
| 1026 | |
| 1027 def test_product(self): | |
| 1028 a = [] | |
| 1029 self.makecycle(product([1,2,a,3], repeat=3), a) | |
| 1030 | |
|
33624
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1031 def test_repeat(self): |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1032 a = [] |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1033 self.makecycle(repeat(a), a) |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1034 |
| 30764 | 1035 def test_starmap(self): |
| 1036 a = [] | |
| 1037 self.makecycle(starmap(lambda *t: t, [(a,a)]*2), a) | |
| 1038 | |
|
33624
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1039 def test_takewhile(self): |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1040 a = [] |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1041 self.makecycle(takewhile(bool, [1, 0, a, a]), a) |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1042 |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1043 def R(seqn): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1044 'Regular generator' |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1045 for i in seqn: |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1046 yield i |
|
28936
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
1047 |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1048 class G: |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1049 'Sequence using __getitem__' |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1050 def __init__(self, seqn): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1051 self.seqn = seqn |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1052 def __getitem__(self, i): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1053 return self.seqn[i] |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1054 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1055 class I: |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1056 'Sequence using iterator protocol' |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1057 def __init__(self, seqn): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1058 self.seqn = seqn |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1059 self.i = 0 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1060 def __iter__(self): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1061 return self |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1062 def next(self): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1063 if self.i >= len(self.seqn): raise StopIteration |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1064 v = self.seqn[self.i] |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1065 self.i += 1 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1066 return v |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1067 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1068 class Ig: |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1069 'Sequence using iterator protocol defined with a generator' |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1070 def __init__(self, seqn): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1071 self.seqn = seqn |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1072 self.i = 0 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1073 def __iter__(self): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1074 for val in self.seqn: |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1075 yield val |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1076 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1077 class X: |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1078 'Missing __getitem__ and __iter__' |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1079 def __init__(self, seqn): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1080 self.seqn = seqn |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1081 self.i = 0 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1082 def next(self): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1083 if self.i >= len(self.seqn): raise StopIteration |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1084 v = self.seqn[self.i] |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1085 self.i += 1 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1086 return v |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1087 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1088 class N: |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1089 'Iterator missing next()' |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1090 def __init__(self, seqn): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1091 self.seqn = seqn |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1092 self.i = 0 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1093 def __iter__(self): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1094 return self |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1095 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1096 class E: |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1097 'Test propagation of exceptions' |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1098 def __init__(self, seqn): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1099 self.seqn = seqn |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1100 self.i = 0 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1101 def __iter__(self): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1102 return self |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1103 def next(self): |
|
33597
355b15e4a4da
Use floor division operator.
Raymond Hettinger <python@rcn.com>
parents:
33546
diff
changeset
|
1104 3 // 0 |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1105 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1106 class S: |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1107 'Test immediate stop' |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1108 def __init__(self, seqn): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1109 pass |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1110 def __iter__(self): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1111 return self |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1112 def next(self): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1113 raise StopIteration |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1114 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1115 def L(seqn): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1116 'Test multiple tiers of iterators' |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1117 return chain(imap(lambda x:x, R(Ig(G(seqn))))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1118 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1119 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1120 class TestVariousIteratorArgs(unittest.TestCase): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1121 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1122 def test_chain(self): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1123 for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1124 for g in (G, I, Ig, S, L, R): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1125 self.assertEqual(list(chain(g(s))), list(g(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1126 self.assertEqual(list(chain(g(s), g(s))), list(g(s))+list(g(s))) |
|
45562
43e084847e53
Have itertools.chain() consume its inputs lazily instead of building a tuple of iterators at the outset.
Raymond Hettinger <python@rcn.com>
parents:
45541
diff
changeset
|
1127 self.assertRaises(TypeError, list, chain(X(s))) |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1128 self.assertRaises(TypeError, list, chain(N(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1129 self.assertRaises(ZeroDivisionError, list, chain(E(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1130 |
|
51045
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
1131 def test_compress(self): |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
1132 for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
1133 n = len(s) |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
1134 for g in (G, I, Ig, S, L, R): |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
1135 self.assertEqual(list(compress(g(s), repeat(1))), list(g(s))) |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
1136 self.assertRaises(TypeError, compress, X(s), repeat(1)) |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
1137 self.assertRaises(TypeError, list, compress(N(s), repeat(1))) |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
1138 self.assertRaises(ZeroDivisionError, list, compress(E(s), repeat(1))) |
|
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
1139 |
|
45425
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
1140 def test_product(self): |
|
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
1141 for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): |
|
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
1142 self.assertRaises(TypeError, product, X(s)) |
|
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
1143 self.assertRaises(TypeError, product, N(s)) |
|
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
1144 self.assertRaises(ZeroDivisionError, product, E(s)) |
|
3cc57fd87d0e
First draft for itertools.product(). Docs and other updates forthcoming.
Raymond Hettinger <python@rcn.com>
parents:
44717
diff
changeset
|
1145 |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1146 def test_cycle(self): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1147 for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1148 for g in (G, I, Ig, S, L, R): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1149 tgtlen = len(s) * 3 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1150 expected = list(g(s))*3 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1151 actual = list(islice(cycle(g(s)), tgtlen)) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1152 self.assertEqual(actual, expected) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1153 self.assertRaises(TypeError, cycle, X(s)) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1154 self.assertRaises(TypeError, list, cycle(N(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1155 self.assertRaises(ZeroDivisionError, list, cycle(E(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1156 |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1157 def test_groupby(self): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1158 for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1159 for g in (G, I, Ig, S, L, R): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1160 self.assertEqual([k for k, sb in groupby(g(s))], list(g(s))) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1161 self.assertRaises(TypeError, groupby, X(s)) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1162 self.assertRaises(TypeError, list, groupby(N(s))) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1163 self.assertRaises(ZeroDivisionError, list, groupby(E(s))) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1164 |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1165 def test_ifilter(self): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1166 for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1167 for g in (G, I, Ig, S, L, R): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1168 self.assertEqual(list(ifilter(isEven, g(s))), filter(isEven, g(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1169 self.assertRaises(TypeError, ifilter, isEven, X(s)) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1170 self.assertRaises(TypeError, list, ifilter(isEven, N(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1171 self.assertRaises(ZeroDivisionError, list, ifilter(isEven, E(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1172 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1173 def test_ifilterfalse(self): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1174 for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1175 for g in (G, I, Ig, S, L, R): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1176 self.assertEqual(list(ifilterfalse(isEven, g(s))), filter(isOdd, g(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1177 self.assertRaises(TypeError, ifilterfalse, isEven, X(s)) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1178 self.assertRaises(TypeError, list, ifilterfalse(isEven, N(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1179 self.assertRaises(ZeroDivisionError, list, ifilterfalse(isEven, E(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1180 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1181 def test_izip(self): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1182 for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1183 for g in (G, I, Ig, S, L, R): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1184 self.assertEqual(list(izip(g(s))), zip(g(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1185 self.assertEqual(list(izip(g(s), g(s))), zip(g(s), g(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1186 self.assertRaises(TypeError, izip, X(s)) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1187 self.assertRaises(TypeError, list, izip(N(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1188 self.assertRaises(ZeroDivisionError, list, izip(E(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1189 |
|
40877
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
1190 def test_iziplongest(self): |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
1191 for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
1192 for g in (G, I, Ig, S, L, R): |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
1193 self.assertEqual(list(izip_longest(g(s))), zip(g(s))) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
1194 self.assertEqual(list(izip_longest(g(s), g(s))), zip(g(s), g(s))) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
1195 self.assertRaises(TypeError, izip_longest, X(s)) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
1196 self.assertRaises(TypeError, list, izip_longest(N(s))) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
1197 self.assertRaises(ZeroDivisionError, list, izip_longest(E(s))) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
1198 |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1199 def test_imap(self): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1200 for s in (range(10), range(0), range(100), (7,11), xrange(20,50,5)): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1201 for g in (G, I, Ig, S, L, R): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1202 self.assertEqual(list(imap(onearg, g(s))), map(onearg, g(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1203 self.assertEqual(list(imap(operator.pow, g(s), g(s))), map(operator.pow, g(s), g(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1204 self.assertRaises(TypeError, imap, onearg, X(s)) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1205 self.assertRaises(TypeError, list, imap(onearg, N(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1206 self.assertRaises(ZeroDivisionError, list, imap(onearg, E(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1207 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1208 def test_islice(self): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1209 for s in ("12345", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1210 for g in (G, I, Ig, S, L, R): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1211 self.assertEqual(list(islice(g(s),1,None,2)), list(g(s))[1::2]) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1212 self.assertRaises(TypeError, islice, X(s), 10) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1213 self.assertRaises(TypeError, list, islice(N(s), 10)) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1214 self.assertRaises(ZeroDivisionError, list, islice(E(s), 10)) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1215 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1216 def test_starmap(self): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1217 for s in (range(10), range(0), range(100), (7,11), xrange(20,50,5)): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1218 for g in (G, I, Ig, S, L, R): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1219 ss = zip(s, s) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1220 self.assertEqual(list(starmap(operator.pow, g(ss))), map(operator.pow, g(s), g(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1221 self.assertRaises(TypeError, starmap, operator.pow, X(ss)) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1222 self.assertRaises(TypeError, list, starmap(operator.pow, N(ss))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1223 self.assertRaises(ZeroDivisionError, list, starmap(operator.pow, E(ss))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1224 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1225 def test_takewhile(self): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1226 for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1227 for g in (G, I, Ig, S, L, R): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1228 tgt = [] |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1229 for elem in g(s): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1230 if not isEven(elem): break |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1231 tgt.append(elem) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1232 self.assertEqual(list(takewhile(isEven, g(s))), tgt) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1233 self.assertRaises(TypeError, takewhile, isEven, X(s)) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1234 self.assertRaises(TypeError, list, takewhile(isEven, N(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1235 self.assertRaises(ZeroDivisionError, list, takewhile(isEven, E(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1236 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1237 def test_dropwhile(self): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1238 for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1239 for g in (G, I, Ig, S, L, R): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1240 tgt = [] |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1241 for elem in g(s): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1242 if not tgt and isOdd(elem): continue |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1243 tgt.append(elem) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1244 self.assertEqual(list(dropwhile(isOdd, g(s))), tgt) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1245 self.assertRaises(TypeError, dropwhile, isOdd, X(s)) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1246 self.assertRaises(TypeError, list, dropwhile(isOdd, N(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1247 self.assertRaises(ZeroDivisionError, list, dropwhile(isOdd, E(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1248 |
| 30764 | 1249 def test_tee(self): |
| 1250 for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): | |
| 1251 for g in (G, I, Ig, S, L, R): | |
| 1252 it1, it2 = tee(g(s)) | |
| 1253 self.assertEqual(list(it1), list(g(s))) | |
| 1254 self.assertEqual(list(it2), list(g(s))) | |
| 1255 self.assertRaises(TypeError, tee, X(s)) | |
| 1256 self.assertRaises(TypeError, list, tee(N(s))[0]) | |
| 1257 self.assertRaises(ZeroDivisionError, list, tee(E(s))[0]) | |
| 1258 | |
|
31501
14f860b03be2
Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents:
31401
diff
changeset
|
1259 class LengthTransparency(unittest.TestCase): |
|
14f860b03be2
Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents:
31401
diff
changeset
|
1260 |
|
14f860b03be2
Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents:
31401
diff
changeset
|
1261 def test_repeat(self): |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
34814
diff
changeset
|
1262 from test.test_iterlen import len |
|
31501
14f860b03be2
Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents:
31401
diff
changeset
|
1263 self.assertEqual(len(repeat(None, 50)), 50) |
|
14f860b03be2
Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents:
31401
diff
changeset
|
1264 self.assertRaises(TypeError, len, repeat(None)) |
|
14f860b03be2
Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents:
31401
diff
changeset
|
1265 |
|
30238
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1266 class RegressionTests(unittest.TestCase): |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1267 |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1268 def test_sf_793826(self): |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1269 # Fix Armin Rigo's successful efforts to wreak havoc |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1270 |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1271 def mutatingtuple(tuple1, f, tuple2): |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1272 # this builds a tuple t which is a copy of tuple1, |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1273 # then calls f(t), then mutates t to be equal to tuple2 |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1274 # (needs len(tuple1) == len(tuple2)). |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1275 def g(value, first=[1]): |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1276 if first: |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1277 del first[:] |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1278 f(z.next()) |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1279 return value |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1280 items = list(tuple2) |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1281 items[1:1] = list(tuple1) |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1282 gen = imap(g, items) |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1283 z = izip(*[gen]*len(tuple1)) |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1284 z.next() |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1285 |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1286 def f(t): |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1287 global T |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1288 T = t |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1289 first[:] = list(T) |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1290 |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1291 first = [] |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1292 mutatingtuple((1,2,3), f, (4,5,6)) |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1293 second = list(T) |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1294 self.assertEqual(first, second) |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1295 |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1296 |
|
32010
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1297 def test_sf_950057(self): |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1298 # Make sure that chain() and cycle() catch exceptions immediately |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1299 # rather than when shifting between input sources |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1300 |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1301 def gen1(): |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1302 hist.append(0) |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1303 yield 1 |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1304 hist.append(1) |
|
32747
e4575519ec26
test_sf_950057's gen1() used an assert statement, which caused the test
Tim Peters <tim.peters@gmail.com>
parents:
32010
diff
changeset
|
1305 raise AssertionError |
|
32010
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1306 hist.append(2) |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1307 |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1308 def gen2(x): |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1309 hist.append(3) |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1310 yield 2 |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1311 hist.append(4) |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1312 if x: |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1313 raise StopIteration |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1314 |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1315 hist = [] |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1316 self.assertRaises(AssertionError, list, chain(gen1(), gen2(False))) |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1317 self.assertEqual(hist, [0,1]) |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1318 |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1319 hist = [] |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1320 self.assertRaises(AssertionError, list, chain(gen1(), gen2(True))) |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1321 self.assertEqual(hist, [0,1]) |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1322 |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1323 hist = [] |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1324 self.assertRaises(AssertionError, list, cycle(gen1())) |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1325 self.assertEqual(hist, [0,1]) |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1326 |
|
40682
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
1327 class SubclassWithKwargsTest(unittest.TestCase): |
|
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
1328 def test_keywords_in_subclass(self): |
|
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
1329 # count is not subclassable... |
|
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
1330 for cls in (repeat, izip, ifilter, ifilterfalse, chain, imap, |
|
51045
14ace205447b
Promote compress() from a recipe to being a regular itertool.
Raymond Hettinger <python@rcn.com>
parents:
50631
diff
changeset
|
1331 starmap, islice, takewhile, dropwhile, cycle, compress): |
|
40682
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
1332 class Subclass(cls): |
|
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
1333 def __init__(self, newarg=None, *args): |
|
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
1334 cls.__init__(self, *args) |
|
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
1335 try: |
|
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
1336 Subclass(newarg=1) |
|
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
1337 except TypeError, err: |
|
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
1338 # we expect type errors because of wrong argument count |
|
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
1339 self.failIf("does not take keyword arguments" in err.args[0]) |
|
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
1340 |
|
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
1341 |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1342 libreftest = """ Doctest for examples in the library reference: libitertools.tex |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1343 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1344 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1345 >>> amounts = [120.15, 764.05, 823.14] |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1346 >>> for checknum, amount in izip(count(1200), amounts): |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1347 ... print 'Check %d is for $%.2f' % (checknum, amount) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1348 ... |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1349 Check 1200 is for $120.15 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1350 Check 1201 is for $764.05 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1351 Check 1202 is for $823.14 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1352 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1353 >>> import operator |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1354 >>> for cube in imap(operator.pow, xrange(1,4), repeat(3)): |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1355 ... print cube |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1356 ... |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1357 1 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1358 8 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1359 27 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1360 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1361 >>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele'] |
|
29627
e1fa07bf8548
Add take() to examples. Tighten the islice() example
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
1362 >>> for name in islice(reportlines, 3, None, 2): |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1363 ... print name.title() |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1364 ... |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1365 Alex |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1366 Laura |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1367 Martin |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1368 Walter |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1369 Samuele |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1370 |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1371 >>> from operator import itemgetter |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1372 >>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3) |
| 38464 | 1373 >>> di = sorted(sorted(d.iteritems()), key=itemgetter(1)) |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1374 >>> for k, g in groupby(di, itemgetter(1)): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1375 ... print k, map(itemgetter(0), g) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1376 ... |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1377 1 ['a', 'c', 'e'] |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1378 2 ['b', 'd', 'f'] |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1379 3 ['g'] |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1380 |
|
31401
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1381 # Find runs of consecutive numbers using groupby. The key to the solution |
|
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1382 # is differencing with a range so that consecutive numbers all appear in |
|
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1383 # same group. |
|
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1384 >>> data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28] |
|
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1385 >>> for k, g in groupby(enumerate(data), lambda (i,x):i-x): |
|
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1386 ... print map(operator.itemgetter(1), g) |
|
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1387 ... |
|
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1388 [1] |
|
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1389 [4, 5, 6] |
|
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1390 [10] |
|
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1391 [15, 16, 17, 18] |
|
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1392 [22] |
|
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1393 [25, 26, 27, 28] |
|
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1394 |
|
48033
43c6934514d7
Clean-up itertools docs and recipes.
Raymond Hettinger <python@rcn.com>
parents:
48032
diff
changeset
|
1395 >>> def take(n, iterable): |
|
43c6934514d7
Clean-up itertools docs and recipes.
Raymond Hettinger <python@rcn.com>
parents:
48032
diff
changeset
|
1396 ... "Return first n items of the iterable as a list" |
|
43c6934514d7
Clean-up itertools docs and recipes.
Raymond Hettinger <python@rcn.com>
parents:
48032
diff
changeset
|
1397 ... return list(islice(iterable, n)) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1398 |
|
48033
43c6934514d7
Clean-up itertools docs and recipes.
Raymond Hettinger <python@rcn.com>
parents:
48032
diff
changeset
|
1399 >>> def enumerate(iterable, start=0): |
|
43c6934514d7
Clean-up itertools docs and recipes.
Raymond Hettinger <python@rcn.com>
parents:
48032
diff
changeset
|
1400 ... return izip(count(start), iterable) |
|
43c6934514d7
Clean-up itertools docs and recipes.
Raymond Hettinger <python@rcn.com>
parents:
48032
diff
changeset
|
1401 |
|
43c6934514d7
Clean-up itertools docs and recipes.
Raymond Hettinger <python@rcn.com>
parents:
48032
diff
changeset
|
1402 >>> def tabulate(function, start=0): |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1403 ... "Return function(0), function(1), ..." |
|
48033
43c6934514d7
Clean-up itertools docs and recipes.
Raymond Hettinger <python@rcn.com>
parents:
48032
diff
changeset
|
1404 ... return imap(function, count(start)) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1405 |
|
51724
302263e3c8f1
Add some cross-references to the docs. Simplify the python code equivalent for izip(). Supply an optional argument for the nth() recipe.
Raymond Hettinger <python@rcn.com>
parents:
51718
diff
changeset
|
1406 >>> def nth(iterable, n, default=None): |
|
302263e3c8f1
Add some cross-references to the docs. Simplify the python code equivalent for izip(). Supply an optional argument for the nth() recipe.
Raymond Hettinger <python@rcn.com>
parents:
51718
diff
changeset
|
1407 ... "Returns the nth item or a default value" |
|
302263e3c8f1
Add some cross-references to the docs. Simplify the python code equivalent for izip(). Supply an optional argument for the nth() recipe.
Raymond Hettinger <python@rcn.com>
parents:
51718
diff
changeset
|
1408 ... return next(islice(iterable, n, None), default) |
| 27769 | 1409 |
|
48033
43c6934514d7
Clean-up itertools docs and recipes.
Raymond Hettinger <python@rcn.com>
parents:
48032
diff
changeset
|
1410 >>> def quantify(iterable, pred=bool): |
|
43c6934514d7
Clean-up itertools docs and recipes.
Raymond Hettinger <python@rcn.com>
parents:
48032
diff
changeset
|
1411 ... "Count how many times the predicate is true" |
|
43c6934514d7
Clean-up itertools docs and recipes.
Raymond Hettinger <python@rcn.com>
parents:
48032
diff
changeset
|
1412 ... return sum(imap(pred, iterable)) |
| 27769 | 1413 |
|
48033
43c6934514d7
Clean-up itertools docs and recipes.
Raymond Hettinger <python@rcn.com>
parents:
48032
diff
changeset
|
1414 >>> def padnone(iterable): |
|
43c6934514d7
Clean-up itertools docs and recipes.
Raymond Hettinger <python@rcn.com>
parents:
48032
diff
changeset
|
1415 ... "Returns the sequence elements and then returns None indefinitely" |
|
43c6934514d7
Clean-up itertools docs and recipes.
Raymond Hettinger <python@rcn.com>
parents:
48032
diff
changeset
|
1416 ... return chain(iterable, repeat(None)) |
|
30092
d4cce553478a
Re-sync doc tests with the doc updates.
Raymond Hettinger <python@rcn.com>
parents:
29690
diff
changeset
|
1417 |
|
48033
43c6934514d7
Clean-up itertools docs and recipes.
Raymond Hettinger <python@rcn.com>
parents:
48032
diff
changeset
|
1418 >>> def ncycles(iterable, n): |
|
43c6934514d7
Clean-up itertools docs and recipes.
Raymond Hettinger <python@rcn.com>
parents:
48032
diff
changeset
|
1419 ... "Returns the seqeuence elements n times" |
|
43c6934514d7
Clean-up itertools docs and recipes.
Raymond Hettinger <python@rcn.com>
parents:
48032
diff
changeset
|
1420 ... return chain(*repeat(iterable, n)) |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1421 |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1422 >>> def dotproduct(vec1, vec2): |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1423 ... return sum(imap(operator.mul, vec1, vec2)) |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1424 |
| 30764 | 1425 >>> def flatten(listOfLists): |
|
45701
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
1426 ... return list(chain.from_iterable(listOfLists)) |
| 30764 | 1427 |
| 1428 >>> def repeatfunc(func, times=None, *args): | |
| 1429 ... "Repeat calls to func with specified arguments." | |
| 1430 ... " Example: repeatfunc(random.random)" | |
| 1431 ... if times is None: | |
| 1432 ... return starmap(func, repeat(args)) | |
| 1433 ... else: | |
| 1434 ... return starmap(func, repeat(args, times)) | |
| 1435 | |
|
30787
ea058504104c
Replace the window() example with pairwise() which demonstrates tee().
Raymond Hettinger <python@rcn.com>
parents:
30786
diff
changeset
|
1436 >>> def pairwise(iterable): |
|
ea058504104c
Replace the window() example with pairwise() which demonstrates tee().
Raymond Hettinger <python@rcn.com>
parents:
30786
diff
changeset
|
1437 ... "s -> (s0,s1), (s1,s2), (s2, s3), ..." |
|
ea058504104c
Replace the window() example with pairwise() which demonstrates tee().
Raymond Hettinger <python@rcn.com>
parents:
30786
diff
changeset
|
1438 ... a, b = tee(iterable) |
|
45701
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
1439 ... for elem in b: |
|
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
1440 ... break |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
1441 ... return izip(a, b) |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1442 |
|
45701
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
1443 >>> def grouper(n, iterable, fillvalue=None): |
|
48098
689b5e8ae69d
Neaten-up the itertools recipes.
Raymond Hettinger <python@rcn.com>
parents:
48033
diff
changeset
|
1444 ... "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx" |
|
45701
5f49b569dedf
Tweak recipes and tests
Raymond Hettinger <python@rcn.com>
parents:
45699
diff
changeset
|
1445 ... args = [iter(iterable)] * n |
|
48108
5db43284ea7a
Alter recipe to show how to call izip_longest() with
Raymond Hettinger <python@rcn.com>
parents:
48098
diff
changeset
|
1446 ... return izip_longest(fillvalue=fillvalue, *args) |
| 45698 | 1447 |
| 1448 >>> def roundrobin(*iterables): | |
|
48098
689b5e8ae69d
Neaten-up the itertools recipes.
Raymond Hettinger <python@rcn.com>
parents:
48033
diff
changeset
|
1449 ... "roundrobin('ABC', 'D', 'EF') --> A D E B F C" |
| 45698 | 1450 ... # Recipe credited to George Sakkis |
| 1451 ... pending = len(iterables) | |
| 1452 ... nexts = cycle(iter(it).next for it in iterables) | |
| 1453 ... while pending: | |
| 1454 ... try: | |
| 1455 ... for next in nexts: | |
| 1456 ... yield next() | |
| 1457 ... except StopIteration: | |
| 1458 ... pending -= 1 | |
| 1459 ... nexts = cycle(islice(nexts, pending)) | |
| 1460 | |
| 1461 >>> def powerset(iterable): | |
|
51046
e4705ff626f0
Improved itertools recipe for generating powerset().
Raymond Hettinger <python@rcn.com>
parents:
51045
diff
changeset
|
1462 ... "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)" |
|
e4705ff626f0
Improved itertools recipe for generating powerset().
Raymond Hettinger <python@rcn.com>
parents:
51045
diff
changeset
|
1463 ... s = list(iterable) |
|
e4705ff626f0
Improved itertools recipe for generating powerset().
Raymond Hettinger <python@rcn.com>
parents:
51045
diff
changeset
|
1464 ... return chain.from_iterable(combinations(s, r) for r in range(len(s)+1)) |
| 45698 | 1465 |
|
50467
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1466 >>> def unique_everseen(iterable, key=None): |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1467 ... "List unique elements, preserving order. Remember all elements ever seen." |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1468 ... # unique_everseen('AAAABBBCCDAABBB') --> A B C D |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1469 ... # unique_everseen('ABBCcAD', str.lower) --> A B C D |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1470 ... seen = set() |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1471 ... seen_add = seen.add |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1472 ... if key is None: |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1473 ... for element in iterable: |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1474 ... if element not in seen: |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1475 ... seen_add(element) |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1476 ... yield element |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1477 ... else: |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1478 ... for element in iterable: |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1479 ... k = key(element) |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1480 ... if k not in seen: |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1481 ... seen_add(k) |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1482 ... yield element |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1483 |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1484 >>> def unique_justseen(iterable, key=None): |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1485 ... "List unique elements, preserving order. Remember only the element just seen." |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1486 ... # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1487 ... # unique_justseen('ABBCcAD', str.lower) --> A B C A D |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1488 ... return imap(next, imap(itemgetter(1), groupby(iterable, key))) |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1489 |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1490 This is not part of the examples but it tests to make sure the definitions |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1491 perform as purported. |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1492 |
|
30381
a52a074e8ae5
Add an example to address a common question of how to split iterators.
Raymond Hettinger <python@rcn.com>
parents:
30238
diff
changeset
|
1493 >>> take(10, count()) |
|
a52a074e8ae5
Add an example to address a common question of how to split iterators.
Raymond Hettinger <python@rcn.com>
parents:
30238
diff
changeset
|
1494 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
|
a52a074e8ae5
Add an example to address a common question of how to split iterators.
Raymond Hettinger <python@rcn.com>
parents:
30238
diff
changeset
|
1495 |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1496 >>> list(enumerate('abc')) |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1497 [(0, 'a'), (1, 'b'), (2, 'c')] |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1498 |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1499 >>> list(islice(tabulate(lambda x: 2*x), 4)) |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1500 [0, 2, 4, 6] |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1501 |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1502 >>> nth('abcde', 3) |
| 51312 | 1503 'd' |
| 1504 | |
| 1505 >>> nth('abcde', 9) is None | |
| 1506 True | |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1507 |
|
30650
ca030320963f
Adopt Christian Stork's suggested argument order for the logic quantifiers.
Raymond Hettinger <python@rcn.com>
parents:
30419
diff
changeset
|
1508 >>> quantify(xrange(99), lambda x: x%2==0) |
|
30093
0ac4396b1894
Modified itertools.izip() to match the behavior of __builtin__.zip()
Raymond Hettinger <python@rcn.com>
parents:
30092
diff
changeset
|
1509 50 |
|
0ac4396b1894
Modified itertools.izip() to match the behavior of __builtin__.zip()
Raymond Hettinger <python@rcn.com>
parents:
30092
diff
changeset
|
1510 |
| 30764 | 1511 >>> a = [[1, 2, 3], [4, 5, 6]] |
| 1512 >>> flatten(a) | |
| 1513 [1, 2, 3, 4, 5, 6] | |
| 1514 | |
| 1515 >>> list(repeatfunc(pow, 5, 2, 3)) | |
| 1516 [8, 8, 8, 8, 8] | |
| 1517 | |
| 1518 >>> import random | |
| 1519 >>> take(5, imap(int, repeatfunc(random.random))) | |
| 1520 [0, 0, 0, 0, 0] | |
| 1521 | |
|
30787
ea058504104c
Replace the window() example with pairwise() which demonstrates tee().
Raymond Hettinger <python@rcn.com>
parents:
30786
diff
changeset
|
1522 >>> list(pairwise('abcd')) |
|
ea058504104c
Replace the window() example with pairwise() which demonstrates tee().
Raymond Hettinger <python@rcn.com>
parents:
30786
diff
changeset
|
1523 [('a', 'b'), ('b', 'c'), ('c', 'd')] |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1524 |
|
30787
ea058504104c
Replace the window() example with pairwise() which demonstrates tee().
Raymond Hettinger <python@rcn.com>
parents:
30786
diff
changeset
|
1525 >>> list(pairwise([])) |
|
ea058504104c
Replace the window() example with pairwise() which demonstrates tee().
Raymond Hettinger <python@rcn.com>
parents:
30786
diff
changeset
|
1526 [] |
|
ea058504104c
Replace the window() example with pairwise() which demonstrates tee().
Raymond Hettinger <python@rcn.com>
parents:
30786
diff
changeset
|
1527 |
|
ea058504104c
Replace the window() example with pairwise() which demonstrates tee().
Raymond Hettinger <python@rcn.com>
parents:
30786
diff
changeset
|
1528 >>> list(pairwise('a')) |
| 29535 | 1529 [] |
| 1530 | |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1531 >>> list(islice(padnone('abc'), 0, 6)) |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1532 ['a', 'b', 'c', None, None, None] |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1533 |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1534 >>> list(ncycles('abc', 3)) |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1535 ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'] |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1536 |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1537 >>> dotproduct([1,2,3], [4,5,6]) |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1538 32 |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1539 |
| 45698 | 1540 >>> list(grouper(3, 'abcdefg', 'x')) |
| 1541 [('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')] | |
| 1542 | |
| 1543 >>> list(roundrobin('abc', 'd', 'ef')) | |
| 1544 ['a', 'd', 'e', 'b', 'f', 'c'] | |
| 1545 | |
|
51046
e4705ff626f0
Improved itertools recipe for generating powerset().
Raymond Hettinger <python@rcn.com>
parents:
51045
diff
changeset
|
1546 >>> list(powerset([1,2,3])) |
|
e4705ff626f0
Improved itertools recipe for generating powerset().
Raymond Hettinger <python@rcn.com>
parents:
51045
diff
changeset
|
1547 [(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)] |
| 45698 | 1548 |
|
51119
9a4fdbcf2d9f
Add more tests for the powerset() recipe.
Raymond Hettinger <python@rcn.com>
parents:
51114
diff
changeset
|
1549 >>> all(len(list(powerset(range(n)))) == 2**n for n in range(18)) |
|
9a4fdbcf2d9f
Add more tests for the powerset() recipe.
Raymond Hettinger <python@rcn.com>
parents:
51114
diff
changeset
|
1550 True |
|
9a4fdbcf2d9f
Add more tests for the powerset() recipe.
Raymond Hettinger <python@rcn.com>
parents:
51114
diff
changeset
|
1551 |
|
9a4fdbcf2d9f
Add more tests for the powerset() recipe.
Raymond Hettinger <python@rcn.com>
parents:
51114
diff
changeset
|
1552 >>> list(powerset('abcde')) == sorted(sorted(set(powerset('abcde'))), key=len) |
|
9a4fdbcf2d9f
Add more tests for the powerset() recipe.
Raymond Hettinger <python@rcn.com>
parents:
51114
diff
changeset
|
1553 True |
|
9a4fdbcf2d9f
Add more tests for the powerset() recipe.
Raymond Hettinger <python@rcn.com>
parents:
51114
diff
changeset
|
1554 |
|
50467
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1555 >>> list(unique_everseen('AAAABBBCCDAABBB')) |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1556 ['A', 'B', 'C', 'D'] |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1557 |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1558 >>> list(unique_everseen('ABBCcAD', str.lower)) |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1559 ['A', 'B', 'C', 'D'] |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1560 |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1561 >>> list(unique_justseen('AAAABBBCCDAABBB')) |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1562 ['A', 'B', 'C', 'D', 'A', 'B'] |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1563 |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1564 >>> list(unique_justseen('ABBCcAD', str.lower)) |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1565 ['A', 'B', 'C', 'A', 'D'] |
|
0052d23d39f6
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48108
diff
changeset
|
1566 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1567 """ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1568 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1569 __test__ = {'libreftest' : libreftest} |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1570 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1571 def test_main(verbose=None): |
|
30238
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1572 test_classes = (TestBasicOps, TestVariousIteratorArgs, TestGC, |
|
40682
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
1573 RegressionTests, LengthTransparency, |
| 45698 | 1574 SubclassWithKwargsTest, TestExamples) |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1575 test_support.run_unittest(*test_classes) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1576 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1577 # verify reference counting |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1578 if verbose and hasattr(sys, "gettotalrefcount"): |
| 29690 | 1579 import gc |
|
28936
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
1580 counts = [None] * 5 |
|
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
1581 for i in xrange(len(counts)): |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1582 test_support.run_unittest(*test_classes) |
| 29690 | 1583 gc.collect() |
|
28936
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
1584 counts[i] = sys.gettotalrefcount() |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1585 print counts |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1586 |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1587 # doctest the examples in the library reference |
| 29139 | 1588 test_support.run_doctest(sys.modules[__name__], verbose) |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1589 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1590 if __name__ == "__main__": |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1591 test_main(verbose=True) |
