annotate Lib/test/test_itertools.py @ 79675:c0a2294de787 2.7

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