1 | n/a | import unittest |
---|
2 | n/a | from test import support |
---|
3 | n/a | from itertools import * |
---|
4 | n/a | import weakref |
---|
5 | n/a | from decimal import Decimal |
---|
6 | n/a | from fractions import Fraction |
---|
7 | n/a | import operator |
---|
8 | n/a | import random |
---|
9 | n/a | import copy |
---|
10 | n/a | import pickle |
---|
11 | n/a | from functools import reduce |
---|
12 | n/a | import sys |
---|
13 | n/a | import struct |
---|
14 | n/a | maxsize = support.MAX_Py_ssize_t |
---|
15 | n/a | minsize = -maxsize-1 |
---|
16 | n/a | |
---|
17 | n/a | def lzip(*args): |
---|
18 | n/a | return list(zip(*args)) |
---|
19 | n/a | |
---|
20 | n/a | def onearg(x): |
---|
21 | n/a | 'Test function of one argument' |
---|
22 | n/a | return 2*x |
---|
23 | n/a | |
---|
24 | n/a | def errfunc(*args): |
---|
25 | n/a | 'Test function that raises an error' |
---|
26 | n/a | raise ValueError |
---|
27 | n/a | |
---|
28 | n/a | def gen3(): |
---|
29 | n/a | 'Non-restartable source sequence' |
---|
30 | n/a | for i in (0, 1, 2): |
---|
31 | n/a | yield i |
---|
32 | n/a | |
---|
33 | n/a | def isEven(x): |
---|
34 | n/a | 'Test predicate' |
---|
35 | n/a | return x%2==0 |
---|
36 | n/a | |
---|
37 | n/a | def isOdd(x): |
---|
38 | n/a | 'Test predicate' |
---|
39 | n/a | return x%2==1 |
---|
40 | n/a | |
---|
41 | n/a | def tupleize(*args): |
---|
42 | n/a | return args |
---|
43 | n/a | |
---|
44 | n/a | def irange(n): |
---|
45 | n/a | for i in range(n): |
---|
46 | n/a | yield i |
---|
47 | n/a | |
---|
48 | n/a | class StopNow: |
---|
49 | n/a | 'Class emulating an empty iterable.' |
---|
50 | n/a | def __iter__(self): |
---|
51 | n/a | return self |
---|
52 | n/a | def __next__(self): |
---|
53 | n/a | raise StopIteration |
---|
54 | n/a | |
---|
55 | n/a | def take(n, seq): |
---|
56 | n/a | 'Convenience function for partially consuming a long of infinite iterable' |
---|
57 | n/a | return list(islice(seq, n)) |
---|
58 | n/a | |
---|
59 | n/a | def prod(iterable): |
---|
60 | n/a | return reduce(operator.mul, iterable, 1) |
---|
61 | n/a | |
---|
62 | n/a | def fact(n): |
---|
63 | n/a | 'Factorial' |
---|
64 | n/a | return prod(range(1, n+1)) |
---|
65 | n/a | |
---|
66 | n/a | # root level methods for pickling ability |
---|
67 | n/a | def testR(r): |
---|
68 | n/a | return r[0] |
---|
69 | n/a | |
---|
70 | n/a | def testR2(r): |
---|
71 | n/a | return r[2] |
---|
72 | n/a | |
---|
73 | n/a | def underten(x): |
---|
74 | n/a | return x<10 |
---|
75 | n/a | |
---|
76 | n/a | picklecopiers = [lambda s, proto=proto: pickle.loads(pickle.dumps(s, proto)) |
---|
77 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1)] |
---|
78 | n/a | |
---|
79 | n/a | class TestBasicOps(unittest.TestCase): |
---|
80 | n/a | |
---|
81 | n/a | def pickletest(self, protocol, it, stop=4, take=1, compare=None): |
---|
82 | n/a | """Test that an iterator is the same after pickling, also when part-consumed""" |
---|
83 | n/a | def expand(it, i=0): |
---|
84 | n/a | # Recursively expand iterables, within sensible bounds |
---|
85 | n/a | if i > 10: |
---|
86 | n/a | raise RuntimeError("infinite recursion encountered") |
---|
87 | n/a | if isinstance(it, str): |
---|
88 | n/a | return it |
---|
89 | n/a | try: |
---|
90 | n/a | l = list(islice(it, stop)) |
---|
91 | n/a | except TypeError: |
---|
92 | n/a | return it # can't expand it |
---|
93 | n/a | return [expand(e, i+1) for e in l] |
---|
94 | n/a | |
---|
95 | n/a | # Test the initial copy against the original |
---|
96 | n/a | dump = pickle.dumps(it, protocol) |
---|
97 | n/a | i2 = pickle.loads(dump) |
---|
98 | n/a | self.assertEqual(type(it), type(i2)) |
---|
99 | n/a | a, b = expand(it), expand(i2) |
---|
100 | n/a | self.assertEqual(a, b) |
---|
101 | n/a | if compare: |
---|
102 | n/a | c = expand(compare) |
---|
103 | n/a | self.assertEqual(a, c) |
---|
104 | n/a | |
---|
105 | n/a | # Take from the copy, and create another copy and compare them. |
---|
106 | n/a | i3 = pickle.loads(dump) |
---|
107 | n/a | took = 0 |
---|
108 | n/a | try: |
---|
109 | n/a | for i in range(take): |
---|
110 | n/a | next(i3) |
---|
111 | n/a | took += 1 |
---|
112 | n/a | except StopIteration: |
---|
113 | n/a | pass #in case there is less data than 'take' |
---|
114 | n/a | dump = pickle.dumps(i3, protocol) |
---|
115 | n/a | i4 = pickle.loads(dump) |
---|
116 | n/a | a, b = expand(i3), expand(i4) |
---|
117 | n/a | self.assertEqual(a, b) |
---|
118 | n/a | if compare: |
---|
119 | n/a | c = expand(compare[took:]) |
---|
120 | n/a | self.assertEqual(a, c); |
---|
121 | n/a | |
---|
122 | n/a | def test_accumulate(self): |
---|
123 | n/a | self.assertEqual(list(accumulate(range(10))), # one positional arg |
---|
124 | n/a | [0, 1, 3, 6, 10, 15, 21, 28, 36, 45]) |
---|
125 | n/a | self.assertEqual(list(accumulate(iterable=range(10))), # kw arg |
---|
126 | n/a | [0, 1, 3, 6, 10, 15, 21, 28, 36, 45]) |
---|
127 | n/a | for typ in int, complex, Decimal, Fraction: # multiple types |
---|
128 | n/a | self.assertEqual( |
---|
129 | n/a | list(accumulate(map(typ, range(10)))), |
---|
130 | n/a | list(map(typ, [0, 1, 3, 6, 10, 15, 21, 28, 36, 45]))) |
---|
131 | n/a | self.assertEqual(list(accumulate('abc')), ['a', 'ab', 'abc']) # works with non-numeric |
---|
132 | n/a | self.assertEqual(list(accumulate([])), []) # empty iterable |
---|
133 | n/a | self.assertEqual(list(accumulate([7])), [7]) # iterable of length one |
---|
134 | n/a | self.assertRaises(TypeError, accumulate, range(10), 5, 6) # too many args |
---|
135 | n/a | self.assertRaises(TypeError, accumulate) # too few args |
---|
136 | n/a | self.assertRaises(TypeError, accumulate, x=range(10)) # unexpected kwd arg |
---|
137 | n/a | self.assertRaises(TypeError, list, accumulate([1, []])) # args that don't add |
---|
138 | n/a | |
---|
139 | n/a | s = [2, 8, 9, 5, 7, 0, 3, 4, 1, 6] |
---|
140 | n/a | self.assertEqual(list(accumulate(s, min)), |
---|
141 | n/a | [2, 2, 2, 2, 2, 0, 0, 0, 0, 0]) |
---|
142 | n/a | self.assertEqual(list(accumulate(s, max)), |
---|
143 | n/a | [2, 8, 9, 9, 9, 9, 9, 9, 9, 9]) |
---|
144 | n/a | self.assertEqual(list(accumulate(s, operator.mul)), |
---|
145 | n/a | [2, 16, 144, 720, 5040, 0, 0, 0, 0, 0]) |
---|
146 | n/a | with self.assertRaises(TypeError): |
---|
147 | n/a | list(accumulate(s, chr)) # unary-operation |
---|
148 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
149 | n/a | self.pickletest(proto, accumulate(range(10))) # test pickling |
---|
150 | n/a | |
---|
151 | n/a | def test_chain(self): |
---|
152 | n/a | |
---|
153 | n/a | def chain2(*iterables): |
---|
154 | n/a | 'Pure python version in the docs' |
---|
155 | n/a | for it in iterables: |
---|
156 | n/a | for element in it: |
---|
157 | n/a | yield element |
---|
158 | n/a | |
---|
159 | n/a | for c in (chain, chain2): |
---|
160 | n/a | self.assertEqual(list(c('abc', 'def')), list('abcdef')) |
---|
161 | n/a | self.assertEqual(list(c('abc')), list('abc')) |
---|
162 | n/a | self.assertEqual(list(c('')), []) |
---|
163 | n/a | self.assertEqual(take(4, c('abc', 'def')), list('abcd')) |
---|
164 | n/a | self.assertRaises(TypeError, list,c(2, 3)) |
---|
165 | n/a | |
---|
166 | n/a | def test_chain_from_iterable(self): |
---|
167 | n/a | self.assertEqual(list(chain.from_iterable(['abc', 'def'])), list('abcdef')) |
---|
168 | n/a | self.assertEqual(list(chain.from_iterable(['abc'])), list('abc')) |
---|
169 | n/a | self.assertEqual(list(chain.from_iterable([''])), []) |
---|
170 | n/a | self.assertEqual(take(4, chain.from_iterable(['abc', 'def'])), list('abcd')) |
---|
171 | n/a | self.assertRaises(TypeError, list, chain.from_iterable([2, 3])) |
---|
172 | n/a | |
---|
173 | n/a | def test_chain_reducible(self): |
---|
174 | n/a | for oper in [copy.deepcopy] + picklecopiers: |
---|
175 | n/a | it = chain('abc', 'def') |
---|
176 | n/a | self.assertEqual(list(oper(it)), list('abcdef')) |
---|
177 | n/a | self.assertEqual(next(it), 'a') |
---|
178 | n/a | self.assertEqual(list(oper(it)), list('bcdef')) |
---|
179 | n/a | |
---|
180 | n/a | self.assertEqual(list(oper(chain(''))), []) |
---|
181 | n/a | self.assertEqual(take(4, oper(chain('abc', 'def'))), list('abcd')) |
---|
182 | n/a | self.assertRaises(TypeError, list, oper(chain(2, 3))) |
---|
183 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
184 | n/a | self.pickletest(proto, chain('abc', 'def'), compare=list('abcdef')) |
---|
185 | n/a | |
---|
186 | n/a | def test_chain_setstate(self): |
---|
187 | n/a | self.assertRaises(TypeError, chain().__setstate__, ()) |
---|
188 | n/a | self.assertRaises(TypeError, chain().__setstate__, []) |
---|
189 | n/a | self.assertRaises(TypeError, chain().__setstate__, 0) |
---|
190 | n/a | self.assertRaises(TypeError, chain().__setstate__, ([],)) |
---|
191 | n/a | self.assertRaises(TypeError, chain().__setstate__, (iter([]), [])) |
---|
192 | n/a | it = chain() |
---|
193 | n/a | it.__setstate__((iter(['abc', 'def']),)) |
---|
194 | n/a | self.assertEqual(list(it), ['a', 'b', 'c', 'd', 'e', 'f']) |
---|
195 | n/a | it = chain() |
---|
196 | n/a | it.__setstate__((iter(['abc', 'def']), iter(['ghi']))) |
---|
197 | n/a | self.assertEqual(list(it), ['ghi', 'a', 'b', 'c', 'd', 'e', 'f']) |
---|
198 | n/a | |
---|
199 | n/a | def test_combinations(self): |
---|
200 | n/a | self.assertRaises(TypeError, combinations, 'abc') # missing r argument |
---|
201 | n/a | self.assertRaises(TypeError, combinations, 'abc', 2, 1) # too many arguments |
---|
202 | n/a | self.assertRaises(TypeError, combinations, None) # pool is not iterable |
---|
203 | n/a | self.assertRaises(ValueError, combinations, 'abc', -2) # r is negative |
---|
204 | n/a | |
---|
205 | n/a | for op in [lambda a:a] + picklecopiers: |
---|
206 | n/a | self.assertEqual(list(op(combinations('abc', 32))), []) # r > n |
---|
207 | n/a | |
---|
208 | n/a | self.assertEqual(list(op(combinations('ABCD', 2))), |
---|
209 | n/a | [('A','B'), ('A','C'), ('A','D'), ('B','C'), ('B','D'), ('C','D')]) |
---|
210 | n/a | testIntermediate = combinations('ABCD', 2) |
---|
211 | n/a | next(testIntermediate) |
---|
212 | n/a | self.assertEqual(list(op(testIntermediate)), |
---|
213 | n/a | [('A','C'), ('A','D'), ('B','C'), ('B','D'), ('C','D')]) |
---|
214 | n/a | |
---|
215 | n/a | self.assertEqual(list(op(combinations(range(4), 3))), |
---|
216 | n/a | [(0,1,2), (0,1,3), (0,2,3), (1,2,3)]) |
---|
217 | n/a | testIntermediate = combinations(range(4), 3) |
---|
218 | n/a | next(testIntermediate) |
---|
219 | n/a | self.assertEqual(list(op(testIntermediate)), |
---|
220 | n/a | [(0,1,3), (0,2,3), (1,2,3)]) |
---|
221 | n/a | |
---|
222 | n/a | |
---|
223 | n/a | def combinations1(iterable, r): |
---|
224 | n/a | 'Pure python version shown in the docs' |
---|
225 | n/a | pool = tuple(iterable) |
---|
226 | n/a | n = len(pool) |
---|
227 | n/a | if r > n: |
---|
228 | n/a | return |
---|
229 | n/a | indices = list(range(r)) |
---|
230 | n/a | yield tuple(pool[i] for i in indices) |
---|
231 | n/a | while 1: |
---|
232 | n/a | for i in reversed(range(r)): |
---|
233 | n/a | if indices[i] != i + n - r: |
---|
234 | n/a | break |
---|
235 | n/a | else: |
---|
236 | n/a | return |
---|
237 | n/a | indices[i] += 1 |
---|
238 | n/a | for j in range(i+1, r): |
---|
239 | n/a | indices[j] = indices[j-1] + 1 |
---|
240 | n/a | yield tuple(pool[i] for i in indices) |
---|
241 | n/a | |
---|
242 | n/a | def combinations2(iterable, r): |
---|
243 | n/a | 'Pure python version shown in the docs' |
---|
244 | n/a | pool = tuple(iterable) |
---|
245 | n/a | n = len(pool) |
---|
246 | n/a | for indices in permutations(range(n), r): |
---|
247 | n/a | if sorted(indices) == list(indices): |
---|
248 | n/a | yield tuple(pool[i] for i in indices) |
---|
249 | n/a | |
---|
250 | n/a | def combinations3(iterable, r): |
---|
251 | n/a | 'Pure python version from cwr()' |
---|
252 | n/a | pool = tuple(iterable) |
---|
253 | n/a | n = len(pool) |
---|
254 | n/a | for indices in combinations_with_replacement(range(n), r): |
---|
255 | n/a | if len(set(indices)) == r: |
---|
256 | n/a | yield tuple(pool[i] for i in indices) |
---|
257 | n/a | |
---|
258 | n/a | for n in range(7): |
---|
259 | n/a | values = [5*x-12 for x in range(n)] |
---|
260 | n/a | for r in range(n+2): |
---|
261 | n/a | result = list(combinations(values, r)) |
---|
262 | n/a | self.assertEqual(len(result), 0 if r>n else fact(n) / fact(r) / fact(n-r)) # right number of combs |
---|
263 | n/a | self.assertEqual(len(result), len(set(result))) # no repeats |
---|
264 | n/a | self.assertEqual(result, sorted(result)) # lexicographic order |
---|
265 | n/a | for c in result: |
---|
266 | n/a | self.assertEqual(len(c), r) # r-length combinations |
---|
267 | n/a | self.assertEqual(len(set(c)), r) # no duplicate elements |
---|
268 | n/a | self.assertEqual(list(c), sorted(c)) # keep original ordering |
---|
269 | n/a | self.assertTrue(all(e in values for e in c)) # elements taken from input iterable |
---|
270 | n/a | self.assertEqual(list(c), |
---|
271 | n/a | [e for e in values if e in c]) # comb is a subsequence of the input iterable |
---|
272 | n/a | self.assertEqual(result, list(combinations1(values, r))) # matches first pure python version |
---|
273 | n/a | self.assertEqual(result, list(combinations2(values, r))) # matches second pure python version |
---|
274 | n/a | self.assertEqual(result, list(combinations3(values, r))) # matches second pure python version |
---|
275 | n/a | |
---|
276 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
277 | n/a | self.pickletest(proto, combinations(values, r)) # test pickling |
---|
278 | n/a | |
---|
279 | n/a | @support.bigaddrspacetest |
---|
280 | n/a | def test_combinations_overflow(self): |
---|
281 | n/a | with self.assertRaises((OverflowError, MemoryError)): |
---|
282 | n/a | combinations("AA", 2**29) |
---|
283 | n/a | |
---|
284 | n/a | # Test implementation detail: tuple re-use |
---|
285 | n/a | @support.impl_detail("tuple reuse is specific to CPython") |
---|
286 | n/a | def test_combinations_tuple_reuse(self): |
---|
287 | n/a | self.assertEqual(len(set(map(id, combinations('abcde', 3)))), 1) |
---|
288 | n/a | self.assertNotEqual(len(set(map(id, list(combinations('abcde', 3))))), 1) |
---|
289 | n/a | |
---|
290 | n/a | def test_combinations_with_replacement(self): |
---|
291 | n/a | cwr = combinations_with_replacement |
---|
292 | n/a | self.assertRaises(TypeError, cwr, 'abc') # missing r argument |
---|
293 | n/a | self.assertRaises(TypeError, cwr, 'abc', 2, 1) # too many arguments |
---|
294 | n/a | self.assertRaises(TypeError, cwr, None) # pool is not iterable |
---|
295 | n/a | self.assertRaises(ValueError, cwr, 'abc', -2) # r is negative |
---|
296 | n/a | |
---|
297 | n/a | for op in [lambda a:a] + picklecopiers: |
---|
298 | n/a | self.assertEqual(list(op(cwr('ABC', 2))), |
---|
299 | n/a | [('A','A'), ('A','B'), ('A','C'), ('B','B'), ('B','C'), ('C','C')]) |
---|
300 | n/a | testIntermediate = cwr('ABC', 2) |
---|
301 | n/a | next(testIntermediate) |
---|
302 | n/a | self.assertEqual(list(op(testIntermediate)), |
---|
303 | n/a | [('A','B'), ('A','C'), ('B','B'), ('B','C'), ('C','C')]) |
---|
304 | n/a | |
---|
305 | n/a | |
---|
306 | n/a | def cwr1(iterable, r): |
---|
307 | n/a | 'Pure python version shown in the docs' |
---|
308 | n/a | # number items returned: (n+r-1)! / r! / (n-1)! when n>0 |
---|
309 | n/a | pool = tuple(iterable) |
---|
310 | n/a | n = len(pool) |
---|
311 | n/a | if not n and r: |
---|
312 | n/a | return |
---|
313 | n/a | indices = [0] * r |
---|
314 | n/a | yield tuple(pool[i] for i in indices) |
---|
315 | n/a | while 1: |
---|
316 | n/a | for i in reversed(range(r)): |
---|
317 | n/a | if indices[i] != n - 1: |
---|
318 | n/a | break |
---|
319 | n/a | else: |
---|
320 | n/a | return |
---|
321 | n/a | indices[i:] = [indices[i] + 1] * (r - i) |
---|
322 | n/a | yield tuple(pool[i] for i in indices) |
---|
323 | n/a | |
---|
324 | n/a | def cwr2(iterable, r): |
---|
325 | n/a | 'Pure python version shown in the docs' |
---|
326 | n/a | pool = tuple(iterable) |
---|
327 | n/a | n = len(pool) |
---|
328 | n/a | for indices in product(range(n), repeat=r): |
---|
329 | n/a | if sorted(indices) == list(indices): |
---|
330 | n/a | yield tuple(pool[i] for i in indices) |
---|
331 | n/a | |
---|
332 | n/a | def numcombs(n, r): |
---|
333 | n/a | if not n: |
---|
334 | n/a | return 0 if r else 1 |
---|
335 | n/a | return fact(n+r-1) / fact(r)/ fact(n-1) |
---|
336 | n/a | |
---|
337 | n/a | for n in range(7): |
---|
338 | n/a | values = [5*x-12 for x in range(n)] |
---|
339 | n/a | for r in range(n+2): |
---|
340 | n/a | result = list(cwr(values, r)) |
---|
341 | n/a | |
---|
342 | n/a | self.assertEqual(len(result), numcombs(n, r)) # right number of combs |
---|
343 | n/a | self.assertEqual(len(result), len(set(result))) # no repeats |
---|
344 | n/a | self.assertEqual(result, sorted(result)) # lexicographic order |
---|
345 | n/a | |
---|
346 | n/a | regular_combs = list(combinations(values, r)) # compare to combs without replacement |
---|
347 | n/a | if n == 0 or r <= 1: |
---|
348 | n/a | self.assertEqual(result, regular_combs) # cases that should be identical |
---|
349 | n/a | else: |
---|
350 | n/a | self.assertTrue(set(result) >= set(regular_combs)) # rest should be supersets of regular combs |
---|
351 | n/a | |
---|
352 | n/a | for c in result: |
---|
353 | n/a | self.assertEqual(len(c), r) # r-length combinations |
---|
354 | n/a | noruns = [k for k,v in groupby(c)] # combo without consecutive repeats |
---|
355 | n/a | self.assertEqual(len(noruns), len(set(noruns))) # no repeats other than consecutive |
---|
356 | n/a | self.assertEqual(list(c), sorted(c)) # keep original ordering |
---|
357 | n/a | self.assertTrue(all(e in values for e in c)) # elements taken from input iterable |
---|
358 | n/a | self.assertEqual(noruns, |
---|
359 | n/a | [e for e in values if e in c]) # comb is a subsequence of the input iterable |
---|
360 | n/a | self.assertEqual(result, list(cwr1(values, r))) # matches first pure python version |
---|
361 | n/a | self.assertEqual(result, list(cwr2(values, r))) # matches second pure python version |
---|
362 | n/a | |
---|
363 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
364 | n/a | self.pickletest(proto, cwr(values,r)) # test pickling |
---|
365 | n/a | |
---|
366 | n/a | @support.bigaddrspacetest |
---|
367 | n/a | def test_combinations_with_replacement_overflow(self): |
---|
368 | n/a | with self.assertRaises((OverflowError, MemoryError)): |
---|
369 | n/a | combinations_with_replacement("AA", 2**30) |
---|
370 | n/a | |
---|
371 | n/a | # Test implementation detail: tuple re-use |
---|
372 | n/a | @support.impl_detail("tuple reuse is specific to CPython") |
---|
373 | n/a | def test_combinations_with_replacement_tuple_reuse(self): |
---|
374 | n/a | cwr = combinations_with_replacement |
---|
375 | n/a | self.assertEqual(len(set(map(id, cwr('abcde', 3)))), 1) |
---|
376 | n/a | self.assertNotEqual(len(set(map(id, list(cwr('abcde', 3))))), 1) |
---|
377 | n/a | |
---|
378 | n/a | def test_permutations(self): |
---|
379 | n/a | self.assertRaises(TypeError, permutations) # too few arguments |
---|
380 | n/a | self.assertRaises(TypeError, permutations, 'abc', 2, 1) # too many arguments |
---|
381 | n/a | self.assertRaises(TypeError, permutations, None) # pool is not iterable |
---|
382 | n/a | self.assertRaises(ValueError, permutations, 'abc', -2) # r is negative |
---|
383 | n/a | self.assertEqual(list(permutations('abc', 32)), []) # r > n |
---|
384 | n/a | self.assertRaises(TypeError, permutations, 'abc', 's') # r is not an int or None |
---|
385 | n/a | self.assertEqual(list(permutations(range(3), 2)), |
---|
386 | n/a | [(0,1), (0,2), (1,0), (1,2), (2,0), (2,1)]) |
---|
387 | n/a | |
---|
388 | n/a | def permutations1(iterable, r=None): |
---|
389 | n/a | 'Pure python version shown in the docs' |
---|
390 | n/a | pool = tuple(iterable) |
---|
391 | n/a | n = len(pool) |
---|
392 | n/a | r = n if r is None else r |
---|
393 | n/a | if r > n: |
---|
394 | n/a | return |
---|
395 | n/a | indices = list(range(n)) |
---|
396 | n/a | cycles = list(range(n-r+1, n+1))[::-1] |
---|
397 | n/a | yield tuple(pool[i] for i in indices[:r]) |
---|
398 | n/a | while n: |
---|
399 | n/a | for i in reversed(range(r)): |
---|
400 | n/a | cycles[i] -= 1 |
---|
401 | n/a | if cycles[i] == 0: |
---|
402 | n/a | indices[i:] = indices[i+1:] + indices[i:i+1] |
---|
403 | n/a | cycles[i] = n - i |
---|
404 | n/a | else: |
---|
405 | n/a | j = cycles[i] |
---|
406 | n/a | indices[i], indices[-j] = indices[-j], indices[i] |
---|
407 | n/a | yield tuple(pool[i] for i in indices[:r]) |
---|
408 | n/a | break |
---|
409 | n/a | else: |
---|
410 | n/a | return |
---|
411 | n/a | |
---|
412 | n/a | def permutations2(iterable, r=None): |
---|
413 | n/a | 'Pure python version shown in the docs' |
---|
414 | n/a | pool = tuple(iterable) |
---|
415 | n/a | n = len(pool) |
---|
416 | n/a | r = n if r is None else r |
---|
417 | n/a | for indices in product(range(n), repeat=r): |
---|
418 | n/a | if len(set(indices)) == r: |
---|
419 | n/a | yield tuple(pool[i] for i in indices) |
---|
420 | n/a | |
---|
421 | n/a | for n in range(7): |
---|
422 | n/a | values = [5*x-12 for x in range(n)] |
---|
423 | n/a | for r in range(n+2): |
---|
424 | n/a | result = list(permutations(values, r)) |
---|
425 | n/a | self.assertEqual(len(result), 0 if r>n else fact(n) / fact(n-r)) # right number of perms |
---|
426 | n/a | self.assertEqual(len(result), len(set(result))) # no repeats |
---|
427 | n/a | self.assertEqual(result, sorted(result)) # lexicographic order |
---|
428 | n/a | for p in result: |
---|
429 | n/a | self.assertEqual(len(p), r) # r-length permutations |
---|
430 | n/a | self.assertEqual(len(set(p)), r) # no duplicate elements |
---|
431 | n/a | self.assertTrue(all(e in values for e in p)) # elements taken from input iterable |
---|
432 | n/a | self.assertEqual(result, list(permutations1(values, r))) # matches first pure python version |
---|
433 | n/a | self.assertEqual(result, list(permutations2(values, r))) # matches second pure python version |
---|
434 | n/a | if r == n: |
---|
435 | n/a | self.assertEqual(result, list(permutations(values, None))) # test r as None |
---|
436 | n/a | self.assertEqual(result, list(permutations(values))) # test default r |
---|
437 | n/a | |
---|
438 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
439 | n/a | self.pickletest(proto, permutations(values, r)) # test pickling |
---|
440 | n/a | |
---|
441 | n/a | @support.bigaddrspacetest |
---|
442 | n/a | def test_permutations_overflow(self): |
---|
443 | n/a | with self.assertRaises((OverflowError, MemoryError)): |
---|
444 | n/a | permutations("A", 2**30) |
---|
445 | n/a | |
---|
446 | n/a | @support.impl_detail("tuple reuse is specific to CPython") |
---|
447 | n/a | def test_permutations_tuple_reuse(self): |
---|
448 | n/a | self.assertEqual(len(set(map(id, permutations('abcde', 3)))), 1) |
---|
449 | n/a | self.assertNotEqual(len(set(map(id, list(permutations('abcde', 3))))), 1) |
---|
450 | n/a | |
---|
451 | n/a | def test_combinatorics(self): |
---|
452 | n/a | # Test relationships between product(), permutations(), |
---|
453 | n/a | # combinations() and combinations_with_replacement(). |
---|
454 | n/a | |
---|
455 | n/a | for n in range(6): |
---|
456 | n/a | s = 'ABCDEFG'[:n] |
---|
457 | n/a | for r in range(8): |
---|
458 | n/a | prod = list(product(s, repeat=r)) |
---|
459 | n/a | cwr = list(combinations_with_replacement(s, r)) |
---|
460 | n/a | perm = list(permutations(s, r)) |
---|
461 | n/a | comb = list(combinations(s, r)) |
---|
462 | n/a | |
---|
463 | n/a | # Check size |
---|
464 | n/a | self.assertEqual(len(prod), n**r) |
---|
465 | n/a | self.assertEqual(len(cwr), (fact(n+r-1) / fact(r)/ fact(n-1)) if n else (not r)) |
---|
466 | n/a | self.assertEqual(len(perm), 0 if r>n else fact(n) / fact(n-r)) |
---|
467 | n/a | self.assertEqual(len(comb), 0 if r>n else fact(n) / fact(r) / fact(n-r)) |
---|
468 | n/a | |
---|
469 | n/a | # Check lexicographic order without repeated tuples |
---|
470 | n/a | self.assertEqual(prod, sorted(set(prod))) |
---|
471 | n/a | self.assertEqual(cwr, sorted(set(cwr))) |
---|
472 | n/a | self.assertEqual(perm, sorted(set(perm))) |
---|
473 | n/a | self.assertEqual(comb, sorted(set(comb))) |
---|
474 | n/a | |
---|
475 | n/a | # Check interrelationships |
---|
476 | n/a | self.assertEqual(cwr, [t for t in prod if sorted(t)==list(t)]) # cwr: prods which are sorted |
---|
477 | n/a | self.assertEqual(perm, [t for t in prod if len(set(t))==r]) # perm: prods with no dups |
---|
478 | n/a | self.assertEqual(comb, [t for t in perm if sorted(t)==list(t)]) # comb: perms that are sorted |
---|
479 | n/a | self.assertEqual(comb, [t for t in cwr if len(set(t))==r]) # comb: cwrs without dups |
---|
480 | n/a | self.assertEqual(comb, list(filter(set(cwr).__contains__, perm))) # comb: perm that is a cwr |
---|
481 | n/a | self.assertEqual(comb, list(filter(set(perm).__contains__, cwr))) # comb: cwr that is a perm |
---|
482 | n/a | self.assertEqual(comb, sorted(set(cwr) & set(perm))) # comb: both a cwr and a perm |
---|
483 | n/a | |
---|
484 | n/a | def test_compress(self): |
---|
485 | n/a | self.assertEqual(list(compress(data='ABCDEF', selectors=[1,0,1,0,1,1])), list('ACEF')) |
---|
486 | n/a | self.assertEqual(list(compress('ABCDEF', [1,0,1,0,1,1])), list('ACEF')) |
---|
487 | n/a | self.assertEqual(list(compress('ABCDEF', [0,0,0,0,0,0])), list('')) |
---|
488 | n/a | self.assertEqual(list(compress('ABCDEF', [1,1,1,1,1,1])), list('ABCDEF')) |
---|
489 | n/a | self.assertEqual(list(compress('ABCDEF', [1,0,1])), list('AC')) |
---|
490 | n/a | self.assertEqual(list(compress('ABC', [0,1,1,1,1,1])), list('BC')) |
---|
491 | n/a | n = 10000 |
---|
492 | n/a | data = chain.from_iterable(repeat(range(6), n)) |
---|
493 | n/a | selectors = chain.from_iterable(repeat((0, 1))) |
---|
494 | n/a | self.assertEqual(list(compress(data, selectors)), [1,3,5] * n) |
---|
495 | n/a | self.assertRaises(TypeError, compress, None, range(6)) # 1st arg not iterable |
---|
496 | n/a | self.assertRaises(TypeError, compress, range(6), None) # 2nd arg not iterable |
---|
497 | n/a | self.assertRaises(TypeError, compress, range(6)) # too few args |
---|
498 | n/a | self.assertRaises(TypeError, compress, range(6), None) # too many args |
---|
499 | n/a | |
---|
500 | n/a | # check copy, deepcopy, pickle |
---|
501 | n/a | for op in [lambda a:copy.copy(a), lambda a:copy.deepcopy(a)] + picklecopiers: |
---|
502 | n/a | for data, selectors, result1, result2 in [ |
---|
503 | n/a | ('ABCDEF', [1,0,1,0,1,1], 'ACEF', 'CEF'), |
---|
504 | n/a | ('ABCDEF', [0,0,0,0,0,0], '', ''), |
---|
505 | n/a | ('ABCDEF', [1,1,1,1,1,1], 'ABCDEF', 'BCDEF'), |
---|
506 | n/a | ('ABCDEF', [1,0,1], 'AC', 'C'), |
---|
507 | n/a | ('ABC', [0,1,1,1,1,1], 'BC', 'C'), |
---|
508 | n/a | ]: |
---|
509 | n/a | |
---|
510 | n/a | self.assertEqual(list(op(compress(data=data, selectors=selectors))), list(result1)) |
---|
511 | n/a | self.assertEqual(list(op(compress(data, selectors))), list(result1)) |
---|
512 | n/a | testIntermediate = compress(data, selectors) |
---|
513 | n/a | if result1: |
---|
514 | n/a | next(testIntermediate) |
---|
515 | n/a | self.assertEqual(list(op(testIntermediate)), list(result2)) |
---|
516 | n/a | |
---|
517 | n/a | |
---|
518 | n/a | def test_count(self): |
---|
519 | n/a | self.assertEqual(lzip('abc',count()), [('a', 0), ('b', 1), ('c', 2)]) |
---|
520 | n/a | self.assertEqual(lzip('abc',count(3)), [('a', 3), ('b', 4), ('c', 5)]) |
---|
521 | n/a | self.assertEqual(take(2, lzip('abc',count(3))), [('a', 3), ('b', 4)]) |
---|
522 | n/a | self.assertEqual(take(2, zip('abc',count(-1))), [('a', -1), ('b', 0)]) |
---|
523 | n/a | self.assertEqual(take(2, zip('abc',count(-3))), [('a', -3), ('b', -2)]) |
---|
524 | n/a | self.assertRaises(TypeError, count, 2, 3, 4) |
---|
525 | n/a | self.assertRaises(TypeError, count, 'a') |
---|
526 | n/a | self.assertEqual(take(10, count(maxsize-5)), |
---|
527 | n/a | list(range(maxsize-5, maxsize+5))) |
---|
528 | n/a | self.assertEqual(take(10, count(-maxsize-5)), |
---|
529 | n/a | list(range(-maxsize-5, -maxsize+5))) |
---|
530 | n/a | self.assertEqual(take(3, count(3.25)), [3.25, 4.25, 5.25]) |
---|
531 | n/a | self.assertEqual(take(3, count(3.25-4j)), [3.25-4j, 4.25-4j, 5.25-4j]) |
---|
532 | n/a | self.assertEqual(take(3, count(Decimal('1.1'))), |
---|
533 | n/a | [Decimal('1.1'), Decimal('2.1'), Decimal('3.1')]) |
---|
534 | n/a | self.assertEqual(take(3, count(Fraction(2, 3))), |
---|
535 | n/a | [Fraction(2, 3), Fraction(5, 3), Fraction(8, 3)]) |
---|
536 | n/a | BIGINT = 1<<1000 |
---|
537 | n/a | self.assertEqual(take(3, count(BIGINT)), [BIGINT, BIGINT+1, BIGINT+2]) |
---|
538 | n/a | c = count(3) |
---|
539 | n/a | self.assertEqual(repr(c), 'count(3)') |
---|
540 | n/a | next(c) |
---|
541 | n/a | self.assertEqual(repr(c), 'count(4)') |
---|
542 | n/a | c = count(-9) |
---|
543 | n/a | self.assertEqual(repr(c), 'count(-9)') |
---|
544 | n/a | next(c) |
---|
545 | n/a | self.assertEqual(next(c), -8) |
---|
546 | n/a | self.assertEqual(repr(count(10.25)), 'count(10.25)') |
---|
547 | n/a | self.assertEqual(repr(count(10.0)), 'count(10.0)') |
---|
548 | n/a | self.assertEqual(type(next(count(10.0))), float) |
---|
549 | n/a | for i in (-sys.maxsize-5, -sys.maxsize+5 ,-10, -1, 0, 10, sys.maxsize-5, sys.maxsize+5): |
---|
550 | n/a | # Test repr |
---|
551 | n/a | r1 = repr(count(i)) |
---|
552 | n/a | r2 = 'count(%r)'.__mod__(i) |
---|
553 | n/a | self.assertEqual(r1, r2) |
---|
554 | n/a | |
---|
555 | n/a | # check copy, deepcopy, pickle |
---|
556 | n/a | for value in -3, 3, maxsize-5, maxsize+5: |
---|
557 | n/a | c = count(value) |
---|
558 | n/a | self.assertEqual(next(copy.copy(c)), value) |
---|
559 | n/a | self.assertEqual(next(copy.deepcopy(c)), value) |
---|
560 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
561 | n/a | self.pickletest(proto, count(value)) |
---|
562 | n/a | |
---|
563 | n/a | #check proper internal error handling for large "step' sizes |
---|
564 | n/a | count(1, maxsize+5); sys.exc_info() |
---|
565 | n/a | |
---|
566 | n/a | def test_count_with_stride(self): |
---|
567 | n/a | self.assertEqual(lzip('abc',count(2,3)), [('a', 2), ('b', 5), ('c', 8)]) |
---|
568 | n/a | self.assertEqual(lzip('abc',count(start=2,step=3)), |
---|
569 | n/a | [('a', 2), ('b', 5), ('c', 8)]) |
---|
570 | n/a | self.assertEqual(lzip('abc',count(step=-1)), |
---|
571 | n/a | [('a', 0), ('b', -1), ('c', -2)]) |
---|
572 | n/a | self.assertRaises(TypeError, count, 'a', 'b') |
---|
573 | n/a | self.assertEqual(lzip('abc',count(2,0)), [('a', 2), ('b', 2), ('c', 2)]) |
---|
574 | n/a | self.assertEqual(lzip('abc',count(2,1)), [('a', 2), ('b', 3), ('c', 4)]) |
---|
575 | n/a | self.assertEqual(lzip('abc',count(2,3)), [('a', 2), ('b', 5), ('c', 8)]) |
---|
576 | n/a | self.assertEqual(take(20, count(maxsize-15, 3)), take(20, range(maxsize-15, maxsize+100, 3))) |
---|
577 | n/a | self.assertEqual(take(20, count(-maxsize-15, 3)), take(20, range(-maxsize-15,-maxsize+100, 3))) |
---|
578 | n/a | self.assertEqual(take(3, count(10, maxsize+5)), |
---|
579 | n/a | list(range(10, 10+3*(maxsize+5), maxsize+5))) |
---|
580 | n/a | self.assertEqual(take(3, count(2, 1.25)), [2, 3.25, 4.5]) |
---|
581 | n/a | self.assertEqual(take(3, count(2, 3.25-4j)), [2, 5.25-4j, 8.5-8j]) |
---|
582 | n/a | self.assertEqual(take(3, count(Decimal('1.1'), Decimal('.1'))), |
---|
583 | n/a | [Decimal('1.1'), Decimal('1.2'), Decimal('1.3')]) |
---|
584 | n/a | self.assertEqual(take(3, count(Fraction(2,3), Fraction(1,7))), |
---|
585 | n/a | [Fraction(2,3), Fraction(17,21), Fraction(20,21)]) |
---|
586 | n/a | BIGINT = 1<<1000 |
---|
587 | n/a | self.assertEqual(take(3, count(step=BIGINT)), [0, BIGINT, 2*BIGINT]) |
---|
588 | n/a | self.assertEqual(repr(take(3, count(10, 2.5))), repr([10, 12.5, 15.0])) |
---|
589 | n/a | c = count(3, 5) |
---|
590 | n/a | self.assertEqual(repr(c), 'count(3, 5)') |
---|
591 | n/a | next(c) |
---|
592 | n/a | self.assertEqual(repr(c), 'count(8, 5)') |
---|
593 | n/a | c = count(-9, 0) |
---|
594 | n/a | self.assertEqual(repr(c), 'count(-9, 0)') |
---|
595 | n/a | next(c) |
---|
596 | n/a | self.assertEqual(repr(c), 'count(-9, 0)') |
---|
597 | n/a | c = count(-9, -3) |
---|
598 | n/a | self.assertEqual(repr(c), 'count(-9, -3)') |
---|
599 | n/a | next(c) |
---|
600 | n/a | self.assertEqual(repr(c), 'count(-12, -3)') |
---|
601 | n/a | self.assertEqual(repr(c), 'count(-12, -3)') |
---|
602 | n/a | self.assertEqual(repr(count(10.5, 1.25)), 'count(10.5, 1.25)') |
---|
603 | n/a | self.assertEqual(repr(count(10.5, 1)), 'count(10.5)') # suppress step=1 when it's an int |
---|
604 | n/a | self.assertEqual(repr(count(10.5, 1.00)), 'count(10.5, 1.0)') # do show float values lilke 1.0 |
---|
605 | n/a | self.assertEqual(repr(count(10, 1.00)), 'count(10, 1.0)') |
---|
606 | n/a | c = count(10, 1.0) |
---|
607 | n/a | self.assertEqual(type(next(c)), int) |
---|
608 | n/a | self.assertEqual(type(next(c)), float) |
---|
609 | n/a | for i in (-sys.maxsize-5, -sys.maxsize+5 ,-10, -1, 0, 10, sys.maxsize-5, sys.maxsize+5): |
---|
610 | n/a | for j in (-sys.maxsize-5, -sys.maxsize+5 ,-10, -1, 0, 1, 10, sys.maxsize-5, sys.maxsize+5): |
---|
611 | n/a | # Test repr |
---|
612 | n/a | r1 = repr(count(i, j)) |
---|
613 | n/a | if j == 1: |
---|
614 | n/a | r2 = ('count(%r)' % i) |
---|
615 | n/a | else: |
---|
616 | n/a | r2 = ('count(%r, %r)' % (i, j)) |
---|
617 | n/a | self.assertEqual(r1, r2) |
---|
618 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
619 | n/a | self.pickletest(proto, count(i, j)) |
---|
620 | n/a | |
---|
621 | n/a | def test_cycle(self): |
---|
622 | n/a | self.assertEqual(take(10, cycle('abc')), list('abcabcabca')) |
---|
623 | n/a | self.assertEqual(list(cycle('')), []) |
---|
624 | n/a | self.assertRaises(TypeError, cycle) |
---|
625 | n/a | self.assertRaises(TypeError, cycle, 5) |
---|
626 | n/a | self.assertEqual(list(islice(cycle(gen3()),10)), [0,1,2,0,1,2,0,1,2,0]) |
---|
627 | n/a | |
---|
628 | n/a | # check copy, deepcopy, pickle |
---|
629 | n/a | c = cycle('abc') |
---|
630 | n/a | self.assertEqual(next(c), 'a') |
---|
631 | n/a | #simple copy currently not supported, because __reduce__ returns |
---|
632 | n/a | #an internal iterator |
---|
633 | n/a | #self.assertEqual(take(10, copy.copy(c)), list('bcabcabcab')) |
---|
634 | n/a | self.assertEqual(take(10, copy.deepcopy(c)), list('bcabcabcab')) |
---|
635 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
636 | n/a | self.assertEqual(take(10, pickle.loads(pickle.dumps(c, proto))), |
---|
637 | n/a | list('bcabcabcab')) |
---|
638 | n/a | next(c) |
---|
639 | n/a | self.assertEqual(take(10, pickle.loads(pickle.dumps(c, proto))), |
---|
640 | n/a | list('cabcabcabc')) |
---|
641 | n/a | next(c) |
---|
642 | n/a | next(c) |
---|
643 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
644 | n/a | self.pickletest(proto, cycle('abc')) |
---|
645 | n/a | |
---|
646 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
647 | n/a | # test with partial consumed input iterable |
---|
648 | n/a | it = iter('abcde') |
---|
649 | n/a | c = cycle(it) |
---|
650 | n/a | _ = [next(c) for i in range(2)] # consume 2 of 5 inputs |
---|
651 | n/a | p = pickle.dumps(c, proto) |
---|
652 | n/a | d = pickle.loads(p) # rebuild the cycle object |
---|
653 | n/a | self.assertEqual(take(20, d), list('cdeabcdeabcdeabcdeab')) |
---|
654 | n/a | |
---|
655 | n/a | # test with completely consumed input iterable |
---|
656 | n/a | it = iter('abcde') |
---|
657 | n/a | c = cycle(it) |
---|
658 | n/a | _ = [next(c) for i in range(7)] # consume 7 of 5 inputs |
---|
659 | n/a | p = pickle.dumps(c, proto) |
---|
660 | n/a | d = pickle.loads(p) # rebuild the cycle object |
---|
661 | n/a | self.assertEqual(take(20, d), list('cdeabcdeabcdeabcdeab')) |
---|
662 | n/a | |
---|
663 | n/a | def test_cycle_setstate(self): |
---|
664 | n/a | # Verify both modes for restoring state |
---|
665 | n/a | |
---|
666 | n/a | # Mode 0 is efficient. It uses an incompletely consumed input |
---|
667 | n/a | # iterator to build a cycle object and then passes in state with |
---|
668 | n/a | # a list of previously consumed values. There is no data |
---|
669 | n/a | # overlap between the two. |
---|
670 | n/a | c = cycle('defg') |
---|
671 | n/a | c.__setstate__((list('abc'), 0)) |
---|
672 | n/a | self.assertEqual(take(20, c), list('defgabcdefgabcdefgab')) |
---|
673 | n/a | |
---|
674 | n/a | # Mode 1 is inefficient. It starts with a cycle object built |
---|
675 | n/a | # from an iterator over the remaining elements in a partial |
---|
676 | n/a | # cycle and then passes in state with all of the previously |
---|
677 | n/a | # seen values (this overlaps values included in the iterator). |
---|
678 | n/a | c = cycle('defg') |
---|
679 | n/a | c.__setstate__((list('abcdefg'), 1)) |
---|
680 | n/a | self.assertEqual(take(20, c), list('defgabcdefgabcdefgab')) |
---|
681 | n/a | |
---|
682 | n/a | # The first argument to setstate needs to be a tuple |
---|
683 | n/a | with self.assertRaises(TypeError): |
---|
684 | n/a | cycle('defg').__setstate__([list('abcdefg'), 0]) |
---|
685 | n/a | |
---|
686 | n/a | # The first argument in the setstate tuple must be a list |
---|
687 | n/a | with self.assertRaises(TypeError): |
---|
688 | n/a | c = cycle('defg') |
---|
689 | n/a | c.__setstate__((tuple('defg'), 0)) |
---|
690 | n/a | take(20, c) |
---|
691 | n/a | |
---|
692 | n/a | # The second argument in the setstate tuple must be an int |
---|
693 | n/a | with self.assertRaises(TypeError): |
---|
694 | n/a | cycle('defg').__setstate__((list('abcdefg'), 'x')) |
---|
695 | n/a | |
---|
696 | n/a | self.assertRaises(TypeError, cycle('').__setstate__, ()) |
---|
697 | n/a | self.assertRaises(TypeError, cycle('').__setstate__, ([],)) |
---|
698 | n/a | |
---|
699 | n/a | def test_groupby(self): |
---|
700 | n/a | # Check whether it accepts arguments correctly |
---|
701 | n/a | self.assertEqual([], list(groupby([]))) |
---|
702 | n/a | self.assertEqual([], list(groupby([], key=id))) |
---|
703 | n/a | self.assertRaises(TypeError, list, groupby('abc', [])) |
---|
704 | n/a | self.assertRaises(TypeError, groupby, None) |
---|
705 | n/a | self.assertRaises(TypeError, groupby, 'abc', lambda x:x, 10) |
---|
706 | n/a | |
---|
707 | n/a | # Check normal input |
---|
708 | n/a | s = [(0, 10, 20), (0, 11,21), (0,12,21), (1,13,21), (1,14,22), |
---|
709 | n/a | (2,15,22), (3,16,23), (3,17,23)] |
---|
710 | n/a | dup = [] |
---|
711 | n/a | for k, g in groupby(s, lambda r:r[0]): |
---|
712 | n/a | for elem in g: |
---|
713 | n/a | self.assertEqual(k, elem[0]) |
---|
714 | n/a | dup.append(elem) |
---|
715 | n/a | self.assertEqual(s, dup) |
---|
716 | n/a | |
---|
717 | n/a | # Check normal pickled |
---|
718 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
719 | n/a | dup = [] |
---|
720 | n/a | for k, g in pickle.loads(pickle.dumps(groupby(s, testR), proto)): |
---|
721 | n/a | for elem in g: |
---|
722 | n/a | self.assertEqual(k, elem[0]) |
---|
723 | n/a | dup.append(elem) |
---|
724 | n/a | self.assertEqual(s, dup) |
---|
725 | n/a | |
---|
726 | n/a | # Check nested case |
---|
727 | n/a | dup = [] |
---|
728 | n/a | for k, g in groupby(s, testR): |
---|
729 | n/a | for ik, ig in groupby(g, testR2): |
---|
730 | n/a | for elem in ig: |
---|
731 | n/a | self.assertEqual(k, elem[0]) |
---|
732 | n/a | self.assertEqual(ik, elem[2]) |
---|
733 | n/a | dup.append(elem) |
---|
734 | n/a | self.assertEqual(s, dup) |
---|
735 | n/a | |
---|
736 | n/a | # Check nested and pickled |
---|
737 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
738 | n/a | dup = [] |
---|
739 | n/a | for k, g in pickle.loads(pickle.dumps(groupby(s, testR), proto)): |
---|
740 | n/a | for ik, ig in pickle.loads(pickle.dumps(groupby(g, testR2), proto)): |
---|
741 | n/a | for elem in ig: |
---|
742 | n/a | self.assertEqual(k, elem[0]) |
---|
743 | n/a | self.assertEqual(ik, elem[2]) |
---|
744 | n/a | dup.append(elem) |
---|
745 | n/a | self.assertEqual(s, dup) |
---|
746 | n/a | |
---|
747 | n/a | |
---|
748 | n/a | # Check case where inner iterator is not used |
---|
749 | n/a | keys = [k for k, g in groupby(s, testR)] |
---|
750 | n/a | expectedkeys = set([r[0] for r in s]) |
---|
751 | n/a | self.assertEqual(set(keys), expectedkeys) |
---|
752 | n/a | self.assertEqual(len(keys), len(expectedkeys)) |
---|
753 | n/a | |
---|
754 | n/a | # Exercise pipes and filters style |
---|
755 | n/a | s = 'abracadabra' |
---|
756 | n/a | # sort s | uniq |
---|
757 | n/a | r = [k for k, g in groupby(sorted(s))] |
---|
758 | n/a | self.assertEqual(r, ['a', 'b', 'c', 'd', 'r']) |
---|
759 | n/a | # sort s | uniq -d |
---|
760 | n/a | r = [k for k, g in groupby(sorted(s)) if list(islice(g,1,2))] |
---|
761 | n/a | self.assertEqual(r, ['a', 'b', 'r']) |
---|
762 | n/a | # sort s | uniq -c |
---|
763 | n/a | r = [(len(list(g)), k) for k, g in groupby(sorted(s))] |
---|
764 | n/a | self.assertEqual(r, [(5, 'a'), (2, 'b'), (1, 'c'), (1, 'd'), (2, 'r')]) |
---|
765 | n/a | # sort s | uniq -c | sort -rn | head -3 |
---|
766 | n/a | r = sorted([(len(list(g)) , k) for k, g in groupby(sorted(s))], reverse=True)[:3] |
---|
767 | n/a | self.assertEqual(r, [(5, 'a'), (2, 'r'), (2, 'b')]) |
---|
768 | n/a | |
---|
769 | n/a | # iter.__next__ failure |
---|
770 | n/a | class ExpectedError(Exception): |
---|
771 | n/a | pass |
---|
772 | n/a | def delayed_raise(n=0): |
---|
773 | n/a | for i in range(n): |
---|
774 | n/a | yield 'yo' |
---|
775 | n/a | raise ExpectedError |
---|
776 | n/a | def gulp(iterable, keyp=None, func=list): |
---|
777 | n/a | return [func(g) for k, g in groupby(iterable, keyp)] |
---|
778 | n/a | |
---|
779 | n/a | # iter.__next__ failure on outer object |
---|
780 | n/a | self.assertRaises(ExpectedError, gulp, delayed_raise(0)) |
---|
781 | n/a | # iter.__next__ failure on inner object |
---|
782 | n/a | self.assertRaises(ExpectedError, gulp, delayed_raise(1)) |
---|
783 | n/a | |
---|
784 | n/a | # __eq__ failure |
---|
785 | n/a | class DummyCmp: |
---|
786 | n/a | def __eq__(self, dst): |
---|
787 | n/a | raise ExpectedError |
---|
788 | n/a | s = [DummyCmp(), DummyCmp(), None] |
---|
789 | n/a | |
---|
790 | n/a | # __eq__ failure on outer object |
---|
791 | n/a | self.assertRaises(ExpectedError, gulp, s, func=id) |
---|
792 | n/a | # __eq__ failure on inner object |
---|
793 | n/a | self.assertRaises(ExpectedError, gulp, s) |
---|
794 | n/a | |
---|
795 | n/a | # keyfunc failure |
---|
796 | n/a | def keyfunc(obj): |
---|
797 | n/a | if keyfunc.skip > 0: |
---|
798 | n/a | keyfunc.skip -= 1 |
---|
799 | n/a | return obj |
---|
800 | n/a | else: |
---|
801 | n/a | raise ExpectedError |
---|
802 | n/a | |
---|
803 | n/a | # keyfunc failure on outer object |
---|
804 | n/a | keyfunc.skip = 0 |
---|
805 | n/a | self.assertRaises(ExpectedError, gulp, [None], keyfunc) |
---|
806 | n/a | keyfunc.skip = 1 |
---|
807 | n/a | self.assertRaises(ExpectedError, gulp, [None, None], keyfunc) |
---|
808 | n/a | |
---|
809 | n/a | def test_filter(self): |
---|
810 | n/a | self.assertEqual(list(filter(isEven, range(6))), [0,2,4]) |
---|
811 | n/a | self.assertEqual(list(filter(None, [0,1,0,2,0])), [1,2]) |
---|
812 | n/a | self.assertEqual(list(filter(bool, [0,1,0,2,0])), [1,2]) |
---|
813 | n/a | self.assertEqual(take(4, filter(isEven, count())), [0,2,4,6]) |
---|
814 | n/a | self.assertRaises(TypeError, filter) |
---|
815 | n/a | self.assertRaises(TypeError, filter, lambda x:x) |
---|
816 | n/a | self.assertRaises(TypeError, filter, lambda x:x, range(6), 7) |
---|
817 | n/a | self.assertRaises(TypeError, filter, isEven, 3) |
---|
818 | n/a | self.assertRaises(TypeError, next, filter(range(6), range(6))) |
---|
819 | n/a | |
---|
820 | n/a | # check copy, deepcopy, pickle |
---|
821 | n/a | ans = [0,2,4] |
---|
822 | n/a | |
---|
823 | n/a | c = filter(isEven, range(6)) |
---|
824 | n/a | self.assertEqual(list(copy.copy(c)), ans) |
---|
825 | n/a | c = filter(isEven, range(6)) |
---|
826 | n/a | self.assertEqual(list(copy.deepcopy(c)), ans) |
---|
827 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
828 | n/a | c = filter(isEven, range(6)) |
---|
829 | n/a | self.assertEqual(list(pickle.loads(pickle.dumps(c, proto))), ans) |
---|
830 | n/a | next(c) |
---|
831 | n/a | self.assertEqual(list(pickle.loads(pickle.dumps(c, proto))), ans[1:]) |
---|
832 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
833 | n/a | c = filter(isEven, range(6)) |
---|
834 | n/a | self.pickletest(proto, c) |
---|
835 | n/a | |
---|
836 | n/a | def test_filterfalse(self): |
---|
837 | n/a | self.assertEqual(list(filterfalse(isEven, range(6))), [1,3,5]) |
---|
838 | n/a | self.assertEqual(list(filterfalse(None, [0,1,0,2,0])), [0,0,0]) |
---|
839 | n/a | self.assertEqual(list(filterfalse(bool, [0,1,0,2,0])), [0,0,0]) |
---|
840 | n/a | self.assertEqual(take(4, filterfalse(isEven, count())), [1,3,5,7]) |
---|
841 | n/a | self.assertRaises(TypeError, filterfalse) |
---|
842 | n/a | self.assertRaises(TypeError, filterfalse, lambda x:x) |
---|
843 | n/a | self.assertRaises(TypeError, filterfalse, lambda x:x, range(6), 7) |
---|
844 | n/a | self.assertRaises(TypeError, filterfalse, isEven, 3) |
---|
845 | n/a | self.assertRaises(TypeError, next, filterfalse(range(6), range(6))) |
---|
846 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
847 | n/a | self.pickletest(proto, filterfalse(isEven, range(6))) |
---|
848 | n/a | |
---|
849 | n/a | def test_zip(self): |
---|
850 | n/a | # XXX This is rather silly now that builtin zip() calls zip()... |
---|
851 | n/a | ans = [(x,y) for x, y in zip('abc',count())] |
---|
852 | n/a | self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)]) |
---|
853 | n/a | self.assertEqual(list(zip('abc', range(6))), lzip('abc', range(6))) |
---|
854 | n/a | self.assertEqual(list(zip('abcdef', range(3))), lzip('abcdef', range(3))) |
---|
855 | n/a | self.assertEqual(take(3,zip('abcdef', count())), lzip('abcdef', range(3))) |
---|
856 | n/a | self.assertEqual(list(zip('abcdef')), lzip('abcdef')) |
---|
857 | n/a | self.assertEqual(list(zip()), lzip()) |
---|
858 | n/a | self.assertRaises(TypeError, zip, 3) |
---|
859 | n/a | self.assertRaises(TypeError, zip, range(3), 3) |
---|
860 | n/a | self.assertEqual([tuple(list(pair)) for pair in zip('abc', 'def')], |
---|
861 | n/a | lzip('abc', 'def')) |
---|
862 | n/a | self.assertEqual([pair for pair in zip('abc', 'def')], |
---|
863 | n/a | lzip('abc', 'def')) |
---|
864 | n/a | |
---|
865 | n/a | @support.impl_detail("tuple reuse is specific to CPython") |
---|
866 | n/a | def test_zip_tuple_reuse(self): |
---|
867 | n/a | ids = list(map(id, zip('abc', 'def'))) |
---|
868 | n/a | self.assertEqual(min(ids), max(ids)) |
---|
869 | n/a | ids = list(map(id, list(zip('abc', 'def')))) |
---|
870 | n/a | self.assertEqual(len(dict.fromkeys(ids)), len(ids)) |
---|
871 | n/a | |
---|
872 | n/a | # check copy, deepcopy, pickle |
---|
873 | n/a | ans = [(x,y) for x, y in copy.copy(zip('abc',count()))] |
---|
874 | n/a | self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)]) |
---|
875 | n/a | |
---|
876 | n/a | ans = [(x,y) for x, y in copy.deepcopy(zip('abc',count()))] |
---|
877 | n/a | self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)]) |
---|
878 | n/a | |
---|
879 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
880 | n/a | ans = [(x,y) for x, y in pickle.loads(pickle.dumps(zip('abc',count()), proto))] |
---|
881 | n/a | self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)]) |
---|
882 | n/a | |
---|
883 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
884 | n/a | testIntermediate = zip('abc',count()) |
---|
885 | n/a | next(testIntermediate) |
---|
886 | n/a | ans = [(x,y) for x, y in pickle.loads(pickle.dumps(testIntermediate, proto))] |
---|
887 | n/a | self.assertEqual(ans, [('b', 1), ('c', 2)]) |
---|
888 | n/a | |
---|
889 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
890 | n/a | self.pickletest(proto, zip('abc', count())) |
---|
891 | n/a | |
---|
892 | n/a | def test_ziplongest(self): |
---|
893 | n/a | for args in [ |
---|
894 | n/a | ['abc', range(6)], |
---|
895 | n/a | [range(6), 'abc'], |
---|
896 | n/a | [range(1000), range(2000,2100), range(3000,3050)], |
---|
897 | n/a | [range(1000), range(0), range(3000,3050), range(1200), range(1500)], |
---|
898 | n/a | [range(1000), range(0), range(3000,3050), range(1200), range(1500), range(0)], |
---|
899 | n/a | ]: |
---|
900 | n/a | target = [tuple([arg[i] if i < len(arg) else None for arg in args]) |
---|
901 | n/a | for i in range(max(map(len, args)))] |
---|
902 | n/a | self.assertEqual(list(zip_longest(*args)), target) |
---|
903 | n/a | self.assertEqual(list(zip_longest(*args, **{})), target) |
---|
904 | n/a | target = [tuple((e is None and 'X' or e) for e in t) for t in target] # Replace None fills with 'X' |
---|
905 | n/a | self.assertEqual(list(zip_longest(*args, **dict(fillvalue='X'))), target) |
---|
906 | n/a | |
---|
907 | n/a | self.assertEqual(take(3,zip_longest('abcdef', count())), list(zip('abcdef', range(3)))) # take 3 from infinite input |
---|
908 | n/a | |
---|
909 | n/a | self.assertEqual(list(zip_longest()), list(zip())) |
---|
910 | n/a | self.assertEqual(list(zip_longest([])), list(zip([]))) |
---|
911 | n/a | self.assertEqual(list(zip_longest('abcdef')), list(zip('abcdef'))) |
---|
912 | n/a | |
---|
913 | n/a | self.assertEqual(list(zip_longest('abc', 'defg', **{})), |
---|
914 | n/a | list(zip(list('abc')+[None], 'defg'))) # empty keyword dict |
---|
915 | n/a | self.assertRaises(TypeError, zip_longest, 3) |
---|
916 | n/a | self.assertRaises(TypeError, zip_longest, range(3), 3) |
---|
917 | n/a | |
---|
918 | n/a | for stmt in [ |
---|
919 | n/a | "zip_longest('abc', fv=1)", |
---|
920 | n/a | "zip_longest('abc', fillvalue=1, bogus_keyword=None)", |
---|
921 | n/a | ]: |
---|
922 | n/a | try: |
---|
923 | n/a | eval(stmt, globals(), locals()) |
---|
924 | n/a | except TypeError: |
---|
925 | n/a | pass |
---|
926 | n/a | else: |
---|
927 | n/a | self.fail('Did not raise Type in: ' + stmt) |
---|
928 | n/a | |
---|
929 | n/a | self.assertEqual([tuple(list(pair)) for pair in zip_longest('abc', 'def')], |
---|
930 | n/a | list(zip('abc', 'def'))) |
---|
931 | n/a | self.assertEqual([pair for pair in zip_longest('abc', 'def')], |
---|
932 | n/a | list(zip('abc', 'def'))) |
---|
933 | n/a | |
---|
934 | n/a | @support.impl_detail("tuple reuse is specific to CPython") |
---|
935 | n/a | def test_zip_longest_tuple_reuse(self): |
---|
936 | n/a | ids = list(map(id, zip_longest('abc', 'def'))) |
---|
937 | n/a | self.assertEqual(min(ids), max(ids)) |
---|
938 | n/a | ids = list(map(id, list(zip_longest('abc', 'def')))) |
---|
939 | n/a | self.assertEqual(len(dict.fromkeys(ids)), len(ids)) |
---|
940 | n/a | |
---|
941 | n/a | def test_zip_longest_pickling(self): |
---|
942 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
943 | n/a | self.pickletest(proto, zip_longest("abc", "def")) |
---|
944 | n/a | self.pickletest(proto, zip_longest("abc", "defgh")) |
---|
945 | n/a | self.pickletest(proto, zip_longest("abc", "defgh", fillvalue=1)) |
---|
946 | n/a | self.pickletest(proto, zip_longest("", "defgh")) |
---|
947 | n/a | |
---|
948 | n/a | def test_bug_7244(self): |
---|
949 | n/a | |
---|
950 | n/a | class Repeater: |
---|
951 | n/a | # this class is similar to itertools.repeat |
---|
952 | n/a | def __init__(self, o, t, e): |
---|
953 | n/a | self.o = o |
---|
954 | n/a | self.t = int(t) |
---|
955 | n/a | self.e = e |
---|
956 | n/a | def __iter__(self): # its iterator is itself |
---|
957 | n/a | return self |
---|
958 | n/a | def __next__(self): |
---|
959 | n/a | if self.t > 0: |
---|
960 | n/a | self.t -= 1 |
---|
961 | n/a | return self.o |
---|
962 | n/a | else: |
---|
963 | n/a | raise self.e |
---|
964 | n/a | |
---|
965 | n/a | # Formerly this code in would fail in debug mode |
---|
966 | n/a | # with Undetected Error and Stop Iteration |
---|
967 | n/a | r1 = Repeater(1, 3, StopIteration) |
---|
968 | n/a | r2 = Repeater(2, 4, StopIteration) |
---|
969 | n/a | def run(r1, r2): |
---|
970 | n/a | result = [] |
---|
971 | n/a | for i, j in zip_longest(r1, r2, fillvalue=0): |
---|
972 | n/a | with support.captured_output('stdout'): |
---|
973 | n/a | print((i, j)) |
---|
974 | n/a | result.append((i, j)) |
---|
975 | n/a | return result |
---|
976 | n/a | self.assertEqual(run(r1, r2), [(1,2), (1,2), (1,2), (0,2)]) |
---|
977 | n/a | |
---|
978 | n/a | # Formerly, the RuntimeError would be lost |
---|
979 | n/a | # and StopIteration would stop as expected |
---|
980 | n/a | r1 = Repeater(1, 3, RuntimeError) |
---|
981 | n/a | r2 = Repeater(2, 4, StopIteration) |
---|
982 | n/a | it = zip_longest(r1, r2, fillvalue=0) |
---|
983 | n/a | self.assertEqual(next(it), (1, 2)) |
---|
984 | n/a | self.assertEqual(next(it), (1, 2)) |
---|
985 | n/a | self.assertEqual(next(it), (1, 2)) |
---|
986 | n/a | self.assertRaises(RuntimeError, next, it) |
---|
987 | n/a | |
---|
988 | n/a | def test_product(self): |
---|
989 | n/a | for args, result in [ |
---|
990 | n/a | ([], [()]), # zero iterables |
---|
991 | n/a | (['ab'], [('a',), ('b',)]), # one iterable |
---|
992 | n/a | ([range(2), range(3)], [(0,0), (0,1), (0,2), (1,0), (1,1), (1,2)]), # two iterables |
---|
993 | n/a | ([range(0), range(2), range(3)], []), # first iterable with zero length |
---|
994 | n/a | ([range(2), range(0), range(3)], []), # middle iterable with zero length |
---|
995 | n/a | ([range(2), range(3), range(0)], []), # last iterable with zero length |
---|
996 | n/a | ]: |
---|
997 | n/a | self.assertEqual(list(product(*args)), result) |
---|
998 | n/a | for r in range(4): |
---|
999 | n/a | self.assertEqual(list(product(*(args*r))), |
---|
1000 | n/a | list(product(*args, **dict(repeat=r)))) |
---|
1001 | n/a | self.assertEqual(len(list(product(*[range(7)]*6))), 7**6) |
---|
1002 | n/a | self.assertRaises(TypeError, product, range(6), None) |
---|
1003 | n/a | |
---|
1004 | n/a | def product1(*args, **kwds): |
---|
1005 | n/a | pools = list(map(tuple, args)) * kwds.get('repeat', 1) |
---|
1006 | n/a | n = len(pools) |
---|
1007 | n/a | if n == 0: |
---|
1008 | n/a | yield () |
---|
1009 | n/a | return |
---|
1010 | n/a | if any(len(pool) == 0 for pool in pools): |
---|
1011 | n/a | return |
---|
1012 | n/a | indices = [0] * n |
---|
1013 | n/a | yield tuple(pool[i] for pool, i in zip(pools, indices)) |
---|
1014 | n/a | while 1: |
---|
1015 | n/a | for i in reversed(range(n)): # right to left |
---|
1016 | n/a | if indices[i] == len(pools[i]) - 1: |
---|
1017 | n/a | continue |
---|
1018 | n/a | indices[i] += 1 |
---|
1019 | n/a | for j in range(i+1, n): |
---|
1020 | n/a | indices[j] = 0 |
---|
1021 | n/a | yield tuple(pool[i] for pool, i in zip(pools, indices)) |
---|
1022 | n/a | break |
---|
1023 | n/a | else: |
---|
1024 | n/a | return |
---|
1025 | n/a | |
---|
1026 | n/a | def product2(*args, **kwds): |
---|
1027 | n/a | 'Pure python version used in docs' |
---|
1028 | n/a | pools = list(map(tuple, args)) * kwds.get('repeat', 1) |
---|
1029 | n/a | result = [[]] |
---|
1030 | n/a | for pool in pools: |
---|
1031 | n/a | result = [x+[y] for x in result for y in pool] |
---|
1032 | n/a | for prod in result: |
---|
1033 | n/a | yield tuple(prod) |
---|
1034 | n/a | |
---|
1035 | n/a | argtypes = ['', 'abc', '', range(0), range(4), dict(a=1, b=2, c=3), |
---|
1036 | n/a | set('abcdefg'), range(11), tuple(range(13))] |
---|
1037 | n/a | for i in range(100): |
---|
1038 | n/a | args = [random.choice(argtypes) for j in range(random.randrange(5))] |
---|
1039 | n/a | expected_len = prod(map(len, args)) |
---|
1040 | n/a | self.assertEqual(len(list(product(*args))), expected_len) |
---|
1041 | n/a | self.assertEqual(list(product(*args)), list(product1(*args))) |
---|
1042 | n/a | self.assertEqual(list(product(*args)), list(product2(*args))) |
---|
1043 | n/a | args = map(iter, args) |
---|
1044 | n/a | self.assertEqual(len(list(product(*args))), expected_len) |
---|
1045 | n/a | |
---|
1046 | n/a | @support.bigaddrspacetest |
---|
1047 | n/a | def test_product_overflow(self): |
---|
1048 | n/a | with self.assertRaises((OverflowError, MemoryError)): |
---|
1049 | n/a | product(*(['ab']*2**5), repeat=2**25) |
---|
1050 | n/a | |
---|
1051 | n/a | @support.impl_detail("tuple reuse is specific to CPython") |
---|
1052 | n/a | def test_product_tuple_reuse(self): |
---|
1053 | n/a | self.assertEqual(len(set(map(id, product('abc', 'def')))), 1) |
---|
1054 | n/a | self.assertNotEqual(len(set(map(id, list(product('abc', 'def'))))), 1) |
---|
1055 | n/a | |
---|
1056 | n/a | def test_product_pickling(self): |
---|
1057 | n/a | # check copy, deepcopy, pickle |
---|
1058 | n/a | for args, result in [ |
---|
1059 | n/a | ([], [()]), # zero iterables |
---|
1060 | n/a | (['ab'], [('a',), ('b',)]), # one iterable |
---|
1061 | n/a | ([range(2), range(3)], [(0,0), (0,1), (0,2), (1,0), (1,1), (1,2)]), # two iterables |
---|
1062 | n/a | ([range(0), range(2), range(3)], []), # first iterable with zero length |
---|
1063 | n/a | ([range(2), range(0), range(3)], []), # middle iterable with zero length |
---|
1064 | n/a | ([range(2), range(3), range(0)], []), # last iterable with zero length |
---|
1065 | n/a | ]: |
---|
1066 | n/a | self.assertEqual(list(copy.copy(product(*args))), result) |
---|
1067 | n/a | self.assertEqual(list(copy.deepcopy(product(*args))), result) |
---|
1068 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
1069 | n/a | self.pickletest(proto, product(*args)) |
---|
1070 | n/a | |
---|
1071 | n/a | def test_product_issue_25021(self): |
---|
1072 | n/a | # test that indices are properly clamped to the length of the tuples |
---|
1073 | n/a | p = product((1, 2),(3,)) |
---|
1074 | n/a | p.__setstate__((0, 0x1000)) # will access tuple element 1 if not clamped |
---|
1075 | n/a | self.assertEqual(next(p), (2, 3)) |
---|
1076 | n/a | # test that empty tuple in the list will result in an immediate StopIteration |
---|
1077 | n/a | p = product((1, 2), (), (3,)) |
---|
1078 | n/a | p.__setstate__((0, 0, 0x1000)) # will access tuple element 1 if not clamped |
---|
1079 | n/a | self.assertRaises(StopIteration, next, p) |
---|
1080 | n/a | |
---|
1081 | n/a | def test_repeat(self): |
---|
1082 | n/a | self.assertEqual(list(repeat(object='a', times=3)), ['a', 'a', 'a']) |
---|
1083 | n/a | self.assertEqual(lzip(range(3),repeat('a')), |
---|
1084 | n/a | [(0, 'a'), (1, 'a'), (2, 'a')]) |
---|
1085 | n/a | self.assertEqual(list(repeat('a', 3)), ['a', 'a', 'a']) |
---|
1086 | n/a | self.assertEqual(take(3, repeat('a')), ['a', 'a', 'a']) |
---|
1087 | n/a | self.assertEqual(list(repeat('a', 0)), []) |
---|
1088 | n/a | self.assertEqual(list(repeat('a', -3)), []) |
---|
1089 | n/a | self.assertRaises(TypeError, repeat) |
---|
1090 | n/a | self.assertRaises(TypeError, repeat, None, 3, 4) |
---|
1091 | n/a | self.assertRaises(TypeError, repeat, None, 'a') |
---|
1092 | n/a | r = repeat(1+0j) |
---|
1093 | n/a | self.assertEqual(repr(r), 'repeat((1+0j))') |
---|
1094 | n/a | r = repeat(1+0j, 5) |
---|
1095 | n/a | self.assertEqual(repr(r), 'repeat((1+0j), 5)') |
---|
1096 | n/a | list(r) |
---|
1097 | n/a | self.assertEqual(repr(r), 'repeat((1+0j), 0)') |
---|
1098 | n/a | |
---|
1099 | n/a | # check copy, deepcopy, pickle |
---|
1100 | n/a | c = repeat(object='a', times=10) |
---|
1101 | n/a | self.assertEqual(next(c), 'a') |
---|
1102 | n/a | self.assertEqual(take(2, copy.copy(c)), list('a' * 2)) |
---|
1103 | n/a | self.assertEqual(take(2, copy.deepcopy(c)), list('a' * 2)) |
---|
1104 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
1105 | n/a | self.pickletest(proto, repeat(object='a', times=10)) |
---|
1106 | n/a | |
---|
1107 | n/a | def test_repeat_with_negative_times(self): |
---|
1108 | n/a | self.assertEqual(repr(repeat('a', -1)), "repeat('a', 0)") |
---|
1109 | n/a | self.assertEqual(repr(repeat('a', -2)), "repeat('a', 0)") |
---|
1110 | n/a | self.assertEqual(repr(repeat('a', times=-1)), "repeat('a', 0)") |
---|
1111 | n/a | self.assertEqual(repr(repeat('a', times=-2)), "repeat('a', 0)") |
---|
1112 | n/a | |
---|
1113 | n/a | def test_map(self): |
---|
1114 | n/a | self.assertEqual(list(map(operator.pow, range(3), range(1,7))), |
---|
1115 | n/a | [0**1, 1**2, 2**3]) |
---|
1116 | n/a | self.assertEqual(list(map(tupleize, 'abc', range(5))), |
---|
1117 | n/a | [('a',0),('b',1),('c',2)]) |
---|
1118 | n/a | self.assertEqual(list(map(tupleize, 'abc', count())), |
---|
1119 | n/a | [('a',0),('b',1),('c',2)]) |
---|
1120 | n/a | self.assertEqual(take(2,map(tupleize, 'abc', count())), |
---|
1121 | n/a | [('a',0),('b',1)]) |
---|
1122 | n/a | self.assertEqual(list(map(operator.pow, [])), []) |
---|
1123 | n/a | self.assertRaises(TypeError, map) |
---|
1124 | n/a | self.assertRaises(TypeError, list, map(None, range(3), range(3))) |
---|
1125 | n/a | self.assertRaises(TypeError, map, operator.neg) |
---|
1126 | n/a | self.assertRaises(TypeError, next, map(10, range(5))) |
---|
1127 | n/a | self.assertRaises(ValueError, next, map(errfunc, [4], [5])) |
---|
1128 | n/a | self.assertRaises(TypeError, next, map(onearg, [4], [5])) |
---|
1129 | n/a | |
---|
1130 | n/a | # check copy, deepcopy, pickle |
---|
1131 | n/a | ans = [('a',0),('b',1),('c',2)] |
---|
1132 | n/a | |
---|
1133 | n/a | c = map(tupleize, 'abc', count()) |
---|
1134 | n/a | self.assertEqual(list(copy.copy(c)), ans) |
---|
1135 | n/a | |
---|
1136 | n/a | c = map(tupleize, 'abc', count()) |
---|
1137 | n/a | self.assertEqual(list(copy.deepcopy(c)), ans) |
---|
1138 | n/a | |
---|
1139 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
1140 | n/a | c = map(tupleize, 'abc', count()) |
---|
1141 | n/a | self.pickletest(proto, c) |
---|
1142 | n/a | |
---|
1143 | n/a | def test_starmap(self): |
---|
1144 | n/a | self.assertEqual(list(starmap(operator.pow, zip(range(3), range(1,7)))), |
---|
1145 | n/a | [0**1, 1**2, 2**3]) |
---|
1146 | n/a | self.assertEqual(take(3, starmap(operator.pow, zip(count(), count(1)))), |
---|
1147 | n/a | [0**1, 1**2, 2**3]) |
---|
1148 | n/a | self.assertEqual(list(starmap(operator.pow, [])), []) |
---|
1149 | n/a | self.assertEqual(list(starmap(operator.pow, [iter([4,5])])), [4**5]) |
---|
1150 | n/a | self.assertRaises(TypeError, list, starmap(operator.pow, [None])) |
---|
1151 | n/a | self.assertRaises(TypeError, starmap) |
---|
1152 | n/a | self.assertRaises(TypeError, starmap, operator.pow, [(4,5)], 'extra') |
---|
1153 | n/a | self.assertRaises(TypeError, next, starmap(10, [(4,5)])) |
---|
1154 | n/a | self.assertRaises(ValueError, next, starmap(errfunc, [(4,5)])) |
---|
1155 | n/a | self.assertRaises(TypeError, next, starmap(onearg, [(4,5)])) |
---|
1156 | n/a | |
---|
1157 | n/a | # check copy, deepcopy, pickle |
---|
1158 | n/a | ans = [0**1, 1**2, 2**3] |
---|
1159 | n/a | |
---|
1160 | n/a | c = starmap(operator.pow, zip(range(3), range(1,7))) |
---|
1161 | n/a | self.assertEqual(list(copy.copy(c)), ans) |
---|
1162 | n/a | |
---|
1163 | n/a | c = starmap(operator.pow, zip(range(3), range(1,7))) |
---|
1164 | n/a | self.assertEqual(list(copy.deepcopy(c)), ans) |
---|
1165 | n/a | |
---|
1166 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
1167 | n/a | c = starmap(operator.pow, zip(range(3), range(1,7))) |
---|
1168 | n/a | self.pickletest(proto, c) |
---|
1169 | n/a | |
---|
1170 | n/a | def test_islice(self): |
---|
1171 | n/a | for args in [ # islice(args) should agree with range(args) |
---|
1172 | n/a | (10, 20, 3), |
---|
1173 | n/a | (10, 3, 20), |
---|
1174 | n/a | (10, 20), |
---|
1175 | n/a | (10, 3), |
---|
1176 | n/a | (20,) |
---|
1177 | n/a | ]: |
---|
1178 | n/a | self.assertEqual(list(islice(range(100), *args)), |
---|
1179 | n/a | list(range(*args))) |
---|
1180 | n/a | |
---|
1181 | n/a | for args, tgtargs in [ # Stop when seqn is exhausted |
---|
1182 | n/a | ((10, 110, 3), ((10, 100, 3))), |
---|
1183 | n/a | ((10, 110), ((10, 100))), |
---|
1184 | n/a | ((110,), (100,)) |
---|
1185 | n/a | ]: |
---|
1186 | n/a | self.assertEqual(list(islice(range(100), *args)), |
---|
1187 | n/a | list(range(*tgtargs))) |
---|
1188 | n/a | |
---|
1189 | n/a | # Test stop=None |
---|
1190 | n/a | self.assertEqual(list(islice(range(10), None)), list(range(10))) |
---|
1191 | n/a | self.assertEqual(list(islice(range(10), None, None)), list(range(10))) |
---|
1192 | n/a | self.assertEqual(list(islice(range(10), None, None, None)), list(range(10))) |
---|
1193 | n/a | self.assertEqual(list(islice(range(10), 2, None)), list(range(2, 10))) |
---|
1194 | n/a | self.assertEqual(list(islice(range(10), 1, None, 2)), list(range(1, 10, 2))) |
---|
1195 | n/a | |
---|
1196 | n/a | # Test number of items consumed SF #1171417 |
---|
1197 | n/a | it = iter(range(10)) |
---|
1198 | n/a | self.assertEqual(list(islice(it, 3)), list(range(3))) |
---|
1199 | n/a | self.assertEqual(list(it), list(range(3, 10))) |
---|
1200 | n/a | |
---|
1201 | n/a | # Test invalid arguments |
---|
1202 | n/a | ra = range(10) |
---|
1203 | n/a | self.assertRaises(TypeError, islice, ra) |
---|
1204 | n/a | self.assertRaises(TypeError, islice, ra, 1, 2, 3, 4) |
---|
1205 | n/a | self.assertRaises(ValueError, islice, ra, -5, 10, 1) |
---|
1206 | n/a | self.assertRaises(ValueError, islice, ra, 1, -5, -1) |
---|
1207 | n/a | self.assertRaises(ValueError, islice, ra, 1, 10, -1) |
---|
1208 | n/a | self.assertRaises(ValueError, islice, ra, 1, 10, 0) |
---|
1209 | n/a | self.assertRaises(ValueError, islice, ra, 'a') |
---|
1210 | n/a | self.assertRaises(ValueError, islice, ra, 'a', 1) |
---|
1211 | n/a | self.assertRaises(ValueError, islice, ra, 1, 'a') |
---|
1212 | n/a | self.assertRaises(ValueError, islice, ra, 'a', 1, 1) |
---|
1213 | n/a | self.assertRaises(ValueError, islice, ra, 1, 'a', 1) |
---|
1214 | n/a | self.assertEqual(len(list(islice(count(), 1, 10, maxsize))), 1) |
---|
1215 | n/a | |
---|
1216 | n/a | # Issue #10323: Less islice in a predictable state |
---|
1217 | n/a | c = count() |
---|
1218 | n/a | self.assertEqual(list(islice(c, 1, 3, 50)), [1]) |
---|
1219 | n/a | self.assertEqual(next(c), 3) |
---|
1220 | n/a | |
---|
1221 | n/a | # check copy, deepcopy, pickle |
---|
1222 | n/a | for args in [ # islice(args) should agree with range(args) |
---|
1223 | n/a | (10, 20, 3), |
---|
1224 | n/a | (10, 3, 20), |
---|
1225 | n/a | (10, 20), |
---|
1226 | n/a | (10, 3), |
---|
1227 | n/a | (20,) |
---|
1228 | n/a | ]: |
---|
1229 | n/a | self.assertEqual(list(copy.copy(islice(range(100), *args))), |
---|
1230 | n/a | list(range(*args))) |
---|
1231 | n/a | self.assertEqual(list(copy.deepcopy(islice(range(100), *args))), |
---|
1232 | n/a | list(range(*args))) |
---|
1233 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
1234 | n/a | self.pickletest(proto, islice(range(100), *args)) |
---|
1235 | n/a | |
---|
1236 | n/a | # Issue #21321: check source iterator is not referenced |
---|
1237 | n/a | # from islice() after the latter has been exhausted |
---|
1238 | n/a | it = (x for x in (1, 2)) |
---|
1239 | n/a | wr = weakref.ref(it) |
---|
1240 | n/a | it = islice(it, 1) |
---|
1241 | n/a | self.assertIsNotNone(wr()) |
---|
1242 | n/a | list(it) # exhaust the iterator |
---|
1243 | n/a | support.gc_collect() |
---|
1244 | n/a | self.assertIsNone(wr()) |
---|
1245 | n/a | |
---|
1246 | n/a | def test_takewhile(self): |
---|
1247 | n/a | data = [1, 3, 5, 20, 2, 4, 6, 8] |
---|
1248 | n/a | self.assertEqual(list(takewhile(underten, data)), [1, 3, 5]) |
---|
1249 | n/a | self.assertEqual(list(takewhile(underten, [])), []) |
---|
1250 | n/a | self.assertRaises(TypeError, takewhile) |
---|
1251 | n/a | self.assertRaises(TypeError, takewhile, operator.pow) |
---|
1252 | n/a | self.assertRaises(TypeError, takewhile, operator.pow, [(4,5)], 'extra') |
---|
1253 | n/a | self.assertRaises(TypeError, next, takewhile(10, [(4,5)])) |
---|
1254 | n/a | self.assertRaises(ValueError, next, takewhile(errfunc, [(4,5)])) |
---|
1255 | n/a | t = takewhile(bool, [1, 1, 1, 0, 0, 0]) |
---|
1256 | n/a | self.assertEqual(list(t), [1, 1, 1]) |
---|
1257 | n/a | self.assertRaises(StopIteration, next, t) |
---|
1258 | n/a | |
---|
1259 | n/a | # check copy, deepcopy, pickle |
---|
1260 | n/a | self.assertEqual(list(copy.copy(takewhile(underten, data))), [1, 3, 5]) |
---|
1261 | n/a | self.assertEqual(list(copy.deepcopy(takewhile(underten, data))), |
---|
1262 | n/a | [1, 3, 5]) |
---|
1263 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
1264 | n/a | self.pickletest(proto, takewhile(underten, data)) |
---|
1265 | n/a | |
---|
1266 | n/a | def test_dropwhile(self): |
---|
1267 | n/a | data = [1, 3, 5, 20, 2, 4, 6, 8] |
---|
1268 | n/a | self.assertEqual(list(dropwhile(underten, data)), [20, 2, 4, 6, 8]) |
---|
1269 | n/a | self.assertEqual(list(dropwhile(underten, [])), []) |
---|
1270 | n/a | self.assertRaises(TypeError, dropwhile) |
---|
1271 | n/a | self.assertRaises(TypeError, dropwhile, operator.pow) |
---|
1272 | n/a | self.assertRaises(TypeError, dropwhile, operator.pow, [(4,5)], 'extra') |
---|
1273 | n/a | self.assertRaises(TypeError, next, dropwhile(10, [(4,5)])) |
---|
1274 | n/a | self.assertRaises(ValueError, next, dropwhile(errfunc, [(4,5)])) |
---|
1275 | n/a | |
---|
1276 | n/a | # check copy, deepcopy, pickle |
---|
1277 | n/a | self.assertEqual(list(copy.copy(dropwhile(underten, data))), [20, 2, 4, 6, 8]) |
---|
1278 | n/a | self.assertEqual(list(copy.deepcopy(dropwhile(underten, data))), |
---|
1279 | n/a | [20, 2, 4, 6, 8]) |
---|
1280 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
1281 | n/a | self.pickletest(proto, dropwhile(underten, data)) |
---|
1282 | n/a | |
---|
1283 | n/a | def test_tee(self): |
---|
1284 | n/a | n = 200 |
---|
1285 | n/a | |
---|
1286 | n/a | a, b = tee([]) # test empty iterator |
---|
1287 | n/a | self.assertEqual(list(a), []) |
---|
1288 | n/a | self.assertEqual(list(b), []) |
---|
1289 | n/a | |
---|
1290 | n/a | a, b = tee(irange(n)) # test 100% interleaved |
---|
1291 | n/a | self.assertEqual(lzip(a,b), lzip(range(n), range(n))) |
---|
1292 | n/a | |
---|
1293 | n/a | a, b = tee(irange(n)) # test 0% interleaved |
---|
1294 | n/a | self.assertEqual(list(a), list(range(n))) |
---|
1295 | n/a | self.assertEqual(list(b), list(range(n))) |
---|
1296 | n/a | |
---|
1297 | n/a | a, b = tee(irange(n)) # test dealloc of leading iterator |
---|
1298 | n/a | for i in range(100): |
---|
1299 | n/a | self.assertEqual(next(a), i) |
---|
1300 | n/a | del a |
---|
1301 | n/a | self.assertEqual(list(b), list(range(n))) |
---|
1302 | n/a | |
---|
1303 | n/a | a, b = tee(irange(n)) # test dealloc of trailing iterator |
---|
1304 | n/a | for i in range(100): |
---|
1305 | n/a | self.assertEqual(next(a), i) |
---|
1306 | n/a | del b |
---|
1307 | n/a | self.assertEqual(list(a), list(range(100, n))) |
---|
1308 | n/a | |
---|
1309 | n/a | for j in range(5): # test randomly interleaved |
---|
1310 | n/a | order = [0]*n + [1]*n |
---|
1311 | n/a | random.shuffle(order) |
---|
1312 | n/a | lists = ([], []) |
---|
1313 | n/a | its = tee(irange(n)) |
---|
1314 | n/a | for i in order: |
---|
1315 | n/a | value = next(its[i]) |
---|
1316 | n/a | lists[i].append(value) |
---|
1317 | n/a | self.assertEqual(lists[0], list(range(n))) |
---|
1318 | n/a | self.assertEqual(lists[1], list(range(n))) |
---|
1319 | n/a | |
---|
1320 | n/a | # test argument format checking |
---|
1321 | n/a | self.assertRaises(TypeError, tee) |
---|
1322 | n/a | self.assertRaises(TypeError, tee, 3) |
---|
1323 | n/a | self.assertRaises(TypeError, tee, [1,2], 'x') |
---|
1324 | n/a | self.assertRaises(TypeError, tee, [1,2], 3, 'x') |
---|
1325 | n/a | |
---|
1326 | n/a | # tee object should be instantiable |
---|
1327 | n/a | a, b = tee('abc') |
---|
1328 | n/a | c = type(a)('def') |
---|
1329 | n/a | self.assertEqual(list(c), list('def')) |
---|
1330 | n/a | |
---|
1331 | n/a | # test long-lagged and multi-way split |
---|
1332 | n/a | a, b, c = tee(range(2000), 3) |
---|
1333 | n/a | for i in range(100): |
---|
1334 | n/a | self.assertEqual(next(a), i) |
---|
1335 | n/a | self.assertEqual(list(b), list(range(2000))) |
---|
1336 | n/a | self.assertEqual([next(c), next(c)], list(range(2))) |
---|
1337 | n/a | self.assertEqual(list(a), list(range(100,2000))) |
---|
1338 | n/a | self.assertEqual(list(c), list(range(2,2000))) |
---|
1339 | n/a | |
---|
1340 | n/a | # test values of n |
---|
1341 | n/a | self.assertRaises(TypeError, tee, 'abc', 'invalid') |
---|
1342 | n/a | self.assertRaises(ValueError, tee, [], -1) |
---|
1343 | n/a | for n in range(5): |
---|
1344 | n/a | result = tee('abc', n) |
---|
1345 | n/a | self.assertEqual(type(result), tuple) |
---|
1346 | n/a | self.assertEqual(len(result), n) |
---|
1347 | n/a | self.assertEqual([list(x) for x in result], [list('abc')]*n) |
---|
1348 | n/a | |
---|
1349 | n/a | # tee pass-through to copyable iterator |
---|
1350 | n/a | a, b = tee('abc') |
---|
1351 | n/a | c, d = tee(a) |
---|
1352 | n/a | self.assertTrue(a is c) |
---|
1353 | n/a | |
---|
1354 | n/a | # test tee_new |
---|
1355 | n/a | t1, t2 = tee('abc') |
---|
1356 | n/a | tnew = type(t1) |
---|
1357 | n/a | self.assertRaises(TypeError, tnew) |
---|
1358 | n/a | self.assertRaises(TypeError, tnew, 10) |
---|
1359 | n/a | t3 = tnew(t1) |
---|
1360 | n/a | self.assertTrue(list(t1) == list(t2) == list(t3) == list('abc')) |
---|
1361 | n/a | |
---|
1362 | n/a | # test that tee objects are weak referencable |
---|
1363 | n/a | a, b = tee(range(10)) |
---|
1364 | n/a | p = weakref.proxy(a) |
---|
1365 | n/a | self.assertEqual(getattr(p, '__class__'), type(b)) |
---|
1366 | n/a | del a |
---|
1367 | n/a | self.assertRaises(ReferenceError, getattr, p, '__class__') |
---|
1368 | n/a | |
---|
1369 | n/a | ans = list('abc') |
---|
1370 | n/a | long_ans = list(range(10000)) |
---|
1371 | n/a | |
---|
1372 | n/a | # check copy |
---|
1373 | n/a | a, b = tee('abc') |
---|
1374 | n/a | self.assertEqual(list(copy.copy(a)), ans) |
---|
1375 | n/a | self.assertEqual(list(copy.copy(b)), ans) |
---|
1376 | n/a | a, b = tee(list(range(10000))) |
---|
1377 | n/a | self.assertEqual(list(copy.copy(a)), long_ans) |
---|
1378 | n/a | self.assertEqual(list(copy.copy(b)), long_ans) |
---|
1379 | n/a | |
---|
1380 | n/a | # check partially consumed copy |
---|
1381 | n/a | a, b = tee('abc') |
---|
1382 | n/a | take(2, a) |
---|
1383 | n/a | take(1, b) |
---|
1384 | n/a | self.assertEqual(list(copy.copy(a)), ans[2:]) |
---|
1385 | n/a | self.assertEqual(list(copy.copy(b)), ans[1:]) |
---|
1386 | n/a | self.assertEqual(list(a), ans[2:]) |
---|
1387 | n/a | self.assertEqual(list(b), ans[1:]) |
---|
1388 | n/a | a, b = tee(range(10000)) |
---|
1389 | n/a | take(100, a) |
---|
1390 | n/a | take(60, b) |
---|
1391 | n/a | self.assertEqual(list(copy.copy(a)), long_ans[100:]) |
---|
1392 | n/a | self.assertEqual(list(copy.copy(b)), long_ans[60:]) |
---|
1393 | n/a | self.assertEqual(list(a), long_ans[100:]) |
---|
1394 | n/a | self.assertEqual(list(b), long_ans[60:]) |
---|
1395 | n/a | |
---|
1396 | n/a | # check deepcopy |
---|
1397 | n/a | a, b = tee('abc') |
---|
1398 | n/a | self.assertEqual(list(copy.deepcopy(a)), ans) |
---|
1399 | n/a | self.assertEqual(list(copy.deepcopy(b)), ans) |
---|
1400 | n/a | self.assertEqual(list(a), ans) |
---|
1401 | n/a | self.assertEqual(list(b), ans) |
---|
1402 | n/a | a, b = tee(range(10000)) |
---|
1403 | n/a | self.assertEqual(list(copy.deepcopy(a)), long_ans) |
---|
1404 | n/a | self.assertEqual(list(copy.deepcopy(b)), long_ans) |
---|
1405 | n/a | self.assertEqual(list(a), long_ans) |
---|
1406 | n/a | self.assertEqual(list(b), long_ans) |
---|
1407 | n/a | |
---|
1408 | n/a | # check partially consumed deepcopy |
---|
1409 | n/a | a, b = tee('abc') |
---|
1410 | n/a | take(2, a) |
---|
1411 | n/a | take(1, b) |
---|
1412 | n/a | self.assertEqual(list(copy.deepcopy(a)), ans[2:]) |
---|
1413 | n/a | self.assertEqual(list(copy.deepcopy(b)), ans[1:]) |
---|
1414 | n/a | self.assertEqual(list(a), ans[2:]) |
---|
1415 | n/a | self.assertEqual(list(b), ans[1:]) |
---|
1416 | n/a | a, b = tee(range(10000)) |
---|
1417 | n/a | take(100, a) |
---|
1418 | n/a | take(60, b) |
---|
1419 | n/a | self.assertEqual(list(copy.deepcopy(a)), long_ans[100:]) |
---|
1420 | n/a | self.assertEqual(list(copy.deepcopy(b)), long_ans[60:]) |
---|
1421 | n/a | self.assertEqual(list(a), long_ans[100:]) |
---|
1422 | n/a | self.assertEqual(list(b), long_ans[60:]) |
---|
1423 | n/a | |
---|
1424 | n/a | # check pickle |
---|
1425 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
1426 | n/a | self.pickletest(proto, iter(tee('abc'))) |
---|
1427 | n/a | a, b = tee('abc') |
---|
1428 | n/a | self.pickletest(proto, a, compare=ans) |
---|
1429 | n/a | self.pickletest(proto, b, compare=ans) |
---|
1430 | n/a | |
---|
1431 | n/a | # Issue 13454: Crash when deleting backward iterator from tee() |
---|
1432 | n/a | def test_tee_del_backward(self): |
---|
1433 | n/a | forward, backward = tee(repeat(None, 20000000)) |
---|
1434 | n/a | try: |
---|
1435 | n/a | any(forward) # exhaust the iterator |
---|
1436 | n/a | del backward |
---|
1437 | n/a | except: |
---|
1438 | n/a | del forward, backward |
---|
1439 | n/a | raise |
---|
1440 | n/a | |
---|
1441 | n/a | def test_StopIteration(self): |
---|
1442 | n/a | self.assertRaises(StopIteration, next, zip()) |
---|
1443 | n/a | |
---|
1444 | n/a | for f in (chain, cycle, zip, groupby): |
---|
1445 | n/a | self.assertRaises(StopIteration, next, f([])) |
---|
1446 | n/a | self.assertRaises(StopIteration, next, f(StopNow())) |
---|
1447 | n/a | |
---|
1448 | n/a | self.assertRaises(StopIteration, next, islice([], None)) |
---|
1449 | n/a | self.assertRaises(StopIteration, next, islice(StopNow(), None)) |
---|
1450 | n/a | |
---|
1451 | n/a | p, q = tee([]) |
---|
1452 | n/a | self.assertRaises(StopIteration, next, p) |
---|
1453 | n/a | self.assertRaises(StopIteration, next, q) |
---|
1454 | n/a | p, q = tee(StopNow()) |
---|
1455 | n/a | self.assertRaises(StopIteration, next, p) |
---|
1456 | n/a | self.assertRaises(StopIteration, next, q) |
---|
1457 | n/a | |
---|
1458 | n/a | self.assertRaises(StopIteration, next, repeat(None, 0)) |
---|
1459 | n/a | |
---|
1460 | n/a | for f in (filter, filterfalse, map, takewhile, dropwhile, starmap): |
---|
1461 | n/a | self.assertRaises(StopIteration, next, f(lambda x:x, [])) |
---|
1462 | n/a | self.assertRaises(StopIteration, next, f(lambda x:x, StopNow())) |
---|
1463 | n/a | |
---|
1464 | n/a | class TestExamples(unittest.TestCase): |
---|
1465 | n/a | |
---|
1466 | n/a | def test_accumulate(self): |
---|
1467 | n/a | self.assertEqual(list(accumulate([1,2,3,4,5])), [1, 3, 6, 10, 15]) |
---|
1468 | n/a | |
---|
1469 | n/a | def test_accumulate_reducible(self): |
---|
1470 | n/a | # check copy, deepcopy, pickle |
---|
1471 | n/a | data = [1, 2, 3, 4, 5] |
---|
1472 | n/a | accumulated = [1, 3, 6, 10, 15] |
---|
1473 | n/a | |
---|
1474 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
1475 | n/a | it = accumulate(data) |
---|
1476 | n/a | self.assertEqual(list(pickle.loads(pickle.dumps(it, proto))), accumulated[:]) |
---|
1477 | n/a | self.assertEqual(next(it), 1) |
---|
1478 | n/a | self.assertEqual(list(pickle.loads(pickle.dumps(it, proto))), accumulated[1:]) |
---|
1479 | n/a | it = accumulate(data) |
---|
1480 | n/a | self.assertEqual(next(it), 1) |
---|
1481 | n/a | self.assertEqual(list(copy.deepcopy(it)), accumulated[1:]) |
---|
1482 | n/a | self.assertEqual(list(copy.copy(it)), accumulated[1:]) |
---|
1483 | n/a | |
---|
1484 | n/a | def test_accumulate_reducible_none(self): |
---|
1485 | n/a | # Issue #25718: total is None |
---|
1486 | n/a | it = accumulate([None, None, None], operator.is_) |
---|
1487 | n/a | self.assertEqual(next(it), None) |
---|
1488 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
1489 | n/a | it_copy = pickle.loads(pickle.dumps(it, proto)) |
---|
1490 | n/a | self.assertEqual(list(it_copy), [True, False]) |
---|
1491 | n/a | self.assertEqual(list(copy.deepcopy(it)), [True, False]) |
---|
1492 | n/a | self.assertEqual(list(copy.copy(it)), [True, False]) |
---|
1493 | n/a | |
---|
1494 | n/a | def test_chain(self): |
---|
1495 | n/a | self.assertEqual(''.join(chain('ABC', 'DEF')), 'ABCDEF') |
---|
1496 | n/a | |
---|
1497 | n/a | def test_chain_from_iterable(self): |
---|
1498 | n/a | self.assertEqual(''.join(chain.from_iterable(['ABC', 'DEF'])), 'ABCDEF') |
---|
1499 | n/a | |
---|
1500 | n/a | def test_combinations(self): |
---|
1501 | n/a | self.assertEqual(list(combinations('ABCD', 2)), |
---|
1502 | n/a | [('A','B'), ('A','C'), ('A','D'), ('B','C'), ('B','D'), ('C','D')]) |
---|
1503 | n/a | self.assertEqual(list(combinations(range(4), 3)), |
---|
1504 | n/a | [(0,1,2), (0,1,3), (0,2,3), (1,2,3)]) |
---|
1505 | n/a | |
---|
1506 | n/a | def test_combinations_with_replacement(self): |
---|
1507 | n/a | self.assertEqual(list(combinations_with_replacement('ABC', 2)), |
---|
1508 | n/a | [('A','A'), ('A','B'), ('A','C'), ('B','B'), ('B','C'), ('C','C')]) |
---|
1509 | n/a | |
---|
1510 | n/a | def test_compress(self): |
---|
1511 | n/a | self.assertEqual(list(compress('ABCDEF', [1,0,1,0,1,1])), list('ACEF')) |
---|
1512 | n/a | |
---|
1513 | n/a | def test_count(self): |
---|
1514 | n/a | self.assertEqual(list(islice(count(10), 5)), [10, 11, 12, 13, 14]) |
---|
1515 | n/a | |
---|
1516 | n/a | def test_cycle(self): |
---|
1517 | n/a | self.assertEqual(list(islice(cycle('ABCD'), 12)), list('ABCDABCDABCD')) |
---|
1518 | n/a | |
---|
1519 | n/a | def test_dropwhile(self): |
---|
1520 | n/a | self.assertEqual(list(dropwhile(lambda x: x<5, [1,4,6,4,1])), [6,4,1]) |
---|
1521 | n/a | |
---|
1522 | n/a | def test_groupby(self): |
---|
1523 | n/a | self.assertEqual([k for k, g in groupby('AAAABBBCCDAABBB')], |
---|
1524 | n/a | list('ABCDAB')) |
---|
1525 | n/a | self.assertEqual([(list(g)) for k, g in groupby('AAAABBBCCD')], |
---|
1526 | n/a | [list('AAAA'), list('BBB'), list('CC'), list('D')]) |
---|
1527 | n/a | |
---|
1528 | n/a | def test_filter(self): |
---|
1529 | n/a | self.assertEqual(list(filter(lambda x: x%2, range(10))), [1,3,5,7,9]) |
---|
1530 | n/a | |
---|
1531 | n/a | def test_filterfalse(self): |
---|
1532 | n/a | self.assertEqual(list(filterfalse(lambda x: x%2, range(10))), [0,2,4,6,8]) |
---|
1533 | n/a | |
---|
1534 | n/a | def test_map(self): |
---|
1535 | n/a | self.assertEqual(list(map(pow, (2,3,10), (5,2,3))), [32, 9, 1000]) |
---|
1536 | n/a | |
---|
1537 | n/a | def test_islice(self): |
---|
1538 | n/a | self.assertEqual(list(islice('ABCDEFG', 2)), list('AB')) |
---|
1539 | n/a | self.assertEqual(list(islice('ABCDEFG', 2, 4)), list('CD')) |
---|
1540 | n/a | self.assertEqual(list(islice('ABCDEFG', 2, None)), list('CDEFG')) |
---|
1541 | n/a | self.assertEqual(list(islice('ABCDEFG', 0, None, 2)), list('ACEG')) |
---|
1542 | n/a | |
---|
1543 | n/a | def test_zip(self): |
---|
1544 | n/a | self.assertEqual(list(zip('ABCD', 'xy')), [('A', 'x'), ('B', 'y')]) |
---|
1545 | n/a | |
---|
1546 | n/a | def test_zip_longest(self): |
---|
1547 | n/a | self.assertEqual(list(zip_longest('ABCD', 'xy', fillvalue='-')), |
---|
1548 | n/a | [('A', 'x'), ('B', 'y'), ('C', '-'), ('D', '-')]) |
---|
1549 | n/a | |
---|
1550 | n/a | def test_permutations(self): |
---|
1551 | n/a | self.assertEqual(list(permutations('ABCD', 2)), |
---|
1552 | n/a | list(map(tuple, 'AB AC AD BA BC BD CA CB CD DA DB DC'.split()))) |
---|
1553 | n/a | self.assertEqual(list(permutations(range(3))), |
---|
1554 | n/a | [(0,1,2), (0,2,1), (1,0,2), (1,2,0), (2,0,1), (2,1,0)]) |
---|
1555 | n/a | |
---|
1556 | n/a | def test_product(self): |
---|
1557 | n/a | self.assertEqual(list(product('ABCD', 'xy')), |
---|
1558 | n/a | list(map(tuple, 'Ax Ay Bx By Cx Cy Dx Dy'.split()))) |
---|
1559 | n/a | self.assertEqual(list(product(range(2), repeat=3)), |
---|
1560 | n/a | [(0,0,0), (0,0,1), (0,1,0), (0,1,1), |
---|
1561 | n/a | (1,0,0), (1,0,1), (1,1,0), (1,1,1)]) |
---|
1562 | n/a | |
---|
1563 | n/a | def test_repeat(self): |
---|
1564 | n/a | self.assertEqual(list(repeat(10, 3)), [10, 10, 10]) |
---|
1565 | n/a | |
---|
1566 | n/a | def test_stapmap(self): |
---|
1567 | n/a | self.assertEqual(list(starmap(pow, [(2,5), (3,2), (10,3)])), |
---|
1568 | n/a | [32, 9, 1000]) |
---|
1569 | n/a | |
---|
1570 | n/a | def test_takewhile(self): |
---|
1571 | n/a | self.assertEqual(list(takewhile(lambda x: x<5, [1,4,6,4,1])), [1,4]) |
---|
1572 | n/a | |
---|
1573 | n/a | |
---|
1574 | n/a | class TestGC(unittest.TestCase): |
---|
1575 | n/a | |
---|
1576 | n/a | def makecycle(self, iterator, container): |
---|
1577 | n/a | container.append(iterator) |
---|
1578 | n/a | next(iterator) |
---|
1579 | n/a | del container, iterator |
---|
1580 | n/a | |
---|
1581 | n/a | def test_accumulate(self): |
---|
1582 | n/a | a = [] |
---|
1583 | n/a | self.makecycle(accumulate([1,2,a,3]), a) |
---|
1584 | n/a | |
---|
1585 | n/a | def test_chain(self): |
---|
1586 | n/a | a = [] |
---|
1587 | n/a | self.makecycle(chain(a), a) |
---|
1588 | n/a | |
---|
1589 | n/a | def test_chain_from_iterable(self): |
---|
1590 | n/a | a = [] |
---|
1591 | n/a | self.makecycle(chain.from_iterable([a]), a) |
---|
1592 | n/a | |
---|
1593 | n/a | def test_combinations(self): |
---|
1594 | n/a | a = [] |
---|
1595 | n/a | self.makecycle(combinations([1,2,a,3], 3), a) |
---|
1596 | n/a | |
---|
1597 | n/a | def test_combinations_with_replacement(self): |
---|
1598 | n/a | a = [] |
---|
1599 | n/a | self.makecycle(combinations_with_replacement([1,2,a,3], 3), a) |
---|
1600 | n/a | |
---|
1601 | n/a | def test_compress(self): |
---|
1602 | n/a | a = [] |
---|
1603 | n/a | self.makecycle(compress('ABCDEF', [1,0,1,0,1,0]), a) |
---|
1604 | n/a | |
---|
1605 | n/a | def test_count(self): |
---|
1606 | n/a | a = [] |
---|
1607 | n/a | Int = type('Int', (int,), dict(x=a)) |
---|
1608 | n/a | self.makecycle(count(Int(0), Int(1)), a) |
---|
1609 | n/a | |
---|
1610 | n/a | def test_cycle(self): |
---|
1611 | n/a | a = [] |
---|
1612 | n/a | self.makecycle(cycle([a]*2), a) |
---|
1613 | n/a | |
---|
1614 | n/a | def test_dropwhile(self): |
---|
1615 | n/a | a = [] |
---|
1616 | n/a | self.makecycle(dropwhile(bool, [0, a, a]), a) |
---|
1617 | n/a | |
---|
1618 | n/a | def test_groupby(self): |
---|
1619 | n/a | a = [] |
---|
1620 | n/a | self.makecycle(groupby([a]*2, lambda x:x), a) |
---|
1621 | n/a | |
---|
1622 | n/a | def test_issue2246(self): |
---|
1623 | n/a | # Issue 2246 -- the _grouper iterator was not included in GC |
---|
1624 | n/a | n = 10 |
---|
1625 | n/a | keyfunc = lambda x: x |
---|
1626 | n/a | for i, j in groupby(range(n), key=keyfunc): |
---|
1627 | n/a | keyfunc.__dict__.setdefault('x',[]).append(j) |
---|
1628 | n/a | |
---|
1629 | n/a | def test_filter(self): |
---|
1630 | n/a | a = [] |
---|
1631 | n/a | self.makecycle(filter(lambda x:True, [a]*2), a) |
---|
1632 | n/a | |
---|
1633 | n/a | def test_filterfalse(self): |
---|
1634 | n/a | a = [] |
---|
1635 | n/a | self.makecycle(filterfalse(lambda x:False, a), a) |
---|
1636 | n/a | |
---|
1637 | n/a | def test_zip(self): |
---|
1638 | n/a | a = [] |
---|
1639 | n/a | self.makecycle(zip([a]*2, [a]*3), a) |
---|
1640 | n/a | |
---|
1641 | n/a | def test_zip_longest(self): |
---|
1642 | n/a | a = [] |
---|
1643 | n/a | self.makecycle(zip_longest([a]*2, [a]*3), a) |
---|
1644 | n/a | b = [a, None] |
---|
1645 | n/a | self.makecycle(zip_longest([a]*2, [a]*3, fillvalue=b), a) |
---|
1646 | n/a | |
---|
1647 | n/a | def test_map(self): |
---|
1648 | n/a | a = [] |
---|
1649 | n/a | self.makecycle(map(lambda x:x, [a]*2), a) |
---|
1650 | n/a | |
---|
1651 | n/a | def test_islice(self): |
---|
1652 | n/a | a = [] |
---|
1653 | n/a | self.makecycle(islice([a]*2, None), a) |
---|
1654 | n/a | |
---|
1655 | n/a | def test_permutations(self): |
---|
1656 | n/a | a = [] |
---|
1657 | n/a | self.makecycle(permutations([1,2,a,3], 3), a) |
---|
1658 | n/a | |
---|
1659 | n/a | def test_product(self): |
---|
1660 | n/a | a = [] |
---|
1661 | n/a | self.makecycle(product([1,2,a,3], repeat=3), a) |
---|
1662 | n/a | |
---|
1663 | n/a | def test_repeat(self): |
---|
1664 | n/a | a = [] |
---|
1665 | n/a | self.makecycle(repeat(a), a) |
---|
1666 | n/a | |
---|
1667 | n/a | def test_starmap(self): |
---|
1668 | n/a | a = [] |
---|
1669 | n/a | self.makecycle(starmap(lambda *t: t, [(a,a)]*2), a) |
---|
1670 | n/a | |
---|
1671 | n/a | def test_takewhile(self): |
---|
1672 | n/a | a = [] |
---|
1673 | n/a | self.makecycle(takewhile(bool, [1, 0, a, a]), a) |
---|
1674 | n/a | |
---|
1675 | n/a | def R(seqn): |
---|
1676 | n/a | 'Regular generator' |
---|
1677 | n/a | for i in seqn: |
---|
1678 | n/a | yield i |
---|
1679 | n/a | |
---|
1680 | n/a | class G: |
---|
1681 | n/a | 'Sequence using __getitem__' |
---|
1682 | n/a | def __init__(self, seqn): |
---|
1683 | n/a | self.seqn = seqn |
---|
1684 | n/a | def __getitem__(self, i): |
---|
1685 | n/a | return self.seqn[i] |
---|
1686 | n/a | |
---|
1687 | n/a | class I: |
---|
1688 | n/a | 'Sequence using iterator protocol' |
---|
1689 | n/a | def __init__(self, seqn): |
---|
1690 | n/a | self.seqn = seqn |
---|
1691 | n/a | self.i = 0 |
---|
1692 | n/a | def __iter__(self): |
---|
1693 | n/a | return self |
---|
1694 | n/a | def __next__(self): |
---|
1695 | n/a | if self.i >= len(self.seqn): raise StopIteration |
---|
1696 | n/a | v = self.seqn[self.i] |
---|
1697 | n/a | self.i += 1 |
---|
1698 | n/a | return v |
---|
1699 | n/a | |
---|
1700 | n/a | class Ig: |
---|
1701 | n/a | 'Sequence using iterator protocol defined with a generator' |
---|
1702 | n/a | def __init__(self, seqn): |
---|
1703 | n/a | self.seqn = seqn |
---|
1704 | n/a | self.i = 0 |
---|
1705 | n/a | def __iter__(self): |
---|
1706 | n/a | for val in self.seqn: |
---|
1707 | n/a | yield val |
---|
1708 | n/a | |
---|
1709 | n/a | class X: |
---|
1710 | n/a | 'Missing __getitem__ and __iter__' |
---|
1711 | n/a | def __init__(self, seqn): |
---|
1712 | n/a | self.seqn = seqn |
---|
1713 | n/a | self.i = 0 |
---|
1714 | n/a | def __next__(self): |
---|
1715 | n/a | if self.i >= len(self.seqn): raise StopIteration |
---|
1716 | n/a | v = self.seqn[self.i] |
---|
1717 | n/a | self.i += 1 |
---|
1718 | n/a | return v |
---|
1719 | n/a | |
---|
1720 | n/a | class N: |
---|
1721 | n/a | 'Iterator missing __next__()' |
---|
1722 | n/a | def __init__(self, seqn): |
---|
1723 | n/a | self.seqn = seqn |
---|
1724 | n/a | self.i = 0 |
---|
1725 | n/a | def __iter__(self): |
---|
1726 | n/a | return self |
---|
1727 | n/a | |
---|
1728 | n/a | class E: |
---|
1729 | n/a | 'Test propagation of exceptions' |
---|
1730 | n/a | def __init__(self, seqn): |
---|
1731 | n/a | self.seqn = seqn |
---|
1732 | n/a | self.i = 0 |
---|
1733 | n/a | def __iter__(self): |
---|
1734 | n/a | return self |
---|
1735 | n/a | def __next__(self): |
---|
1736 | n/a | 3 // 0 |
---|
1737 | n/a | |
---|
1738 | n/a | class S: |
---|
1739 | n/a | 'Test immediate stop' |
---|
1740 | n/a | def __init__(self, seqn): |
---|
1741 | n/a | pass |
---|
1742 | n/a | def __iter__(self): |
---|
1743 | n/a | return self |
---|
1744 | n/a | def __next__(self): |
---|
1745 | n/a | raise StopIteration |
---|
1746 | n/a | |
---|
1747 | n/a | def L(seqn): |
---|
1748 | n/a | 'Test multiple tiers of iterators' |
---|
1749 | n/a | return chain(map(lambda x:x, R(Ig(G(seqn))))) |
---|
1750 | n/a | |
---|
1751 | n/a | |
---|
1752 | n/a | class TestVariousIteratorArgs(unittest.TestCase): |
---|
1753 | n/a | |
---|
1754 | n/a | def test_accumulate(self): |
---|
1755 | n/a | s = [1,2,3,4,5] |
---|
1756 | n/a | r = [1,3,6,10,15] |
---|
1757 | n/a | n = len(s) |
---|
1758 | n/a | for g in (G, I, Ig, L, R): |
---|
1759 | n/a | self.assertEqual(list(accumulate(g(s))), r) |
---|
1760 | n/a | self.assertEqual(list(accumulate(S(s))), []) |
---|
1761 | n/a | self.assertRaises(TypeError, accumulate, X(s)) |
---|
1762 | n/a | self.assertRaises(TypeError, accumulate, N(s)) |
---|
1763 | n/a | self.assertRaises(ZeroDivisionError, list, accumulate(E(s))) |
---|
1764 | n/a | |
---|
1765 | n/a | def test_chain(self): |
---|
1766 | n/a | for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)): |
---|
1767 | n/a | for g in (G, I, Ig, S, L, R): |
---|
1768 | n/a | self.assertEqual(list(chain(g(s))), list(g(s))) |
---|
1769 | n/a | self.assertEqual(list(chain(g(s), g(s))), list(g(s))+list(g(s))) |
---|
1770 | n/a | self.assertRaises(TypeError, list, chain(X(s))) |
---|
1771 | n/a | self.assertRaises(TypeError, list, chain(N(s))) |
---|
1772 | n/a | self.assertRaises(ZeroDivisionError, list, chain(E(s))) |
---|
1773 | n/a | |
---|
1774 | n/a | def test_compress(self): |
---|
1775 | n/a | for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)): |
---|
1776 | n/a | n = len(s) |
---|
1777 | n/a | for g in (G, I, Ig, S, L, R): |
---|
1778 | n/a | self.assertEqual(list(compress(g(s), repeat(1))), list(g(s))) |
---|
1779 | n/a | self.assertRaises(TypeError, compress, X(s), repeat(1)) |
---|
1780 | n/a | self.assertRaises(TypeError, compress, N(s), repeat(1)) |
---|
1781 | n/a | self.assertRaises(ZeroDivisionError, list, compress(E(s), repeat(1))) |
---|
1782 | n/a | |
---|
1783 | n/a | def test_product(self): |
---|
1784 | n/a | for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)): |
---|
1785 | n/a | self.assertRaises(TypeError, product, X(s)) |
---|
1786 | n/a | self.assertRaises(TypeError, product, N(s)) |
---|
1787 | n/a | self.assertRaises(ZeroDivisionError, product, E(s)) |
---|
1788 | n/a | |
---|
1789 | n/a | def test_cycle(self): |
---|
1790 | n/a | for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)): |
---|
1791 | n/a | for g in (G, I, Ig, S, L, R): |
---|
1792 | n/a | tgtlen = len(s) * 3 |
---|
1793 | n/a | expected = list(g(s))*3 |
---|
1794 | n/a | actual = list(islice(cycle(g(s)), tgtlen)) |
---|
1795 | n/a | self.assertEqual(actual, expected) |
---|
1796 | n/a | self.assertRaises(TypeError, cycle, X(s)) |
---|
1797 | n/a | self.assertRaises(TypeError, cycle, N(s)) |
---|
1798 | n/a | self.assertRaises(ZeroDivisionError, list, cycle(E(s))) |
---|
1799 | n/a | |
---|
1800 | n/a | def test_groupby(self): |
---|
1801 | n/a | for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)): |
---|
1802 | n/a | for g in (G, I, Ig, S, L, R): |
---|
1803 | n/a | self.assertEqual([k for k, sb in groupby(g(s))], list(g(s))) |
---|
1804 | n/a | self.assertRaises(TypeError, groupby, X(s)) |
---|
1805 | n/a | self.assertRaises(TypeError, groupby, N(s)) |
---|
1806 | n/a | self.assertRaises(ZeroDivisionError, list, groupby(E(s))) |
---|
1807 | n/a | |
---|
1808 | n/a | def test_filter(self): |
---|
1809 | n/a | for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)): |
---|
1810 | n/a | for g in (G, I, Ig, S, L, R): |
---|
1811 | n/a | self.assertEqual(list(filter(isEven, g(s))), |
---|
1812 | n/a | [x for x in g(s) if isEven(x)]) |
---|
1813 | n/a | self.assertRaises(TypeError, filter, isEven, X(s)) |
---|
1814 | n/a | self.assertRaises(TypeError, filter, isEven, N(s)) |
---|
1815 | n/a | self.assertRaises(ZeroDivisionError, list, filter(isEven, E(s))) |
---|
1816 | n/a | |
---|
1817 | n/a | def test_filterfalse(self): |
---|
1818 | n/a | for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)): |
---|
1819 | n/a | for g in (G, I, Ig, S, L, R): |
---|
1820 | n/a | self.assertEqual(list(filterfalse(isEven, g(s))), |
---|
1821 | n/a | [x for x in g(s) if isOdd(x)]) |
---|
1822 | n/a | self.assertRaises(TypeError, filterfalse, isEven, X(s)) |
---|
1823 | n/a | self.assertRaises(TypeError, filterfalse, isEven, N(s)) |
---|
1824 | n/a | self.assertRaises(ZeroDivisionError, list, filterfalse(isEven, E(s))) |
---|
1825 | n/a | |
---|
1826 | n/a | def test_zip(self): |
---|
1827 | n/a | for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)): |
---|
1828 | n/a | for g in (G, I, Ig, S, L, R): |
---|
1829 | n/a | self.assertEqual(list(zip(g(s))), lzip(g(s))) |
---|
1830 | n/a | self.assertEqual(list(zip(g(s), g(s))), lzip(g(s), g(s))) |
---|
1831 | n/a | self.assertRaises(TypeError, zip, X(s)) |
---|
1832 | n/a | self.assertRaises(TypeError, zip, N(s)) |
---|
1833 | n/a | self.assertRaises(ZeroDivisionError, list, zip(E(s))) |
---|
1834 | n/a | |
---|
1835 | n/a | def test_ziplongest(self): |
---|
1836 | n/a | for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)): |
---|
1837 | n/a | for g in (G, I, Ig, S, L, R): |
---|
1838 | n/a | self.assertEqual(list(zip_longest(g(s))), list(zip(g(s)))) |
---|
1839 | n/a | self.assertEqual(list(zip_longest(g(s), g(s))), list(zip(g(s), g(s)))) |
---|
1840 | n/a | self.assertRaises(TypeError, zip_longest, X(s)) |
---|
1841 | n/a | self.assertRaises(TypeError, zip_longest, N(s)) |
---|
1842 | n/a | self.assertRaises(ZeroDivisionError, list, zip_longest(E(s))) |
---|
1843 | n/a | |
---|
1844 | n/a | def test_map(self): |
---|
1845 | n/a | for s in (range(10), range(0), range(100), (7,11), range(20,50,5)): |
---|
1846 | n/a | for g in (G, I, Ig, S, L, R): |
---|
1847 | n/a | self.assertEqual(list(map(onearg, g(s))), |
---|
1848 | n/a | [onearg(x) for x in g(s)]) |
---|
1849 | n/a | self.assertEqual(list(map(operator.pow, g(s), g(s))), |
---|
1850 | n/a | [x**x for x in g(s)]) |
---|
1851 | n/a | self.assertRaises(TypeError, map, onearg, X(s)) |
---|
1852 | n/a | self.assertRaises(TypeError, map, onearg, N(s)) |
---|
1853 | n/a | self.assertRaises(ZeroDivisionError, list, map(onearg, E(s))) |
---|
1854 | n/a | |
---|
1855 | n/a | def test_islice(self): |
---|
1856 | n/a | for s in ("12345", "", range(1000), ('do', 1.2), range(2000,2200,5)): |
---|
1857 | n/a | for g in (G, I, Ig, S, L, R): |
---|
1858 | n/a | self.assertEqual(list(islice(g(s),1,None,2)), list(g(s))[1::2]) |
---|
1859 | n/a | self.assertRaises(TypeError, islice, X(s), 10) |
---|
1860 | n/a | self.assertRaises(TypeError, islice, N(s), 10) |
---|
1861 | n/a | self.assertRaises(ZeroDivisionError, list, islice(E(s), 10)) |
---|
1862 | n/a | |
---|
1863 | n/a | def test_starmap(self): |
---|
1864 | n/a | for s in (range(10), range(0), range(100), (7,11), range(20,50,5)): |
---|
1865 | n/a | for g in (G, I, Ig, S, L, R): |
---|
1866 | n/a | ss = lzip(s, s) |
---|
1867 | n/a | self.assertEqual(list(starmap(operator.pow, g(ss))), |
---|
1868 | n/a | [x**x for x in g(s)]) |
---|
1869 | n/a | self.assertRaises(TypeError, starmap, operator.pow, X(ss)) |
---|
1870 | n/a | self.assertRaises(TypeError, starmap, operator.pow, N(ss)) |
---|
1871 | n/a | self.assertRaises(ZeroDivisionError, list, starmap(operator.pow, E(ss))) |
---|
1872 | n/a | |
---|
1873 | n/a | def test_takewhile(self): |
---|
1874 | n/a | for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)): |
---|
1875 | n/a | for g in (G, I, Ig, S, L, R): |
---|
1876 | n/a | tgt = [] |
---|
1877 | n/a | for elem in g(s): |
---|
1878 | n/a | if not isEven(elem): break |
---|
1879 | n/a | tgt.append(elem) |
---|
1880 | n/a | self.assertEqual(list(takewhile(isEven, g(s))), tgt) |
---|
1881 | n/a | self.assertRaises(TypeError, takewhile, isEven, X(s)) |
---|
1882 | n/a | self.assertRaises(TypeError, takewhile, isEven, N(s)) |
---|
1883 | n/a | self.assertRaises(ZeroDivisionError, list, takewhile(isEven, E(s))) |
---|
1884 | n/a | |
---|
1885 | n/a | def test_dropwhile(self): |
---|
1886 | n/a | for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)): |
---|
1887 | n/a | for g in (G, I, Ig, S, L, R): |
---|
1888 | n/a | tgt = [] |
---|
1889 | n/a | for elem in g(s): |
---|
1890 | n/a | if not tgt and isOdd(elem): continue |
---|
1891 | n/a | tgt.append(elem) |
---|
1892 | n/a | self.assertEqual(list(dropwhile(isOdd, g(s))), tgt) |
---|
1893 | n/a | self.assertRaises(TypeError, dropwhile, isOdd, X(s)) |
---|
1894 | n/a | self.assertRaises(TypeError, dropwhile, isOdd, N(s)) |
---|
1895 | n/a | self.assertRaises(ZeroDivisionError, list, dropwhile(isOdd, E(s))) |
---|
1896 | n/a | |
---|
1897 | n/a | def test_tee(self): |
---|
1898 | n/a | for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)): |
---|
1899 | n/a | for g in (G, I, Ig, S, L, R): |
---|
1900 | n/a | it1, it2 = tee(g(s)) |
---|
1901 | n/a | self.assertEqual(list(it1), list(g(s))) |
---|
1902 | n/a | self.assertEqual(list(it2), list(g(s))) |
---|
1903 | n/a | self.assertRaises(TypeError, tee, X(s)) |
---|
1904 | n/a | self.assertRaises(TypeError, tee, N(s)) |
---|
1905 | n/a | self.assertRaises(ZeroDivisionError, list, tee(E(s))[0]) |
---|
1906 | n/a | |
---|
1907 | n/a | class LengthTransparency(unittest.TestCase): |
---|
1908 | n/a | |
---|
1909 | n/a | def test_repeat(self): |
---|
1910 | n/a | self.assertEqual(operator.length_hint(repeat(None, 50)), 50) |
---|
1911 | n/a | self.assertEqual(operator.length_hint(repeat(None, 0)), 0) |
---|
1912 | n/a | self.assertEqual(operator.length_hint(repeat(None), 12), 12) |
---|
1913 | n/a | |
---|
1914 | n/a | def test_repeat_with_negative_times(self): |
---|
1915 | n/a | self.assertEqual(operator.length_hint(repeat(None, -1)), 0) |
---|
1916 | n/a | self.assertEqual(operator.length_hint(repeat(None, -2)), 0) |
---|
1917 | n/a | self.assertEqual(operator.length_hint(repeat(None, times=-1)), 0) |
---|
1918 | n/a | self.assertEqual(operator.length_hint(repeat(None, times=-2)), 0) |
---|
1919 | n/a | |
---|
1920 | n/a | class RegressionTests(unittest.TestCase): |
---|
1921 | n/a | |
---|
1922 | n/a | def test_sf_793826(self): |
---|
1923 | n/a | # Fix Armin Rigo's successful efforts to wreak havoc |
---|
1924 | n/a | |
---|
1925 | n/a | def mutatingtuple(tuple1, f, tuple2): |
---|
1926 | n/a | # this builds a tuple t which is a copy of tuple1, |
---|
1927 | n/a | # then calls f(t), then mutates t to be equal to tuple2 |
---|
1928 | n/a | # (needs len(tuple1) == len(tuple2)). |
---|
1929 | n/a | def g(value, first=[1]): |
---|
1930 | n/a | if first: |
---|
1931 | n/a | del first[:] |
---|
1932 | n/a | f(next(z)) |
---|
1933 | n/a | return value |
---|
1934 | n/a | items = list(tuple2) |
---|
1935 | n/a | items[1:1] = list(tuple1) |
---|
1936 | n/a | gen = map(g, items) |
---|
1937 | n/a | z = zip(*[gen]*len(tuple1)) |
---|
1938 | n/a | next(z) |
---|
1939 | n/a | |
---|
1940 | n/a | def f(t): |
---|
1941 | n/a | global T |
---|
1942 | n/a | T = t |
---|
1943 | n/a | first[:] = list(T) |
---|
1944 | n/a | |
---|
1945 | n/a | first = [] |
---|
1946 | n/a | mutatingtuple((1,2,3), f, (4,5,6)) |
---|
1947 | n/a | second = list(T) |
---|
1948 | n/a | self.assertEqual(first, second) |
---|
1949 | n/a | |
---|
1950 | n/a | |
---|
1951 | n/a | def test_sf_950057(self): |
---|
1952 | n/a | # Make sure that chain() and cycle() catch exceptions immediately |
---|
1953 | n/a | # rather than when shifting between input sources |
---|
1954 | n/a | |
---|
1955 | n/a | def gen1(): |
---|
1956 | n/a | hist.append(0) |
---|
1957 | n/a | yield 1 |
---|
1958 | n/a | hist.append(1) |
---|
1959 | n/a | raise AssertionError |
---|
1960 | n/a | hist.append(2) |
---|
1961 | n/a | |
---|
1962 | n/a | def gen2(x): |
---|
1963 | n/a | hist.append(3) |
---|
1964 | n/a | yield 2 |
---|
1965 | n/a | hist.append(4) |
---|
1966 | n/a | |
---|
1967 | n/a | hist = [] |
---|
1968 | n/a | self.assertRaises(AssertionError, list, chain(gen1(), gen2(False))) |
---|
1969 | n/a | self.assertEqual(hist, [0,1]) |
---|
1970 | n/a | |
---|
1971 | n/a | hist = [] |
---|
1972 | n/a | self.assertRaises(AssertionError, list, chain(gen1(), gen2(True))) |
---|
1973 | n/a | self.assertEqual(hist, [0,1]) |
---|
1974 | n/a | |
---|
1975 | n/a | hist = [] |
---|
1976 | n/a | self.assertRaises(AssertionError, list, cycle(gen1())) |
---|
1977 | n/a | self.assertEqual(hist, [0,1]) |
---|
1978 | n/a | |
---|
1979 | n/a | class SubclassWithKwargsTest(unittest.TestCase): |
---|
1980 | n/a | def test_keywords_in_subclass(self): |
---|
1981 | n/a | # count is not subclassable... |
---|
1982 | n/a | for cls in (repeat, zip, filter, filterfalse, chain, map, |
---|
1983 | n/a | starmap, islice, takewhile, dropwhile, cycle, compress): |
---|
1984 | n/a | class Subclass(cls): |
---|
1985 | n/a | def __init__(self, newarg=None, *args): |
---|
1986 | n/a | cls.__init__(self, *args) |
---|
1987 | n/a | try: |
---|
1988 | n/a | Subclass(newarg=1) |
---|
1989 | n/a | except TypeError as err: |
---|
1990 | n/a | # we expect type errors because of wrong argument count |
---|
1991 | n/a | self.assertNotIn("does not take keyword arguments", err.args[0]) |
---|
1992 | n/a | |
---|
1993 | n/a | @support.cpython_only |
---|
1994 | n/a | class SizeofTest(unittest.TestCase): |
---|
1995 | n/a | def setUp(self): |
---|
1996 | n/a | self.ssize_t = struct.calcsize('n') |
---|
1997 | n/a | |
---|
1998 | n/a | check_sizeof = support.check_sizeof |
---|
1999 | n/a | |
---|
2000 | n/a | def test_product_sizeof(self): |
---|
2001 | n/a | basesize = support.calcobjsize('3Pi') |
---|
2002 | n/a | check = self.check_sizeof |
---|
2003 | n/a | check(product('ab', '12'), basesize + 2 * self.ssize_t) |
---|
2004 | n/a | check(product(*(('abc',) * 10)), basesize + 10 * self.ssize_t) |
---|
2005 | n/a | |
---|
2006 | n/a | def test_combinations_sizeof(self): |
---|
2007 | n/a | basesize = support.calcobjsize('3Pni') |
---|
2008 | n/a | check = self.check_sizeof |
---|
2009 | n/a | check(combinations('abcd', 3), basesize + 3 * self.ssize_t) |
---|
2010 | n/a | check(combinations(range(10), 4), basesize + 4 * self.ssize_t) |
---|
2011 | n/a | |
---|
2012 | n/a | def test_combinations_with_replacement_sizeof(self): |
---|
2013 | n/a | cwr = combinations_with_replacement |
---|
2014 | n/a | basesize = support.calcobjsize('3Pni') |
---|
2015 | n/a | check = self.check_sizeof |
---|
2016 | n/a | check(cwr('abcd', 3), basesize + 3 * self.ssize_t) |
---|
2017 | n/a | check(cwr(range(10), 4), basesize + 4 * self.ssize_t) |
---|
2018 | n/a | |
---|
2019 | n/a | def test_permutations_sizeof(self): |
---|
2020 | n/a | basesize = support.calcobjsize('4Pni') |
---|
2021 | n/a | check = self.check_sizeof |
---|
2022 | n/a | check(permutations('abcd'), |
---|
2023 | n/a | basesize + 4 * self.ssize_t + 4 * self.ssize_t) |
---|
2024 | n/a | check(permutations('abcd', 3), |
---|
2025 | n/a | basesize + 4 * self.ssize_t + 3 * self.ssize_t) |
---|
2026 | n/a | check(permutations('abcde', 3), |
---|
2027 | n/a | basesize + 5 * self.ssize_t + 3 * self.ssize_t) |
---|
2028 | n/a | check(permutations(range(10), 4), |
---|
2029 | n/a | basesize + 10 * self.ssize_t + 4 * self.ssize_t) |
---|
2030 | n/a | |
---|
2031 | n/a | |
---|
2032 | n/a | libreftest = """ Doctest for examples in the library reference: libitertools.tex |
---|
2033 | n/a | |
---|
2034 | n/a | |
---|
2035 | n/a | >>> amounts = [120.15, 764.05, 823.14] |
---|
2036 | n/a | >>> for checknum, amount in zip(count(1200), amounts): |
---|
2037 | n/a | ... print('Check %d is for $%.2f' % (checknum, amount)) |
---|
2038 | n/a | ... |
---|
2039 | n/a | Check 1200 is for $120.15 |
---|
2040 | n/a | Check 1201 is for $764.05 |
---|
2041 | n/a | Check 1202 is for $823.14 |
---|
2042 | n/a | |
---|
2043 | n/a | >>> import operator |
---|
2044 | n/a | >>> for cube in map(operator.pow, range(1,4), repeat(3)): |
---|
2045 | n/a | ... print(cube) |
---|
2046 | n/a | ... |
---|
2047 | n/a | 1 |
---|
2048 | n/a | 8 |
---|
2049 | n/a | 27 |
---|
2050 | n/a | |
---|
2051 | n/a | >>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele'] |
---|
2052 | n/a | >>> for name in islice(reportlines, 3, None, 2): |
---|
2053 | n/a | ... print(name.title()) |
---|
2054 | n/a | ... |
---|
2055 | n/a | Alex |
---|
2056 | n/a | Laura |
---|
2057 | n/a | Martin |
---|
2058 | n/a | Walter |
---|
2059 | n/a | Samuele |
---|
2060 | n/a | |
---|
2061 | n/a | >>> from operator import itemgetter |
---|
2062 | n/a | >>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3) |
---|
2063 | n/a | >>> di = sorted(sorted(d.items()), key=itemgetter(1)) |
---|
2064 | n/a | >>> for k, g in groupby(di, itemgetter(1)): |
---|
2065 | n/a | ... print(k, list(map(itemgetter(0), g))) |
---|
2066 | n/a | ... |
---|
2067 | n/a | 1 ['a', 'c', 'e'] |
---|
2068 | n/a | 2 ['b', 'd', 'f'] |
---|
2069 | n/a | 3 ['g'] |
---|
2070 | n/a | |
---|
2071 | n/a | # Find runs of consecutive numbers using groupby. The key to the solution |
---|
2072 | n/a | # is differencing with a range so that consecutive numbers all appear in |
---|
2073 | n/a | # same group. |
---|
2074 | n/a | >>> data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28] |
---|
2075 | n/a | >>> for k, g in groupby(enumerate(data), lambda t:t[0]-t[1]): |
---|
2076 | n/a | ... print(list(map(operator.itemgetter(1), g))) |
---|
2077 | n/a | ... |
---|
2078 | n/a | [1] |
---|
2079 | n/a | [4, 5, 6] |
---|
2080 | n/a | [10] |
---|
2081 | n/a | [15, 16, 17, 18] |
---|
2082 | n/a | [22] |
---|
2083 | n/a | [25, 26, 27, 28] |
---|
2084 | n/a | |
---|
2085 | n/a | >>> def take(n, iterable): |
---|
2086 | n/a | ... "Return first n items of the iterable as a list" |
---|
2087 | n/a | ... return list(islice(iterable, n)) |
---|
2088 | n/a | |
---|
2089 | n/a | >>> def enumerate(iterable, start=0): |
---|
2090 | n/a | ... return zip(count(start), iterable) |
---|
2091 | n/a | |
---|
2092 | n/a | >>> def tabulate(function, start=0): |
---|
2093 | n/a | ... "Return function(0), function(1), ..." |
---|
2094 | n/a | ... return map(function, count(start)) |
---|
2095 | n/a | |
---|
2096 | n/a | >>> def nth(iterable, n, default=None): |
---|
2097 | n/a | ... "Returns the nth item or a default value" |
---|
2098 | n/a | ... return next(islice(iterable, n, None), default) |
---|
2099 | n/a | |
---|
2100 | n/a | >>> def all_equal(iterable): |
---|
2101 | n/a | ... "Returns True if all the elements are equal to each other" |
---|
2102 | n/a | ... g = groupby(iterable) |
---|
2103 | n/a | ... return next(g, True) and not next(g, False) |
---|
2104 | n/a | |
---|
2105 | n/a | >>> def quantify(iterable, pred=bool): |
---|
2106 | n/a | ... "Count how many times the predicate is true" |
---|
2107 | n/a | ... return sum(map(pred, iterable)) |
---|
2108 | n/a | |
---|
2109 | n/a | >>> def padnone(iterable): |
---|
2110 | n/a | ... "Returns the sequence elements and then returns None indefinitely" |
---|
2111 | n/a | ... return chain(iterable, repeat(None)) |
---|
2112 | n/a | |
---|
2113 | n/a | >>> def ncycles(iterable, n): |
---|
2114 | n/a | ... "Returns the sequence elements n times" |
---|
2115 | n/a | ... return chain(*repeat(iterable, n)) |
---|
2116 | n/a | |
---|
2117 | n/a | >>> def dotproduct(vec1, vec2): |
---|
2118 | n/a | ... return sum(map(operator.mul, vec1, vec2)) |
---|
2119 | n/a | |
---|
2120 | n/a | >>> def flatten(listOfLists): |
---|
2121 | n/a | ... return list(chain.from_iterable(listOfLists)) |
---|
2122 | n/a | |
---|
2123 | n/a | >>> def repeatfunc(func, times=None, *args): |
---|
2124 | n/a | ... "Repeat calls to func with specified arguments." |
---|
2125 | n/a | ... " Example: repeatfunc(random.random)" |
---|
2126 | n/a | ... if times is None: |
---|
2127 | n/a | ... return starmap(func, repeat(args)) |
---|
2128 | n/a | ... else: |
---|
2129 | n/a | ... return starmap(func, repeat(args, times)) |
---|
2130 | n/a | |
---|
2131 | n/a | >>> def pairwise(iterable): |
---|
2132 | n/a | ... "s -> (s0,s1), (s1,s2), (s2, s3), ..." |
---|
2133 | n/a | ... a, b = tee(iterable) |
---|
2134 | n/a | ... try: |
---|
2135 | n/a | ... next(b) |
---|
2136 | n/a | ... except StopIteration: |
---|
2137 | n/a | ... pass |
---|
2138 | n/a | ... return zip(a, b) |
---|
2139 | n/a | |
---|
2140 | n/a | >>> def grouper(n, iterable, fillvalue=None): |
---|
2141 | n/a | ... "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx" |
---|
2142 | n/a | ... args = [iter(iterable)] * n |
---|
2143 | n/a | ... return zip_longest(*args, fillvalue=fillvalue) |
---|
2144 | n/a | |
---|
2145 | n/a | >>> def roundrobin(*iterables): |
---|
2146 | n/a | ... "roundrobin('ABC', 'D', 'EF') --> A D E B F C" |
---|
2147 | n/a | ... # Recipe credited to George Sakkis |
---|
2148 | n/a | ... pending = len(iterables) |
---|
2149 | n/a | ... nexts = cycle(iter(it).__next__ for it in iterables) |
---|
2150 | n/a | ... while pending: |
---|
2151 | n/a | ... try: |
---|
2152 | n/a | ... for next in nexts: |
---|
2153 | n/a | ... yield next() |
---|
2154 | n/a | ... except StopIteration: |
---|
2155 | n/a | ... pending -= 1 |
---|
2156 | n/a | ... nexts = cycle(islice(nexts, pending)) |
---|
2157 | n/a | |
---|
2158 | n/a | >>> def powerset(iterable): |
---|
2159 | n/a | ... "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)" |
---|
2160 | n/a | ... s = list(iterable) |
---|
2161 | n/a | ... return chain.from_iterable(combinations(s, r) for r in range(len(s)+1)) |
---|
2162 | n/a | |
---|
2163 | n/a | >>> def unique_everseen(iterable, key=None): |
---|
2164 | n/a | ... "List unique elements, preserving order. Remember all elements ever seen." |
---|
2165 | n/a | ... # unique_everseen('AAAABBBCCDAABBB') --> A B C D |
---|
2166 | n/a | ... # unique_everseen('ABBCcAD', str.lower) --> A B C D |
---|
2167 | n/a | ... seen = set() |
---|
2168 | n/a | ... seen_add = seen.add |
---|
2169 | n/a | ... if key is None: |
---|
2170 | n/a | ... for element in iterable: |
---|
2171 | n/a | ... if element not in seen: |
---|
2172 | n/a | ... seen_add(element) |
---|
2173 | n/a | ... yield element |
---|
2174 | n/a | ... else: |
---|
2175 | n/a | ... for element in iterable: |
---|
2176 | n/a | ... k = key(element) |
---|
2177 | n/a | ... if k not in seen: |
---|
2178 | n/a | ... seen_add(k) |
---|
2179 | n/a | ... yield element |
---|
2180 | n/a | |
---|
2181 | n/a | >>> def unique_justseen(iterable, key=None): |
---|
2182 | n/a | ... "List unique elements, preserving order. Remember only the element just seen." |
---|
2183 | n/a | ... # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B |
---|
2184 | n/a | ... # unique_justseen('ABBCcAD', str.lower) --> A B C A D |
---|
2185 | n/a | ... return map(next, map(itemgetter(1), groupby(iterable, key))) |
---|
2186 | n/a | |
---|
2187 | n/a | >>> def first_true(iterable, default=False, pred=None): |
---|
2188 | n/a | ... '''Returns the first true value in the iterable. |
---|
2189 | n/a | ... |
---|
2190 | n/a | ... If no true value is found, returns *default* |
---|
2191 | n/a | ... |
---|
2192 | n/a | ... If *pred* is not None, returns the first item |
---|
2193 | n/a | ... for which pred(item) is true. |
---|
2194 | n/a | ... |
---|
2195 | n/a | ... ''' |
---|
2196 | n/a | ... # first_true([a,b,c], x) --> a or b or c or x |
---|
2197 | n/a | ... # first_true([a,b], x, f) --> a if f(a) else b if f(b) else x |
---|
2198 | n/a | ... return next(filter(pred, iterable), default) |
---|
2199 | n/a | |
---|
2200 | n/a | This is not part of the examples but it tests to make sure the definitions |
---|
2201 | n/a | perform as purported. |
---|
2202 | n/a | |
---|
2203 | n/a | >>> take(10, count()) |
---|
2204 | n/a | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
---|
2205 | n/a | |
---|
2206 | n/a | >>> list(enumerate('abc')) |
---|
2207 | n/a | [(0, 'a'), (1, 'b'), (2, 'c')] |
---|
2208 | n/a | |
---|
2209 | n/a | >>> list(islice(tabulate(lambda x: 2*x), 4)) |
---|
2210 | n/a | [0, 2, 4, 6] |
---|
2211 | n/a | |
---|
2212 | n/a | >>> nth('abcde', 3) |
---|
2213 | n/a | 'd' |
---|
2214 | n/a | |
---|
2215 | n/a | >>> nth('abcde', 9) is None |
---|
2216 | n/a | True |
---|
2217 | n/a | |
---|
2218 | n/a | >>> [all_equal(s) for s in ('', 'A', 'AAAA', 'AAAB', 'AAABA')] |
---|
2219 | n/a | [True, True, True, False, False] |
---|
2220 | n/a | |
---|
2221 | n/a | >>> quantify(range(99), lambda x: x%2==0) |
---|
2222 | n/a | 50 |
---|
2223 | n/a | |
---|
2224 | n/a | >>> a = [[1, 2, 3], [4, 5, 6]] |
---|
2225 | n/a | >>> flatten(a) |
---|
2226 | n/a | [1, 2, 3, 4, 5, 6] |
---|
2227 | n/a | |
---|
2228 | n/a | >>> list(repeatfunc(pow, 5, 2, 3)) |
---|
2229 | n/a | [8, 8, 8, 8, 8] |
---|
2230 | n/a | |
---|
2231 | n/a | >>> import random |
---|
2232 | n/a | >>> take(5, map(int, repeatfunc(random.random))) |
---|
2233 | n/a | [0, 0, 0, 0, 0] |
---|
2234 | n/a | |
---|
2235 | n/a | >>> list(pairwise('abcd')) |
---|
2236 | n/a | [('a', 'b'), ('b', 'c'), ('c', 'd')] |
---|
2237 | n/a | |
---|
2238 | n/a | >>> list(pairwise([])) |
---|
2239 | n/a | [] |
---|
2240 | n/a | |
---|
2241 | n/a | >>> list(pairwise('a')) |
---|
2242 | n/a | [] |
---|
2243 | n/a | |
---|
2244 | n/a | >>> list(islice(padnone('abc'), 0, 6)) |
---|
2245 | n/a | ['a', 'b', 'c', None, None, None] |
---|
2246 | n/a | |
---|
2247 | n/a | >>> list(ncycles('abc', 3)) |
---|
2248 | n/a | ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'] |
---|
2249 | n/a | |
---|
2250 | n/a | >>> dotproduct([1,2,3], [4,5,6]) |
---|
2251 | n/a | 32 |
---|
2252 | n/a | |
---|
2253 | n/a | >>> list(grouper(3, 'abcdefg', 'x')) |
---|
2254 | n/a | [('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')] |
---|
2255 | n/a | |
---|
2256 | n/a | >>> list(roundrobin('abc', 'd', 'ef')) |
---|
2257 | n/a | ['a', 'd', 'e', 'b', 'f', 'c'] |
---|
2258 | n/a | |
---|
2259 | n/a | >>> list(powerset([1,2,3])) |
---|
2260 | n/a | [(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)] |
---|
2261 | n/a | |
---|
2262 | n/a | >>> all(len(list(powerset(range(n)))) == 2**n for n in range(18)) |
---|
2263 | n/a | True |
---|
2264 | n/a | |
---|
2265 | n/a | >>> list(powerset('abcde')) == sorted(sorted(set(powerset('abcde'))), key=len) |
---|
2266 | n/a | True |
---|
2267 | n/a | |
---|
2268 | n/a | >>> list(unique_everseen('AAAABBBCCDAABBB')) |
---|
2269 | n/a | ['A', 'B', 'C', 'D'] |
---|
2270 | n/a | |
---|
2271 | n/a | >>> list(unique_everseen('ABBCcAD', str.lower)) |
---|
2272 | n/a | ['A', 'B', 'C', 'D'] |
---|
2273 | n/a | |
---|
2274 | n/a | >>> list(unique_justseen('AAAABBBCCDAABBB')) |
---|
2275 | n/a | ['A', 'B', 'C', 'D', 'A', 'B'] |
---|
2276 | n/a | |
---|
2277 | n/a | >>> list(unique_justseen('ABBCcAD', str.lower)) |
---|
2278 | n/a | ['A', 'B', 'C', 'A', 'D'] |
---|
2279 | n/a | |
---|
2280 | n/a | >>> first_true('ABC0DEF1', '9', str.isdigit) |
---|
2281 | n/a | '0' |
---|
2282 | n/a | |
---|
2283 | n/a | """ |
---|
2284 | n/a | |
---|
2285 | n/a | __test__ = {'libreftest' : libreftest} |
---|
2286 | n/a | |
---|
2287 | n/a | def test_main(verbose=None): |
---|
2288 | n/a | test_classes = (TestBasicOps, TestVariousIteratorArgs, TestGC, |
---|
2289 | n/a | RegressionTests, LengthTransparency, |
---|
2290 | n/a | SubclassWithKwargsTest, TestExamples, |
---|
2291 | n/a | SizeofTest) |
---|
2292 | n/a | support.run_unittest(*test_classes) |
---|
2293 | n/a | |
---|
2294 | n/a | # verify reference counting |
---|
2295 | n/a | if verbose and hasattr(sys, "gettotalrefcount"): |
---|
2296 | n/a | import gc |
---|
2297 | n/a | counts = [None] * 5 |
---|
2298 | n/a | for i in range(len(counts)): |
---|
2299 | n/a | support.run_unittest(*test_classes) |
---|
2300 | n/a | gc.collect() |
---|
2301 | n/a | counts[i] = sys.gettotalrefcount() |
---|
2302 | n/a | print(counts) |
---|
2303 | n/a | |
---|
2304 | n/a | # doctest the examples in the library reference |
---|
2305 | n/a | support.run_doctest(sys.modules[__name__], verbose) |
---|
2306 | n/a | |
---|
2307 | n/a | if __name__ == "__main__": |
---|
2308 | n/a | test_main(verbose=True) |
---|