Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_itertools.py @ 78069:a8aa77458e12 3.2
Add c_size_t and c_ssize_t to the ctypes summary table.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Thu, 12 Jul 2012 20:31:50 +0200 |
| parents | 50e9d4515ea2 |
| children | f7e14a1af609 b63eeb5a5c07 |
| rev | line source |
|---|---|
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1 import unittest |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
45773
diff
changeset
|
2 from test import support |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
3 from itertools import * |
|
33777
e4b7c923ed5f
Fix and test weak referencing of itertools.tee objects.
Raymond Hettinger <python@rcn.com>
parents:
33624
diff
changeset
|
4 from weakref import proxy |
|
51548
7d29c6e8ba05
Add an extra testcase.
Raymond Hettinger <python@rcn.com>
parents:
51542
diff
changeset
|
5 from decimal import Decimal |
| 51550 | 6 from fractions import Fraction |
|
27747
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27574
diff
changeset
|
7 import sys |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
8 import operator |
| 30764 | 9 import random |
|
57525
0a6137944e61
Issue #7410: deepcopy of itertools.count was resetting the count.
Raymond Hettinger <python@rcn.com>
parents:
57042
diff
changeset
|
10 import copy |
|
0a6137944e61
Issue #7410: deepcopy of itertools.count was resetting the count.
Raymond Hettinger <python@rcn.com>
parents:
57042
diff
changeset
|
11 import pickle |
|
45435
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
12 from functools import reduce |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
45773
diff
changeset
|
13 maxsize = support.MAX_Py_ssize_t |
|
41716
00c02c4488ce
Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
14 minsize = -maxsize-1 |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
15 |
|
39656
08005ba6ceb9
Make built-in zip() equal to itertools.izip().
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
16 def lzip(*args): |
|
08005ba6ceb9
Make built-in zip() equal to itertools.izip().
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
17 return list(zip(*args)) |
|
08005ba6ceb9
Make built-in zip() equal to itertools.izip().
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
18 |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
19 def onearg(x): |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
20 'Test function of one argument' |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
21 return 2*x |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
22 |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
23 def errfunc(*args): |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
24 'Test function that raises an error' |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
25 raise ValueError |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
26 |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
27 def gen3(): |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
28 'Non-restartable source sequence' |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
29 for i in (0, 1, 2): |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
30 yield i |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
31 |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
32 def isEven(x): |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
33 'Test predicate' |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
34 return x%2==0 |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
35 |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
36 def isOdd(x): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
37 'Test predicate' |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
38 return x%2==1 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
39 |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
40 class StopNow: |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
41 'Class emulating an empty iterable.' |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
42 def __iter__(self): |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
43 return self |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
44 def __next__(self): |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
45 raise StopIteration |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
46 |
| 29690 | 47 def take(n, seq): |
| 48 'Convenience function for partially consuming a long of infinite iterable' | |
| 49 return list(islice(seq, n)) | |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
50 |
|
45659
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
51 def prod(iterable): |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
52 return reduce(operator.mul, iterable, 1) |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
53 |
|
45548
8c3c5b4eef55
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45534
diff
changeset
|
54 def fact(n): |
|
8c3c5b4eef55
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45534
diff
changeset
|
55 'Factorial' |
|
45659
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
56 return prod(range(1, n+1)) |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
57 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
58 class TestBasicOps(unittest.TestCase): |
|
66589
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
59 |
|
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
60 def test_accumulate(self): |
|
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
61 self.assertEqual(list(accumulate(range(10))), # one positional arg |
|
66619
1098fab85e47
Simplify the signature for itertools.accumulate() to match numpy. Handle one item iterable the same way as min()/max().
Raymond Hettinger <python@rcn.com>
parents:
66589
diff
changeset
|
62 [0, 1, 3, 6, 10, 15, 21, 28, 36, 45]) |
|
1098fab85e47
Simplify the signature for itertools.accumulate() to match numpy. Handle one item iterable the same way as min()/max().
Raymond Hettinger <python@rcn.com>
parents:
66589
diff
changeset
|
63 self.assertEqual(list(accumulate(iterable=range(10))), # kw arg |
|
1098fab85e47
Simplify the signature for itertools.accumulate() to match numpy. Handle one item iterable the same way as min()/max().
Raymond Hettinger <python@rcn.com>
parents:
66589
diff
changeset
|
64 [0, 1, 3, 6, 10, 15, 21, 28, 36, 45]) |
|
66589
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
65 for typ in int, complex, Decimal, Fraction: # multiple types |
|
66619
1098fab85e47
Simplify the signature for itertools.accumulate() to match numpy. Handle one item iterable the same way as min()/max().
Raymond Hettinger <python@rcn.com>
parents:
66589
diff
changeset
|
66 self.assertEqual( |
|
1098fab85e47
Simplify the signature for itertools.accumulate() to match numpy. Handle one item iterable the same way as min()/max().
Raymond Hettinger <python@rcn.com>
parents:
66589
diff
changeset
|
67 list(accumulate(map(typ, range(10)))), |
|
66589
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
68 list(map(typ, [0, 1, 3, 6, 10, 15, 21, 28, 36, 45]))) |
|
66621
64c642bb7ff7
Update the itertools.accumulate() docs.
Raymond Hettinger <python@rcn.com>
parents:
66619
diff
changeset
|
69 self.assertEqual(list(accumulate('abc')), ['a', 'ab', 'abc']) # works with non-numeric |
|
66589
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
70 self.assertEqual(list(accumulate([])), []) # empty iterable |
|
66619
1098fab85e47
Simplify the signature for itertools.accumulate() to match numpy. Handle one item iterable the same way as min()/max().
Raymond Hettinger <python@rcn.com>
parents:
66589
diff
changeset
|
71 self.assertEqual(list(accumulate([7])), [7]) # iterable of length one |
|
1098fab85e47
Simplify the signature for itertools.accumulate() to match numpy. Handle one item iterable the same way as min()/max().
Raymond Hettinger <python@rcn.com>
parents:
66589
diff
changeset
|
72 self.assertRaises(TypeError, accumulate, range(10), 5) # too many args |
|
66589
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
73 self.assertRaises(TypeError, accumulate) # too few args |
|
66619
1098fab85e47
Simplify the signature for itertools.accumulate() to match numpy. Handle one item iterable the same way as min()/max().
Raymond Hettinger <python@rcn.com>
parents:
66589
diff
changeset
|
74 self.assertRaises(TypeError, accumulate, x=range(10)) # unexpected kwd arg |
|
66589
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
75 self.assertRaises(TypeError, list, accumulate([1, []])) # args that don't add |
|
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
76 |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27769
diff
changeset
|
77 def test_chain(self): |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
78 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
79 def chain2(*iterables): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
80 'Pure python version in the docs' |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
81 for it in iterables: |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
82 for element in it: |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
83 yield element |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
84 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
85 for c in (chain, chain2): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
86 self.assertEqual(list(c('abc', 'def')), list('abcdef')) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
87 self.assertEqual(list(c('abc')), list('abc')) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
88 self.assertEqual(list(c('')), []) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
89 self.assertEqual(take(4, c('abc', 'def')), list('abcd')) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
90 self.assertRaises(TypeError, list,c(2, 3)) |
|
45569
937a3c5af691
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61104,61110-61112,61114-61115,61117-61125 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45548
diff
changeset
|
91 |
|
937a3c5af691
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61104,61110-61112,61114-61115,61117-61125 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45548
diff
changeset
|
92 def test_chain_from_iterable(self): |
|
937a3c5af691
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61104,61110-61112,61114-61115,61117-61125 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45548
diff
changeset
|
93 self.assertEqual(list(chain.from_iterable(['abc', 'def'])), list('abcdef')) |
|
937a3c5af691
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61104,61110-61112,61114-61115,61117-61125 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45548
diff
changeset
|
94 self.assertEqual(list(chain.from_iterable(['abc'])), list('abc')) |
|
937a3c5af691
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61104,61110-61112,61114-61115,61117-61125 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45548
diff
changeset
|
95 self.assertEqual(list(chain.from_iterable([''])), []) |
|
937a3c5af691
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61104,61110-61112,61114-61115,61117-61125 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45548
diff
changeset
|
96 self.assertEqual(take(4, chain.from_iterable(['abc', 'def'])), list('abcd')) |
|
937a3c5af691
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61104,61110-61112,61114-61115,61117-61125 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45548
diff
changeset
|
97 self.assertRaises(TypeError, list, chain.from_iterable([2, 3])) |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27769
diff
changeset
|
98 |
|
45548
8c3c5b4eef55
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45534
diff
changeset
|
99 def test_combinations(self): |
|
50635
fffd10c12a1b
Backport r68409 fixing itertools.permutations() and combinations().
Raymond Hettinger <python@rcn.com>
parents:
50468
diff
changeset
|
100 self.assertRaises(TypeError, combinations, 'abc') # missing r argument |
|
45548
8c3c5b4eef55
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45534
diff
changeset
|
101 self.assertRaises(TypeError, combinations, 'abc', 2, 1) # too many arguments |
|
45659
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
102 self.assertRaises(TypeError, combinations, None) # pool is not iterable |
|
45548
8c3c5b4eef55
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45534
diff
changeset
|
103 self.assertRaises(ValueError, combinations, 'abc', -2) # r is negative |
|
50635
fffd10c12a1b
Backport r68409 fixing itertools.permutations() and combinations().
Raymond Hettinger <python@rcn.com>
parents:
50468
diff
changeset
|
104 self.assertEqual(list(combinations('abc', 32)), []) # r > n |
|
45548
8c3c5b4eef55
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45534
diff
changeset
|
105 self.assertEqual(list(combinations(range(4), 3)), |
|
8c3c5b4eef55
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45534
diff
changeset
|
106 [(0,1,2), (0,1,3), (0,2,3), (1,2,3)]) |
|
45659
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
107 |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
108 def combinations1(iterable, r): |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
109 'Pure python version shown in the docs' |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
110 pool = tuple(iterable) |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
111 n = len(pool) |
|
50635
fffd10c12a1b
Backport r68409 fixing itertools.permutations() and combinations().
Raymond Hettinger <python@rcn.com>
parents:
50468
diff
changeset
|
112 if r > n: |
|
fffd10c12a1b
Backport r68409 fixing itertools.permutations() and combinations().
Raymond Hettinger <python@rcn.com>
parents:
50468
diff
changeset
|
113 return |
|
45659
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
114 indices = list(range(r)) |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
115 yield tuple(pool[i] for i in indices) |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
116 while 1: |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
117 for i in reversed(range(r)): |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
118 if indices[i] != i + n - r: |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
119 break |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
120 else: |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
121 return |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
122 indices[i] += 1 |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
123 for j in range(i+1, r): |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
124 indices[j] = indices[j-1] + 1 |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
125 yield tuple(pool[i] for i in indices) |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
126 |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
127 def combinations2(iterable, r): |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
128 'Pure python version shown in the docs' |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
129 pool = tuple(iterable) |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
130 n = len(pool) |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
131 for indices in permutations(range(n), r): |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
132 if sorted(indices) == list(indices): |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
133 yield tuple(pool[i] for i in indices) |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
134 |
|
51107
14631c627e03
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51103
diff
changeset
|
135 def combinations3(iterable, r): |
|
14631c627e03
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51103
diff
changeset
|
136 'Pure python version from cwr()' |
|
14631c627e03
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51103
diff
changeset
|
137 pool = tuple(iterable) |
|
14631c627e03
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51103
diff
changeset
|
138 n = len(pool) |
|
14631c627e03
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51103
diff
changeset
|
139 for indices in combinations_with_replacement(range(n), r): |
|
14631c627e03
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51103
diff
changeset
|
140 if len(set(indices)) == r: |
|
14631c627e03
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51103
diff
changeset
|
141 yield tuple(pool[i] for i in indices) |
|
14631c627e03
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51103
diff
changeset
|
142 |
|
45659
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
143 for n in range(7): |
|
45548
8c3c5b4eef55
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45534
diff
changeset
|
144 values = [5*x-12 for x in range(n)] |
|
50635
fffd10c12a1b
Backport r68409 fixing itertools.permutations() and combinations().
Raymond Hettinger <python@rcn.com>
parents:
50468
diff
changeset
|
145 for r in range(n+2): |
|
45548
8c3c5b4eef55
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45534
diff
changeset
|
146 result = list(combinations(values, r)) |
|
50635
fffd10c12a1b
Backport r68409 fixing itertools.permutations() and combinations().
Raymond Hettinger <python@rcn.com>
parents:
50468
diff
changeset
|
147 self.assertEqual(len(result), 0 if r>n else fact(n) / fact(r) / fact(n-r)) # right number of combs |
|
45548
8c3c5b4eef55
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45534
diff
changeset
|
148 self.assertEqual(len(result), len(set(result))) # no repeats |
|
8c3c5b4eef55
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45534
diff
changeset
|
149 self.assertEqual(result, sorted(result)) # lexicographic order |
|
8c3c5b4eef55
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45534
diff
changeset
|
150 for c in result: |
|
8c3c5b4eef55
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45534
diff
changeset
|
151 self.assertEqual(len(c), r) # r-length combinations |
|
8c3c5b4eef55
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45534
diff
changeset
|
152 self.assertEqual(len(set(c)), r) # no duplicate elements |
|
8c3c5b4eef55
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45534
diff
changeset
|
153 self.assertEqual(list(c), sorted(c)) # keep original ordering |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51778
diff
changeset
|
154 self.assertTrue(all(e in values for e in c)) # elements taken from input iterable |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
155 self.assertEqual(list(c), |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
156 [e for e in values if e in c]) # comb is a subsequence of the input iterable |
|
45659
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
157 self.assertEqual(result, list(combinations1(values, r))) # matches first pure python version |
|
50635
fffd10c12a1b
Backport r68409 fixing itertools.permutations() and combinations().
Raymond Hettinger <python@rcn.com>
parents:
50468
diff
changeset
|
158 self.assertEqual(result, list(combinations2(values, r))) # matches second pure python version |
|
51107
14631c627e03
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51103
diff
changeset
|
159 self.assertEqual(result, list(combinations3(values, r))) # matches second pure python version |
|
45659
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
160 |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
161 # Test implementation detail: tuple re-use |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
162 self.assertEqual(len(set(map(id, combinations('abcde', 3)))), 1) |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
163 self.assertNotEqual(len(set(map(id, list(combinations('abcde', 3))))), 1) |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
164 |
|
51100
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
165 def test_combinations_with_replacement(self): |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
166 cwr = combinations_with_replacement |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
167 self.assertRaises(TypeError, cwr, 'abc') # missing r argument |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
168 self.assertRaises(TypeError, cwr, 'abc', 2, 1) # too many arguments |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
169 self.assertRaises(TypeError, cwr, None) # pool is not iterable |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
170 self.assertRaises(ValueError, cwr, 'abc', -2) # r is negative |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
171 self.assertEqual(list(cwr('ABC', 2)), |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
172 [('A','A'), ('A','B'), ('A','C'), ('B','B'), ('B','C'), ('C','C')]) |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
173 |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
174 def cwr1(iterable, r): |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
175 'Pure python version shown in the docs' |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
176 # number items returned: (n+r-1)! / r! / (n-1)! when n>0 |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
177 pool = tuple(iterable) |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
178 n = len(pool) |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
179 if not n and r: |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
180 return |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
181 indices = [0] * r |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
182 yield tuple(pool[i] for i in indices) |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
183 while 1: |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
184 for i in reversed(range(r)): |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
185 if indices[i] != n - 1: |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
186 break |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
187 else: |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
188 return |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
189 indices[i:] = [indices[i] + 1] * (r - i) |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
190 yield tuple(pool[i] for i in indices) |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
191 |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
192 def cwr2(iterable, r): |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
193 'Pure python version shown in the docs' |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
194 pool = tuple(iterable) |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
195 n = len(pool) |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
196 for indices in product(range(n), repeat=r): |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
197 if sorted(indices) == list(indices): |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
198 yield tuple(pool[i] for i in indices) |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
199 |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
200 def numcombs(n, r): |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
201 if not n: |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
202 return 0 if r else 1 |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
203 return fact(n+r-1) / fact(r)/ fact(n-1) |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
204 |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
205 for n in range(7): |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
206 values = [5*x-12 for x in range(n)] |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
207 for r in range(n+2): |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
208 result = list(cwr(values, r)) |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
209 |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
210 self.assertEqual(len(result), numcombs(n, r)) # right number of combs |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
211 self.assertEqual(len(result), len(set(result))) # no repeats |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
212 self.assertEqual(result, sorted(result)) # lexicographic order |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
213 |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
214 regular_combs = list(combinations(values, r)) # compare to combs without replacement |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
215 if n == 0 or r <= 1: |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57525
diff
changeset
|
216 self.assertEqual(result, regular_combs) # cases that should be identical |
|
51100
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
217 else: |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51778
diff
changeset
|
218 self.assertTrue(set(result) >= set(regular_combs)) # rest should be supersets of regular combs |
|
51100
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
219 |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
220 for c in result: |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
221 self.assertEqual(len(c), r) # r-length combinations |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
222 noruns = [k for k,v in groupby(c)] # combo without consecutive repeats |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
223 self.assertEqual(len(noruns), len(set(noruns))) # no repeats other than consecutive |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
224 self.assertEqual(list(c), sorted(c)) # keep original ordering |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51778
diff
changeset
|
225 self.assertTrue(all(e in values for e in c)) # elements taken from input iterable |
|
51100
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
226 self.assertEqual(noruns, |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
227 [e for e in values if e in c]) # comb is a subsequence of the input iterable |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
228 self.assertEqual(result, list(cwr1(values, r))) # matches first pure python version |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
229 self.assertEqual(result, list(cwr2(values, r))) # matches second pure python version |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
230 |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
231 # Test implementation detail: tuple re-use |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
232 self.assertEqual(len(set(map(id, cwr('abcde', 3)))), 1) |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
233 self.assertNotEqual(len(set(map(id, list(cwr('abcde', 3))))), 1) |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
234 |
|
45659
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
235 def test_permutations(self): |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
236 self.assertRaises(TypeError, permutations) # too few arguments |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
237 self.assertRaises(TypeError, permutations, 'abc', 2, 1) # too many arguments |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
238 self.assertRaises(TypeError, permutations, None) # pool is not iterable |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
239 self.assertRaises(ValueError, permutations, 'abc', -2) # r is negative |
|
50635
fffd10c12a1b
Backport r68409 fixing itertools.permutations() and combinations().
Raymond Hettinger <python@rcn.com>
parents:
50468
diff
changeset
|
240 self.assertEqual(list(permutations('abc', 32)), []) # r > n |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
241 self.assertRaises(TypeError, permutations, 'abc', 's') # r is not an int or None |
|
45659
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
242 self.assertEqual(list(permutations(range(3), 2)), |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
243 [(0,1), (0,2), (1,0), (1,2), (2,0), (2,1)]) |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
244 |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
245 def permutations1(iterable, r=None): |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
246 'Pure python version shown in the docs' |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
247 pool = tuple(iterable) |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
248 n = len(pool) |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
249 r = n if r is None else r |
|
50635
fffd10c12a1b
Backport r68409 fixing itertools.permutations() and combinations().
Raymond Hettinger <python@rcn.com>
parents:
50468
diff
changeset
|
250 if r > n: |
|
fffd10c12a1b
Backport r68409 fixing itertools.permutations() and combinations().
Raymond Hettinger <python@rcn.com>
parents:
50468
diff
changeset
|
251 return |
|
45659
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
252 indices = list(range(n)) |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
253 cycles = list(range(n-r+1, n+1))[::-1] |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
254 yield tuple(pool[i] for i in indices[:r]) |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
255 while n: |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
256 for i in reversed(range(r)): |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
257 cycles[i] -= 1 |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
258 if cycles[i] == 0: |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
259 indices[i:] = indices[i+1:] + indices[i:i+1] |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
260 cycles[i] = n - i |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
261 else: |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
262 j = cycles[i] |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
263 indices[i], indices[-j] = indices[-j], indices[i] |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
264 yield tuple(pool[i] for i in indices[:r]) |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
265 break |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
266 else: |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
267 return |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
268 |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
269 def permutations2(iterable, r=None): |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
270 'Pure python version shown in the docs' |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
271 pool = tuple(iterable) |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
272 n = len(pool) |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
273 r = n if r is None else r |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
274 for indices in product(range(n), repeat=r): |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
275 if len(set(indices)) == r: |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
276 yield tuple(pool[i] for i in indices) |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
277 |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
278 for n in range(7): |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
279 values = [5*x-12 for x in range(n)] |
|
50635
fffd10c12a1b
Backport r68409 fixing itertools.permutations() and combinations().
Raymond Hettinger <python@rcn.com>
parents:
50468
diff
changeset
|
280 for r in range(n+2): |
|
45659
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
281 result = list(permutations(values, r)) |
|
50635
fffd10c12a1b
Backport r68409 fixing itertools.permutations() and combinations().
Raymond Hettinger <python@rcn.com>
parents:
50468
diff
changeset
|
282 self.assertEqual(len(result), 0 if r>n else fact(n) / fact(n-r)) # right number of perms |
|
45659
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
283 self.assertEqual(len(result), len(set(result))) # no repeats |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
284 self.assertEqual(result, sorted(result)) # lexicographic order |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
285 for p in result: |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
286 self.assertEqual(len(p), r) # r-length permutations |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
287 self.assertEqual(len(set(p)), r) # no duplicate elements |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51778
diff
changeset
|
288 self.assertTrue(all(e in values for e in p)) # elements taken from input iterable |
|
45659
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
289 self.assertEqual(result, list(permutations1(values, r))) # matches first pure python version |
|
50635
fffd10c12a1b
Backport r68409 fixing itertools.permutations() and combinations().
Raymond Hettinger <python@rcn.com>
parents:
50468
diff
changeset
|
290 self.assertEqual(result, list(permutations2(values, r))) # matches second pure python version |
|
45659
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
291 if r == n: |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
292 self.assertEqual(result, list(permutations(values, None))) # test r as None |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
293 self.assertEqual(result, list(permutations(values))) # test default r |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
294 |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
295 # Test implementation detail: tuple re-use |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
296 self.assertEqual(len(set(map(id, permutations('abcde', 3)))), 1) |
|
45659
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
297 self.assertNotEqual(len(set(map(id, list(permutations('abcde', 3))))), 1) |
|
45548
8c3c5b4eef55
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45534
diff
changeset
|
298 |
|
51107
14631c627e03
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51103
diff
changeset
|
299 def test_combinatorics(self): |
|
14631c627e03
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51103
diff
changeset
|
300 # Test relationships between product(), permutations(), |
|
14631c627e03
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51103
diff
changeset
|
301 # combinations() and combinations_with_replacement(). |
|
14631c627e03
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51103
diff
changeset
|
302 |
|
51115
53244c4eef22
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51109
diff
changeset
|
303 for n in range(6): |
|
53244c4eef22
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51109
diff
changeset
|
304 s = 'ABCDEFG'[:n] |
|
53244c4eef22
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51109
diff
changeset
|
305 for r in range(8): |
|
53244c4eef22
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51109
diff
changeset
|
306 prod = list(product(s, repeat=r)) |
|
53244c4eef22
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51109
diff
changeset
|
307 cwr = list(combinations_with_replacement(s, r)) |
|
53244c4eef22
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51109
diff
changeset
|
308 perm = list(permutations(s, r)) |
|
53244c4eef22
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51109
diff
changeset
|
309 comb = list(combinations(s, r)) |
|
53244c4eef22
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51109
diff
changeset
|
310 |
|
53244c4eef22
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51109
diff
changeset
|
311 # Check size |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57525
diff
changeset
|
312 self.assertEqual(len(prod), n**r) |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57525
diff
changeset
|
313 self.assertEqual(len(cwr), (fact(n+r-1) / fact(r)/ fact(n-1)) if n else (not r)) |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57525
diff
changeset
|
314 self.assertEqual(len(perm), 0 if r>n else fact(n) / fact(n-r)) |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57525
diff
changeset
|
315 self.assertEqual(len(comb), 0 if r>n else fact(n) / fact(r) / fact(n-r)) |
|
51107
14631c627e03
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51103
diff
changeset
|
316 |
|
51115
53244c4eef22
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51109
diff
changeset
|
317 # Check lexicographic order without repeated tuples |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57525
diff
changeset
|
318 self.assertEqual(prod, sorted(set(prod))) |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57525
diff
changeset
|
319 self.assertEqual(cwr, sorted(set(cwr))) |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57525
diff
changeset
|
320 self.assertEqual(perm, sorted(set(perm))) |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57525
diff
changeset
|
321 self.assertEqual(comb, sorted(set(comb))) |
|
51115
53244c4eef22
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51109
diff
changeset
|
322 |
|
53244c4eef22
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51109
diff
changeset
|
323 # Check interrelationships |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57525
diff
changeset
|
324 self.assertEqual(cwr, [t for t in prod if sorted(t)==list(t)]) # cwr: prods which are sorted |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57525
diff
changeset
|
325 self.assertEqual(perm, [t for t in prod if len(set(t))==r]) # perm: prods with no dups |
|
51115
53244c4eef22
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51109
diff
changeset
|
326 self.assertEqual(comb, [t for t in perm if sorted(t)==list(t)]) # comb: perms that are sorted |
|
53244c4eef22
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51109
diff
changeset
|
327 self.assertEqual(comb, [t for t in cwr if len(set(t))==r]) # comb: cwrs without dups |
|
53244c4eef22
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51109
diff
changeset
|
328 self.assertEqual(comb, list(filter(set(cwr).__contains__, perm))) # comb: perm that is a cwr |
|
53244c4eef22
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51109
diff
changeset
|
329 self.assertEqual(comb, list(filter(set(perm).__contains__, cwr))) # comb: cwr that is a perm |
|
53244c4eef22
More exhaustive combinatoric checks.
Raymond Hettinger <python@rcn.com>
parents:
51109
diff
changeset
|
330 self.assertEqual(comb, sorted(set(cwr) & set(perm))) # comb: both a cwr and a perm |
|
51107
14631c627e03
Add tests to verify combinatoric relationships.
Raymond Hettinger <python@rcn.com>
parents:
51103
diff
changeset
|
331 |
|
51063
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
332 def test_compress(self): |
|
51717
e260d6daf784
Add keyword arg support to itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51661
diff
changeset
|
333 self.assertEqual(list(compress(data='ABCDEF', selectors=[1,0,1,0,1,1])), list('ACEF')) |
|
51063
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
334 self.assertEqual(list(compress('ABCDEF', [1,0,1,0,1,1])), list('ACEF')) |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
335 self.assertEqual(list(compress('ABCDEF', [0,0,0,0,0,0])), list('')) |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
336 self.assertEqual(list(compress('ABCDEF', [1,1,1,1,1,1])), list('ABCDEF')) |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
337 self.assertEqual(list(compress('ABCDEF', [1,0,1])), list('AC')) |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
338 self.assertEqual(list(compress('ABC', [0,1,1,1,1,1])), list('BC')) |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
339 n = 10000 |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
340 data = chain.from_iterable(repeat(range(6), n)) |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
341 selectors = chain.from_iterable(repeat((0, 1))) |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
342 self.assertEqual(list(compress(data, selectors)), [1,3,5] * n) |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
343 self.assertRaises(TypeError, compress, None, range(6)) # 1st arg not iterable |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
344 self.assertRaises(TypeError, compress, range(6), None) # 2nd arg not iterable |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
345 self.assertRaises(TypeError, compress, range(6)) # too few args |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
346 self.assertRaises(TypeError, compress, range(6), None) # too many args |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
347 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
348 def test_count(self): |
|
39656
08005ba6ceb9
Make built-in zip() equal to itertools.izip().
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
349 self.assertEqual(lzip('abc',count()), [('a', 0), ('b', 1), ('c', 2)]) |
|
08005ba6ceb9
Make built-in zip() equal to itertools.izip().
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
350 self.assertEqual(lzip('abc',count(3)), [('a', 3), ('b', 4), ('c', 5)]) |
|
08005ba6ceb9
Make built-in zip() equal to itertools.izip().
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
351 self.assertEqual(take(2, lzip('abc',count(3))), [('a', 3), ('b', 4)]) |
|
43734
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
352 self.assertEqual(take(2, zip('abc',count(-1))), [('a', -1), ('b', 0)]) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
353 self.assertEqual(take(2, zip('abc',count(-3))), [('a', -3), ('b', -2)]) |
|
51542
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
354 self.assertRaises(TypeError, count, 2, 3, 4) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
355 self.assertRaises(TypeError, count, 'a') |
|
43734
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
356 self.assertEqual(list(islice(count(maxsize-5), 10)), |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
357 list(range(maxsize-5, maxsize+5))) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
358 self.assertEqual(list(islice(count(-maxsize-5), 10)), |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
359 list(range(-maxsize-5, -maxsize+5))) |
|
69062
9a6da9bc257d
Bugfix: Properly test for errors from PyLong_AsLong() in itertools.cycle.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
68585
diff
changeset
|
360 self.assertEqual(list(islice(count(10, maxsize+5), 3)), |
|
9a6da9bc257d
Bugfix: Properly test for errors from PyLong_AsLong() in itertools.cycle.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
68585
diff
changeset
|
361 list(range(10, 10+3*(maxsize+5), maxsize+5))) |
|
33616
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
362 c = count(3) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
363 self.assertEqual(repr(c), 'count(3)') |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
364 next(c) |
|
33616
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
365 self.assertEqual(repr(c), 'count(4)') |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39656
diff
changeset
|
366 c = count(-9) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39656
diff
changeset
|
367 self.assertEqual(repr(c), 'count(-9)') |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
368 next(c) |
|
51542
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
369 self.assertEqual(repr(count(10.25)), 'count(10.25)') |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
370 self.assertEqual(next(c), -8) |
|
44160
9d152530910a
Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents:
43734
diff
changeset
|
371 for i in (-sys.maxsize-5, -sys.maxsize+5 ,-10, -1, 0, 10, sys.maxsize-5, sys.maxsize+5): |
|
43734
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
372 # Test repr (ignoring the L in longs) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
373 r1 = repr(count(i)).replace('L', '') |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
374 r2 = 'count(%r)'.__mod__(i).replace('L', '') |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
375 self.assertEqual(r1, r2) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
376 |
|
57525
0a6137944e61
Issue #7410: deepcopy of itertools.count was resetting the count.
Raymond Hettinger <python@rcn.com>
parents:
57042
diff
changeset
|
377 # check copy, deepcopy, pickle |
|
0a6137944e61
Issue #7410: deepcopy of itertools.count was resetting the count.
Raymond Hettinger <python@rcn.com>
parents:
57042
diff
changeset
|
378 for value in -3, 3, maxsize-5, maxsize+5: |
|
0a6137944e61
Issue #7410: deepcopy of itertools.count was resetting the count.
Raymond Hettinger <python@rcn.com>
parents:
57042
diff
changeset
|
379 c = count(value) |
|
0a6137944e61
Issue #7410: deepcopy of itertools.count was resetting the count.
Raymond Hettinger <python@rcn.com>
parents:
57042
diff
changeset
|
380 self.assertEqual(next(copy.copy(c)), value) |
|
0a6137944e61
Issue #7410: deepcopy of itertools.count was resetting the count.
Raymond Hettinger <python@rcn.com>
parents:
57042
diff
changeset
|
381 self.assertEqual(next(copy.deepcopy(c)), value) |
|
0a6137944e61
Issue #7410: deepcopy of itertools.count was resetting the count.
Raymond Hettinger <python@rcn.com>
parents:
57042
diff
changeset
|
382 self.assertEqual(next(pickle.loads(pickle.dumps(c))), value) |
|
0a6137944e61
Issue #7410: deepcopy of itertools.count was resetting the count.
Raymond Hettinger <python@rcn.com>
parents:
57042
diff
changeset
|
383 |
|
69062
9a6da9bc257d
Bugfix: Properly test for errors from PyLong_AsLong() in itertools.cycle.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
68585
diff
changeset
|
384 #check proper internal error handling for large "step' sizes |
|
9a6da9bc257d
Bugfix: Properly test for errors from PyLong_AsLong() in itertools.cycle.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
68585
diff
changeset
|
385 count(1, maxsize+5); sys.exc_info() |
|
9a6da9bc257d
Bugfix: Properly test for errors from PyLong_AsLong() in itertools.cycle.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
68585
diff
changeset
|
386 |
|
51542
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
387 def test_count_with_stride(self): |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
388 self.assertEqual(lzip('abc',count(2,3)), [('a', 2), ('b', 5), ('c', 8)]) |
|
51778
aa29d0967105
Port r69837: Fix keyword arguments for itertools.count(). Step arg without a start arg was ignored.
Raymond Hettinger <python@rcn.com>
parents:
51722
diff
changeset
|
389 self.assertEqual(lzip('abc',count(start=2,step=3)), |
|
aa29d0967105
Port r69837: Fix keyword arguments for itertools.count(). Step arg without a start arg was ignored.
Raymond Hettinger <python@rcn.com>
parents:
51722
diff
changeset
|
390 [('a', 2), ('b', 5), ('c', 8)]) |
|
aa29d0967105
Port r69837: Fix keyword arguments for itertools.count(). Step arg without a start arg was ignored.
Raymond Hettinger <python@rcn.com>
parents:
51722
diff
changeset
|
391 self.assertEqual(lzip('abc',count(step=-1)), |
|
aa29d0967105
Port r69837: Fix keyword arguments for itertools.count(). Step arg without a start arg was ignored.
Raymond Hettinger <python@rcn.com>
parents:
51722
diff
changeset
|
392 [('a', 0), ('b', -1), ('c', -2)]) |
|
51542
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
393 self.assertEqual(lzip('abc',count(2,0)), [('a', 2), ('b', 2), ('c', 2)]) |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
394 self.assertEqual(lzip('abc',count(2,1)), [('a', 2), ('b', 3), ('c', 4)]) |
|
51624
2a263d66d659
Add keyword argument support to itertools.count().
Raymond Hettinger <python@rcn.com>
parents:
51550
diff
changeset
|
395 self.assertEqual(lzip('abc',count(2,3)), [('a', 2), ('b', 5), ('c', 8)]) |
|
51542
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
396 self.assertEqual(take(20, count(maxsize-15, 3)), take(20, range(maxsize-15, maxsize+100, 3))) |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
397 self.assertEqual(take(20, count(-maxsize-15, 3)), take(20, range(-maxsize-15,-maxsize+100, 3))) |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
398 self.assertEqual(take(3, count(2, 3.25-4j)), [2, 5.25-4j, 8.5-8j]) |
|
51548
7d29c6e8ba05
Add an extra testcase.
Raymond Hettinger <python@rcn.com>
parents:
51542
diff
changeset
|
399 self.assertEqual(take(3, count(Decimal('1.1'), Decimal('.1'))), |
|
7d29c6e8ba05
Add an extra testcase.
Raymond Hettinger <python@rcn.com>
parents:
51542
diff
changeset
|
400 [Decimal('1.1'), Decimal('1.2'), Decimal('1.3')]) |
| 51550 | 401 self.assertEqual(take(3, count(Fraction(2,3), Fraction(1,7))), |
| 402 [Fraction(2,3), Fraction(17,21), Fraction(20,21)]) | |
|
51542
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
403 self.assertEqual(repr(take(3, count(10, 2.5))), repr([10, 12.5, 15.0])) |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
404 c = count(3, 5) |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
405 self.assertEqual(repr(c), 'count(3, 5)') |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
406 next(c) |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
407 self.assertEqual(repr(c), 'count(8, 5)') |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
408 c = count(-9, 0) |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
409 self.assertEqual(repr(c), 'count(-9, 0)') |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
410 next(c) |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
411 self.assertEqual(repr(c), 'count(-9, 0)') |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
412 c = count(-9, -3) |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
413 self.assertEqual(repr(c), 'count(-9, -3)') |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
414 next(c) |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
415 self.assertEqual(repr(c), 'count(-12, -3)') |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
416 self.assertEqual(repr(c), 'count(-12, -3)') |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
417 self.assertEqual(repr(count(10.5, 1.25)), 'count(10.5, 1.25)') |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
418 self.assertEqual(repr(count(10.5, 1)), 'count(10.5)') # suppress step=1 when it's an int |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
419 self.assertEqual(repr(count(10.5, 1.00)), 'count(10.5, 1.0)') # do show float values lilke 1.0 |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
420 for i in (-sys.maxsize-5, -sys.maxsize+5 ,-10, -1, 0, 10, sys.maxsize-5, sys.maxsize+5): |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
421 for j in (-sys.maxsize-5, -sys.maxsize+5 ,-10, -1, 0, 1, 10, sys.maxsize-5, sys.maxsize+5): |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
422 # Test repr (ignoring the L in longs) |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
423 r1 = repr(count(i, j)).replace('L', '') |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
424 if j == 1: |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
425 r2 = ('count(%r)' % i).replace('L', '') |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
426 else: |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
427 r2 = ('count(%r, %r)' % (i, j)).replace('L', '') |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
428 self.assertEqual(r1, r2) |
|
b0db109089c9
Issue 5032: added a step argument to itertools.count() and allowed non-integer arguments.
Raymond Hettinger <python@rcn.com>
parents:
51320
diff
changeset
|
429 |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27769
diff
changeset
|
430 def test_cycle(self): |
| 29690 | 431 self.assertEqual(take(10, cycle('abc')), list('abcabcabca')) |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27769
diff
changeset
|
432 self.assertEqual(list(cycle('')), []) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
433 self.assertRaises(TypeError, cycle) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
434 self.assertRaises(TypeError, cycle, 5) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
435 self.assertEqual(list(islice(cycle(gen3()),10)), [0,1,2,0,1,2,0,1,2,0]) |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27769
diff
changeset
|
436 |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
437 def test_groupby(self): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
438 # Check whether it accepts arguments correctly |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
439 self.assertEqual([], list(groupby([]))) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
440 self.assertEqual([], list(groupby([], key=id))) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
441 self.assertRaises(TypeError, list, groupby('abc', [])) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
442 self.assertRaises(TypeError, groupby, None) |
|
33616
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
443 self.assertRaises(TypeError, groupby, 'abc', lambda x:x, 10) |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
444 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
445 # Check normal input |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
446 s = [(0, 10, 20), (0, 11,21), (0,12,21), (1,13,21), (1,14,22), |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
447 (2,15,22), (3,16,23), (3,17,23)] |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
448 dup = [] |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
449 for k, g in groupby(s, lambda r:r[0]): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
450 for elem in g: |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
451 self.assertEqual(k, elem[0]) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
452 dup.append(elem) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
453 self.assertEqual(s, dup) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
454 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
455 # Check nested case |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
456 dup = [] |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
457 for k, g in groupby(s, lambda r:r[0]): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
458 for ik, ig in groupby(g, lambda r:r[2]): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
459 for elem in ig: |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
460 self.assertEqual(k, elem[0]) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
461 self.assertEqual(ik, elem[2]) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
462 dup.append(elem) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
463 self.assertEqual(s, dup) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
464 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
465 # Check case where inner iterator is not used |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
466 keys = [k for k, g in groupby(s, lambda r:r[0])] |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
467 expectedkeys = set([r[0] for r in s]) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
468 self.assertEqual(set(keys), expectedkeys) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
469 self.assertEqual(len(keys), len(expectedkeys)) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
470 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
471 # Exercise pipes and filters style |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
472 s = 'abracadabra' |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
473 # sort s | uniq |
|
31210
c06b570adf12
Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents:
31136
diff
changeset
|
474 r = [k for k, g in groupby(sorted(s))] |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
475 self.assertEqual(r, ['a', 'b', 'c', 'd', 'r']) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
476 # sort s | uniq -d |
|
31210
c06b570adf12
Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents:
31136
diff
changeset
|
477 r = [k for k, g in groupby(sorted(s)) if list(islice(g,1,2))] |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
478 self.assertEqual(r, ['a', 'b', 'r']) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
479 # sort s | uniq -c |
|
31210
c06b570adf12
Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents:
31136
diff
changeset
|
480 r = [(len(list(g)), k) for k, g in groupby(sorted(s))] |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
481 self.assertEqual(r, [(5, 'a'), (2, 'b'), (1, 'c'), (1, 'd'), (2, 'r')]) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
482 # sort s | uniq -c | sort -rn | head -3 |
|
31210
c06b570adf12
Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents:
31136
diff
changeset
|
483 r = sorted([(len(list(g)) , k) for k, g in groupby(sorted(s))], reverse=True)[:3] |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
484 self.assertEqual(r, [(5, 'a'), (2, 'r'), (2, 'b')]) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
485 |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
486 # iter.__next__ failure |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
487 class ExpectedError(Exception): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
488 pass |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
489 def delayed_raise(n=0): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
490 for i in range(n): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
491 yield 'yo' |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
492 raise ExpectedError |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
493 def gulp(iterable, keyp=None, func=list): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
494 return [func(g) for k, g in groupby(iterable, keyp)] |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
495 |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
496 # iter.__next__ failure on outer object |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
497 self.assertRaises(ExpectedError, gulp, delayed_raise(0)) |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
498 # iter.__next__ failure on inner object |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
499 self.assertRaises(ExpectedError, gulp, delayed_raise(1)) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
500 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
501 # __cmp__ failure |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
502 class DummyCmp: |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
38669
diff
changeset
|
503 def __eq__(self, dst): |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
504 raise ExpectedError |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
505 s = [DummyCmp(), DummyCmp(), None] |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
506 |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
38669
diff
changeset
|
507 # __eq__ failure on outer object |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
508 self.assertRaises(ExpectedError, gulp, s, func=id) |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
38669
diff
changeset
|
509 # __eq__ failure on inner object |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
510 self.assertRaises(ExpectedError, gulp, s) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
511 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
512 # keyfunc failure |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
513 def keyfunc(obj): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
514 if keyfunc.skip > 0: |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
515 keyfunc.skip -= 1 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
516 return obj |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
517 else: |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
518 raise ExpectedError |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
519 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
520 # keyfunc failure on outer object |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
521 keyfunc.skip = 0 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
522 self.assertRaises(ExpectedError, gulp, [None], keyfunc) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
523 keyfunc.skip = 1 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
524 self.assertRaises(ExpectedError, gulp, [None, None], keyfunc) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
525 |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
526 def test_filter(self): |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
527 self.assertEqual(list(filter(isEven, range(6))), [0,2,4]) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
528 self.assertEqual(list(filter(None, [0,1,0,2,0])), [1,2]) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
529 self.assertEqual(list(filter(bool, [0,1,0,2,0])), [1,2]) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
530 self.assertEqual(take(4, filter(isEven, count())), [0,2,4,6]) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
531 self.assertRaises(TypeError, filter) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
532 self.assertRaises(TypeError, filter, lambda x:x) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
533 self.assertRaises(TypeError, filter, lambda x:x, range(6), 7) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
534 self.assertRaises(TypeError, filter, isEven, 3) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
535 self.assertRaises(TypeError, next, filter(range(6), range(6))) |
| 27769 | 536 |
|
45736
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
537 def test_filterfalse(self): |
|
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
538 self.assertEqual(list(filterfalse(isEven, range(6))), [1,3,5]) |
|
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
539 self.assertEqual(list(filterfalse(None, [0,1,0,2,0])), [0,0,0]) |
|
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
540 self.assertEqual(list(filterfalse(bool, [0,1,0,2,0])), [0,0,0]) |
|
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
541 self.assertEqual(take(4, filterfalse(isEven, count())), [1,3,5,7]) |
|
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
542 self.assertRaises(TypeError, filterfalse) |
|
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
543 self.assertRaises(TypeError, filterfalse, lambda x:x) |
|
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
544 self.assertRaises(TypeError, filterfalse, lambda x:x, range(6), 7) |
|
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
545 self.assertRaises(TypeError, filterfalse, isEven, 3) |
|
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
546 self.assertRaises(TypeError, next, filterfalse(range(6), range(6))) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
547 |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
548 def test_zip(self): |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
549 # XXX This is rather silly now that builtin zip() calls zip()... |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
550 ans = [(x,y) for x, y in zip('abc',count())] |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
551 self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)]) |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
552 self.assertEqual(list(zip('abc', range(6))), lzip('abc', range(6))) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
553 self.assertEqual(list(zip('abcdef', range(3))), lzip('abcdef', range(3))) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
554 self.assertEqual(take(3,zip('abcdef', count())), lzip('abcdef', range(3))) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
555 self.assertEqual(list(zip('abcdef')), lzip('abcdef')) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
556 self.assertEqual(list(zip()), lzip()) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
557 self.assertRaises(TypeError, zip, 3) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
558 self.assertRaises(TypeError, zip, range(3), 3) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
559 # Check tuple re-use (implementation detail) |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
560 self.assertEqual([tuple(list(pair)) for pair in zip('abc', 'def')], |
|
39656
08005ba6ceb9
Make built-in zip() equal to itertools.izip().
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
561 lzip('abc', 'def')) |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
562 self.assertEqual([pair for pair in zip('abc', 'def')], |
|
39656
08005ba6ceb9
Make built-in zip() equal to itertools.izip().
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
563 lzip('abc', 'def')) |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
564 ids = list(map(id, zip('abc', 'def'))) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
565 self.assertEqual(min(ids), max(ids)) |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
566 ids = list(map(id, list(zip('abc', 'def')))) |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
567 self.assertEqual(len(dict.fromkeys(ids)), len(ids)) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
568 |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
569 def test_ziplongest(self): |
|
40893
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
570 for args in [ |
|
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
571 ['abc', range(6)], |
|
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
572 [range(6), 'abc'], |
|
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
573 [range(1000), range(2000,2100), range(3000,3050)], |
|
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
574 [range(1000), range(0), range(3000,3050), range(1200), range(1500)], |
|
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
575 [range(1000), range(0), range(3000,3050), range(1200), range(1500), range(0)], |
|
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
576 ]: |
|
42142
a53e48be6df9
Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
577 target = [tuple([arg[i] if i < len(arg) else None for arg in args]) |
|
a53e48be6df9
Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
578 for i in range(max(map(len, args)))] |
|
45736
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
579 self.assertEqual(list(zip_longest(*args)), target) |
|
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
580 self.assertEqual(list(zip_longest(*args, **{})), target) |
|
40893
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
581 target = [tuple((e is None and 'X' or e) for e in t) for t in target] # Replace None fills with 'X' |
|
45736
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
582 self.assertEqual(list(zip_longest(*args, **dict(fillvalue='X'))), target) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
583 |
|
45736
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
584 self.assertEqual(take(3,zip_longest('abcdef', count())), list(zip('abcdef', range(3)))) # take 3 from infinite input |
|
40893
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
585 |
|
45736
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
586 self.assertEqual(list(zip_longest()), list(zip())) |
|
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
587 self.assertEqual(list(zip_longest([])), list(zip([]))) |
|
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
588 self.assertEqual(list(zip_longest('abcdef')), list(zip('abcdef'))) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
589 |
|
45736
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
590 self.assertEqual(list(zip_longest('abc', 'defg', **{})), |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
591 list(zip(list('abc')+[None], 'defg'))) # empty keyword dict |
|
45736
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
592 self.assertRaises(TypeError, zip_longest, 3) |
|
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
593 self.assertRaises(TypeError, zip_longest, range(3), 3) |
|
40893
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
594 |
|
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
595 for stmt in [ |
|
45736
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
596 "zip_longest('abc', fv=1)", |
|
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
597 "zip_longest('abc', fillvalue=1, bogus_keyword=None)", |
|
40893
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
598 ]: |
|
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
599 try: |
|
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
600 eval(stmt, globals(), locals()) |
|
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
601 except TypeError: |
|
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
602 pass |
|
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
603 else: |
|
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
604 self.fail('Did not raise Type in: ' + stmt) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
605 |
|
40893
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
606 # Check tuple re-use (implementation detail) |
|
45736
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
607 self.assertEqual([tuple(list(pair)) for pair in zip_longest('abc', 'def')], |
|
40893
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
608 list(zip('abc', 'def'))) |
|
45736
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
609 self.assertEqual([pair for pair in zip_longest('abc', 'def')], |
|
40893
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
610 list(zip('abc', 'def'))) |
|
45736
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
611 ids = list(map(id, zip_longest('abc', 'def'))) |
|
40893
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
612 self.assertEqual(min(ids), max(ids)) |
|
45736
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
613 ids = list(map(id, list(zip_longest('abc', 'def')))) |
|
40893
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
614 self.assertEqual(len(dict.fromkeys(ids)), len(ids)) |
|
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
615 |
|
57042
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
616 def test_bug_7244(self): |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
617 |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
618 class Repeater: |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
619 # this class is similar to itertools.repeat |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
620 def __init__(self, o, t, e): |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
621 self.o = o |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
622 self.t = int(t) |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
623 self.e = e |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
624 def __iter__(self): # its iterator is itself |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
625 return self |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
626 def __next__(self): |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
627 if self.t > 0: |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
628 self.t -= 1 |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
629 return self.o |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
630 else: |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
631 raise self.e |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
632 |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
633 # Formerly this code in would fail in debug mode |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
634 # with Undetected Error and Stop Iteration |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
635 r1 = Repeater(1, 3, StopIteration) |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
636 r2 = Repeater(2, 4, StopIteration) |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
637 def run(r1, r2): |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
638 result = [] |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
639 for i, j in zip_longest(r1, r2, fillvalue=0): |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
640 with support.captured_output('stdout'): |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
641 print((i, j)) |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
642 result.append((i, j)) |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
643 return result |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
644 self.assertEqual(run(r1, r2), [(1,2), (1,2), (1,2), (0,2)]) |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
645 |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
646 # Formerly, the RuntimeError would be lost |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
647 # and StopIteration would stop as expected |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
648 r1 = Repeater(1, 3, RuntimeError) |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
649 r2 = Repeater(2, 4, StopIteration) |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
650 it = zip_longest(r1, r2, fillvalue=0) |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
651 self.assertEqual(next(it), (1, 2)) |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
652 self.assertEqual(next(it), (1, 2)) |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
653 self.assertEqual(next(it), (1, 2)) |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
654 self.assertRaises(RuntimeError, next, it) |
|
658eb58d2e68
Fix exception handling in itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
655 |
|
45435
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
656 def test_product(self): |
|
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
657 for args, result in [ |
|
45659
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
658 ([], [()]), # zero iterables |
|
45435
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
659 (['ab'], [('a',), ('b',)]), # one iterable |
|
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
660 ([range(2), range(3)], [(0,0), (0,1), (0,2), (1,0), (1,1), (1,2)]), # two iterables |
|
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
661 ([range(0), range(2), range(3)], []), # first iterable with zero length |
|
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
662 ([range(2), range(0), range(3)], []), # middle iterable with zero length |
|
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
663 ([range(2), range(3), range(0)], []), # last iterable with zero length |
|
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
664 ]: |
|
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
665 self.assertEqual(list(product(*args)), result) |
|
45569
937a3c5af691
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61104,61110-61112,61114-61115,61117-61125 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45548
diff
changeset
|
666 for r in range(4): |
|
937a3c5af691
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61104,61110-61112,61114-61115,61117-61125 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45548
diff
changeset
|
667 self.assertEqual(list(product(*(args*r))), |
|
937a3c5af691
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61104,61110-61112,61114-61115,61117-61125 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45548
diff
changeset
|
668 list(product(*args, **dict(repeat=r)))) |
|
45435
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
669 self.assertEqual(len(list(product(*[range(7)]*6))), 7**6) |
|
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
670 self.assertRaises(TypeError, product, range(6), None) |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
671 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
672 def product1(*args, **kwds): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
673 pools = list(map(tuple, args)) * kwds.get('repeat', 1) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
674 n = len(pools) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
675 if n == 0: |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
676 yield () |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
677 return |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
678 if any(len(pool) == 0 for pool in pools): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
679 return |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
680 indices = [0] * n |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
681 yield tuple(pool[i] for pool, i in zip(pools, indices)) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
682 while 1: |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
683 for i in reversed(range(n)): # right to left |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
684 if indices[i] == len(pools[i]) - 1: |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
685 continue |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
686 indices[i] += 1 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
687 for j in range(i+1, n): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
688 indices[j] = 0 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
689 yield tuple(pool[i] for pool, i in zip(pools, indices)) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
690 break |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
691 else: |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
692 return |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
693 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
694 def product2(*args, **kwds): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
695 'Pure python version used in docs' |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
696 pools = list(map(tuple, args)) * kwds.get('repeat', 1) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
697 result = [[]] |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
698 for pool in pools: |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
699 result = [x+[y] for x in result for y in pool] |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
700 for prod in result: |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
701 yield tuple(prod) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
702 |
|
45435
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
703 argtypes = ['', 'abc', '', range(0), range(4), dict(a=1, b=2, c=3), |
|
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
704 set('abcdefg'), range(11), tuple(range(13))] |
|
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
705 for i in range(100): |
|
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
706 args = [random.choice(argtypes) for j in range(random.randrange(5))] |
|
45659
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
707 expected_len = prod(map(len, args)) |
|
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
708 self.assertEqual(len(list(product(*args))), expected_len) |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
709 self.assertEqual(list(product(*args)), list(product1(*args))) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
710 self.assertEqual(list(product(*args)), list(product2(*args))) |
|
45435
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
711 args = map(iter, args) |
|
45659
dd318c3f6cb7
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45584
diff
changeset
|
712 self.assertEqual(len(list(product(*args))), expected_len) |
|
45435
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
713 |
|
45446
5e0772ed20d1
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60969,60971-60976 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45435
diff
changeset
|
714 # Test implementation detail: tuple re-use |
|
5e0772ed20d1
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60969,60971-60976 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45435
diff
changeset
|
715 self.assertEqual(len(set(map(id, product('abc', 'def')))), 1) |
|
5e0772ed20d1
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60969,60971-60976 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45435
diff
changeset
|
716 self.assertNotEqual(len(set(map(id, list(product('abc', 'def'))))), 1) |
|
45435
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
717 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
718 def test_repeat(self): |
|
51719
3dbdbc5e6d85
Add keyword arg support to itertools.repeat().
Raymond Hettinger <python@rcn.com>
parents:
51717
diff
changeset
|
719 self.assertEqual(list(repeat(object='a', times=3)), ['a', 'a', 'a']) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
720 self.assertEqual(lzip(range(3),repeat('a')), |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
721 [(0, 'a'), (1, 'a'), (2, 'a')]) |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27769
diff
changeset
|
722 self.assertEqual(list(repeat('a', 3)), ['a', 'a', 'a']) |
| 29690 | 723 self.assertEqual(take(3, repeat('a')), ['a', 'a', 'a']) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
724 self.assertEqual(list(repeat('a', 0)), []) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
725 self.assertEqual(list(repeat('a', -3)), []) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
726 self.assertRaises(TypeError, repeat) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
727 self.assertRaises(TypeError, repeat, None, 3, 4) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
728 self.assertRaises(TypeError, repeat, None, 'a') |
|
33616
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
729 r = repeat(1+0j) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
730 self.assertEqual(repr(r), 'repeat((1+0j))') |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
731 r = repeat(1+0j, 5) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
732 self.assertEqual(repr(r), 'repeat((1+0j), 5)') |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
733 list(r) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
734 self.assertEqual(repr(r), 'repeat((1+0j), 0)') |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
735 |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
736 def test_map(self): |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
737 self.assertEqual(list(map(operator.pow, range(3), range(1,7))), |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
738 [0**1, 1**2, 2**3]) |
|
44881
5298abbdd91c
Replace map(None, *iterables) with zip(*iterables).
Raymond Hettinger <python@rcn.com>
parents:
44742
diff
changeset
|
739 def tupleize(*args): |
|
5298abbdd91c
Replace map(None, *iterables) with zip(*iterables).
Raymond Hettinger <python@rcn.com>
parents:
44742
diff
changeset
|
740 return args |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
741 self.assertEqual(list(map(tupleize, 'abc', range(5))), |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
742 [('a',0),('b',1),('c',2)]) |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
743 self.assertEqual(list(map(tupleize, 'abc', count())), |
| 29690 | 744 [('a',0),('b',1),('c',2)]) |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
745 self.assertEqual(take(2,map(tupleize, 'abc', count())), |
| 29690 | 746 [('a',0),('b',1)]) |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
747 self.assertEqual(list(map(operator.pow, [])), []) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
748 self.assertRaises(TypeError, map) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
749 self.assertRaises(TypeError, list, map(None, range(3), range(3))) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
750 self.assertRaises(TypeError, map, operator.neg) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
751 self.assertRaises(TypeError, next, map(10, range(5))) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
752 self.assertRaises(ValueError, next, map(errfunc, [4], [5])) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
753 self.assertRaises(TypeError, next, map(onearg, [4], [5])) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
754 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
755 def test_starmap(self): |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
756 self.assertEqual(list(starmap(operator.pow, zip(range(3), range(1,7)))), |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
757 [0**1, 1**2, 2**3]) |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
758 self.assertEqual(take(3, starmap(operator.pow, zip(count(), count(1)))), |
| 29690 | 759 [0**1, 1**2, 2**3]) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
760 self.assertEqual(list(starmap(operator.pow, [])), []) |
|
44742
14d4104976fc
Merged revisions 59985-60000,60002,60005-60007,60009-60042 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
761 self.assertEqual(list(starmap(operator.pow, [iter([4,5])])), [4**5]) |
|
14d4104976fc
Merged revisions 59985-60000,60002,60005-60007,60009-60042 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
762 self.assertRaises(TypeError, list, starmap(operator.pow, [None])) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
763 self.assertRaises(TypeError, starmap) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
764 self.assertRaises(TypeError, starmap, operator.pow, [(4,5)], 'extra') |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
765 self.assertRaises(TypeError, next, starmap(10, [(4,5)])) |
|
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
766 self.assertRaises(ValueError, next, starmap(errfunc, [(4,5)])) |
|
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
767 self.assertRaises(TypeError, next, starmap(onearg, [(4,5)])) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
768 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
769 def test_islice(self): |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
770 for args in [ # islice(args) should agree with range(args) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
771 (10, 20, 3), |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
772 (10, 3, 20), |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
773 (10, 20), |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
774 (10, 3), |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
775 (20,) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
776 ]: |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
777 self.assertEqual(list(islice(range(100), *args)), |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
778 list(range(*args))) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
779 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
780 for args, tgtargs in [ # Stop when seqn is exhausted |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
781 ((10, 110, 3), ((10, 100, 3))), |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
782 ((10, 110), ((10, 100))), |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
783 ((110,), (100,)) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
784 ]: |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
785 self.assertEqual(list(islice(range(100), *args)), |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
786 list(range(*tgtargs))) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
787 |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
788 # Test stop=None |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
789 self.assertEqual(list(islice(range(10), None)), list(range(10))) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
790 self.assertEqual(list(islice(range(10), None, None)), list(range(10))) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
791 self.assertEqual(list(islice(range(10), None, None, None)), list(range(10))) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
792 self.assertEqual(list(islice(range(10), 2, None)), list(range(2, 10))) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
793 self.assertEqual(list(islice(range(10), 1, None, 2)), list(range(1, 10, 2))) |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
794 |
|
34814
a412638950a1
SF patch #1171417: bug fix for islice() in docs
Raymond Hettinger <python@rcn.com>
parents:
34738
diff
changeset
|
795 # Test number of items consumed SF #1171417 |
|
a412638950a1
SF patch #1171417: bug fix for islice() in docs
Raymond Hettinger <python@rcn.com>
parents:
34738
diff
changeset
|
796 it = iter(range(10)) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
797 self.assertEqual(list(islice(it, 3)), list(range(3))) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
798 self.assertEqual(list(it), list(range(3, 10))) |
|
34814
a412638950a1
SF patch #1171417: bug fix for islice() in docs
Raymond Hettinger <python@rcn.com>
parents:
34738
diff
changeset
|
799 |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
800 # Test invalid arguments |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
801 self.assertRaises(TypeError, islice, range(10)) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
802 self.assertRaises(TypeError, islice, range(10), 1, 2, 3, 4) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
803 self.assertRaises(ValueError, islice, range(10), -5, 10, 1) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
804 self.assertRaises(ValueError, islice, range(10), 1, -5, -1) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
805 self.assertRaises(ValueError, islice, range(10), 1, 10, -1) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
806 self.assertRaises(ValueError, islice, range(10), 1, 10, 0) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
807 self.assertRaises(ValueError, islice, range(10), 'a') |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
808 self.assertRaises(ValueError, islice, range(10), 'a', 1) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
809 self.assertRaises(ValueError, islice, range(10), 1, 'a') |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
810 self.assertRaises(ValueError, islice, range(10), 'a', 1, 1) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
811 self.assertRaises(ValueError, islice, range(10), 1, 'a', 1) |
|
41716
00c02c4488ce
Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
812 self.assertEqual(len(list(islice(count(), 1, 10, maxsize))), 1) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
813 |
|
66548
9e9a709b389e
Issue #10323: Predictable final state for slice().
Raymond Hettinger <python@rcn.com>
parents:
66340
diff
changeset
|
814 # Issue #10323: Less islice in a predictable state |
|
9e9a709b389e
Issue #10323: Predictable final state for slice().
Raymond Hettinger <python@rcn.com>
parents:
66340
diff
changeset
|
815 c = count() |
|
9e9a709b389e
Issue #10323: Predictable final state for slice().
Raymond Hettinger <python@rcn.com>
parents:
66340
diff
changeset
|
816 self.assertEqual(list(islice(c, 1, 3, 50)), [1]) |
|
9e9a709b389e
Issue #10323: Predictable final state for slice().
Raymond Hettinger <python@rcn.com>
parents:
66340
diff
changeset
|
817 self.assertEqual(next(c), 3) |
|
9e9a709b389e
Issue #10323: Predictable final state for slice().
Raymond Hettinger <python@rcn.com>
parents:
66340
diff
changeset
|
818 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
819 def test_takewhile(self): |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
820 data = [1, 3, 5, 20, 2, 4, 6, 8] |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
821 underten = lambda x: x<10 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
822 self.assertEqual(list(takewhile(underten, data)), [1, 3, 5]) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
823 self.assertEqual(list(takewhile(underten, [])), []) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
824 self.assertRaises(TypeError, takewhile) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
825 self.assertRaises(TypeError, takewhile, operator.pow) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
826 self.assertRaises(TypeError, takewhile, operator.pow, [(4,5)], 'extra') |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
827 self.assertRaises(TypeError, next, takewhile(10, [(4,5)])) |
|
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
828 self.assertRaises(ValueError, next, takewhile(errfunc, [(4,5)])) |
|
33616
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
829 t = takewhile(bool, [1, 1, 1, 0, 0, 0]) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
830 self.assertEqual(list(t), [1, 1, 1]) |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
831 self.assertRaises(StopIteration, next, t) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
832 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
833 def test_dropwhile(self): |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
834 data = [1, 3, 5, 20, 2, 4, 6, 8] |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
835 underten = lambda x: x<10 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
836 self.assertEqual(list(dropwhile(underten, data)), [20, 2, 4, 6, 8]) |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
837 self.assertEqual(list(dropwhile(underten, [])), []) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
838 self.assertRaises(TypeError, dropwhile) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
839 self.assertRaises(TypeError, dropwhile, operator.pow) |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28936
diff
changeset
|
840 self.assertRaises(TypeError, dropwhile, operator.pow, [(4,5)], 'extra') |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
841 self.assertRaises(TypeError, next, dropwhile(10, [(4,5)])) |
|
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
842 self.assertRaises(ValueError, next, dropwhile(errfunc, [(4,5)])) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
843 |
| 30764 | 844 def test_tee(self): |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
845 n = 200 |
| 30764 | 846 def irange(n): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
847 for i in range(n): |
| 30764 | 848 yield i |
| 849 | |
| 850 a, b = tee([]) # test empty iterator | |
| 851 self.assertEqual(list(a), []) | |
| 852 self.assertEqual(list(b), []) | |
| 853 | |
| 854 a, b = tee(irange(n)) # test 100% interleaved | |
|
39656
08005ba6ceb9
Make built-in zip() equal to itertools.izip().
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
855 self.assertEqual(lzip(a,b), lzip(range(n), range(n))) |
| 30764 | 856 |
| 857 a, b = tee(irange(n)) # test 0% interleaved | |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
858 self.assertEqual(list(a), list(range(n))) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
859 self.assertEqual(list(b), list(range(n))) |
| 30764 | 860 |
| 861 a, b = tee(irange(n)) # test dealloc of leading iterator | |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
862 for i in range(100): |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
863 self.assertEqual(next(a), i) |
| 30764 | 864 del a |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
865 self.assertEqual(list(b), list(range(n))) |
| 30764 | 866 |
| 867 a, b = tee(irange(n)) # test dealloc of trailing iterator | |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
868 for i in range(100): |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
869 self.assertEqual(next(a), i) |
| 30764 | 870 del b |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
871 self.assertEqual(list(a), list(range(100, n))) |
| 30764 | 872 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
873 for j in range(5): # test randomly interleaved |
| 30764 | 874 order = [0]*n + [1]*n |
| 875 random.shuffle(order) | |
| 876 lists = ([], []) | |
| 877 its = tee(irange(n)) | |
| 878 for i in order: | |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
879 value = next(its[i]) |
| 30764 | 880 lists[i].append(value) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
881 self.assertEqual(lists[0], list(range(n))) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
882 self.assertEqual(lists[1], list(range(n))) |
| 30764 | 883 |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
884 # test argument format checking |
| 30764 | 885 self.assertRaises(TypeError, tee) |
| 886 self.assertRaises(TypeError, tee, 3) | |
| 887 self.assertRaises(TypeError, tee, [1,2], 'x') | |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
888 self.assertRaises(TypeError, tee, [1,2], 3, 'x') |
| 30764 | 889 |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
890 # tee object should be instantiable |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
891 a, b = tee('abc') |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
892 c = type(a)('def') |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
893 self.assertEqual(list(c), list('def')) |
|
30786
225d848a5f96
Minor improvements to itertools.tee():
Raymond Hettinger <python@rcn.com>
parents:
30764
diff
changeset
|
894 |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
895 # test long-lagged and multi-way split |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
896 a, b, c = tee(range(2000), 3) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
897 for i in range(100): |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
898 self.assertEqual(next(a), i) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
899 self.assertEqual(list(b), list(range(2000))) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
900 self.assertEqual([next(c), next(c)], list(range(2))) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
901 self.assertEqual(list(a), list(range(100,2000))) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
902 self.assertEqual(list(c), list(range(2,2000))) |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
903 |
|
33616
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
904 # test values of n |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
905 self.assertRaises(TypeError, tee, 'abc', 'invalid') |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39656
diff
changeset
|
906 self.assertRaises(ValueError, tee, [], -1) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
907 for n in range(5): |
|
33616
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
908 result = tee('abc', n) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
909 self.assertEqual(type(result), tuple) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
910 self.assertEqual(len(result), n) |
|
42142
a53e48be6df9
Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
911 self.assertEqual([list(x) for x in result], [list('abc')]*n) |
|
33616
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
912 |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
913 # tee pass-through to copyable iterator |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
914 a, b = tee('abc') |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
915 c, d = tee(a) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51778
diff
changeset
|
916 self.assertTrue(a is c) |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
917 |
|
33616
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
918 # test tee_new |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
919 t1, t2 = tee('abc') |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
920 tnew = type(t1) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
921 self.assertRaises(TypeError, tnew) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
922 self.assertRaises(TypeError, tnew, 10) |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
923 t3 = tnew(t1) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51778
diff
changeset
|
924 self.assertTrue(list(t1) == list(t2) == list(t3) == list('abc')) |
|
30786
225d848a5f96
Minor improvements to itertools.tee():
Raymond Hettinger <python@rcn.com>
parents:
30764
diff
changeset
|
925 |
|
33777
e4b7c923ed5f
Fix and test weak referencing of itertools.tee objects.
Raymond Hettinger <python@rcn.com>
parents:
33624
diff
changeset
|
926 # test that tee objects are weak referencable |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
927 a, b = tee(range(10)) |
|
33777
e4b7c923ed5f
Fix and test weak referencing of itertools.tee objects.
Raymond Hettinger <python@rcn.com>
parents:
33624
diff
changeset
|
928 p = proxy(a) |
|
e4b7c923ed5f
Fix and test weak referencing of itertools.tee objects.
Raymond Hettinger <python@rcn.com>
parents:
33624
diff
changeset
|
929 self.assertEqual(getattr(p, '__class__'), type(b)) |
|
e4b7c923ed5f
Fix and test weak referencing of itertools.tee objects.
Raymond Hettinger <python@rcn.com>
parents:
33624
diff
changeset
|
930 del a |
|
e4b7c923ed5f
Fix and test weak referencing of itertools.tee objects.
Raymond Hettinger <python@rcn.com>
parents:
33624
diff
changeset
|
931 self.assertRaises(ReferenceError, getattr, p, '__class__') |
|
e4b7c923ed5f
Fix and test weak referencing of itertools.tee objects.
Raymond Hettinger <python@rcn.com>
parents:
33624
diff
changeset
|
932 |
|
28936
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
933 def test_StopIteration(self): |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
934 self.assertRaises(StopIteration, next, zip()) |
|
30093
0ac4396b1894
Modified itertools.izip() to match the behavior of __builtin__.zip()
Raymond Hettinger <python@rcn.com>
parents:
30092
diff
changeset
|
935 |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
936 for f in (chain, cycle, zip, groupby): |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
937 self.assertRaises(StopIteration, next, f([])) |
|
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
938 self.assertRaises(StopIteration, next, f(StopNow())) |
|
28936
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
939 |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
940 self.assertRaises(StopIteration, next, islice([], None)) |
|
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
941 self.assertRaises(StopIteration, next, islice(StopNow(), None)) |
|
28936
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
942 |
| 30764 | 943 p, q = tee([]) |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
944 self.assertRaises(StopIteration, next, p) |
|
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
945 self.assertRaises(StopIteration, next, q) |
| 30764 | 946 p, q = tee(StopNow()) |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
947 self.assertRaises(StopIteration, next, p) |
|
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
948 self.assertRaises(StopIteration, next, q) |
| 30764 | 949 |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
950 self.assertRaises(StopIteration, next, repeat(None, 0)) |
|
28936
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
951 |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
952 for f in (filter, filterfalse, map, takewhile, dropwhile, starmap): |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
953 self.assertRaises(StopIteration, next, f(lambda x:x, [])) |
|
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
954 self.assertRaises(StopIteration, next, f(lambda x:x, StopNow())) |
|
28936
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
955 |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
956 class TestExamples(unittest.TestCase): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
957 |
|
66589
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
958 def test_accumlate(self): |
|
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
959 self.assertEqual(list(accumulate([1,2,3,4,5])), [1, 3, 6, 10, 15]) |
|
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
960 |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
961 def test_chain(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
962 self.assertEqual(''.join(chain('ABC', 'DEF')), 'ABCDEF') |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
963 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
964 def test_chain_from_iterable(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
965 self.assertEqual(''.join(chain.from_iterable(['ABC', 'DEF'])), 'ABCDEF') |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
966 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
967 def test_combinations(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
968 self.assertEqual(list(combinations('ABCD', 2)), |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
969 [('A','B'), ('A','C'), ('A','D'), ('B','C'), ('B','D'), ('C','D')]) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
970 self.assertEqual(list(combinations(range(4), 3)), |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
971 [(0,1,2), (0,1,3), (0,2,3), (1,2,3)]) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
972 |
|
51100
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
973 def test_combinations_with_replacement(self): |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
974 self.assertEqual(list(combinations_with_replacement('ABC', 2)), |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
975 [('A','A'), ('A','B'), ('A','C'), ('B','B'), ('B','C'), ('C','C')]) |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
976 |
|
51063
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
977 def test_compress(self): |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
978 self.assertEqual(list(compress('ABCDEF', [1,0,1,0,1,1])), list('ACEF')) |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
979 |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
980 def test_count(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
981 self.assertEqual(list(islice(count(10), 5)), [10, 11, 12, 13, 14]) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
982 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
983 def test_cycle(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
984 self.assertEqual(list(islice(cycle('ABCD'), 12)), list('ABCDABCDABCD')) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
985 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
986 def test_dropwhile(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
987 self.assertEqual(list(dropwhile(lambda x: x<5, [1,4,6,4,1])), [6,4,1]) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
988 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
989 def test_groupby(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
990 self.assertEqual([k for k, g in groupby('AAAABBBCCDAABBB')], |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
991 list('ABCDAB')) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
992 self.assertEqual([(list(g)) for k, g in groupby('AAAABBBCCD')], |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
993 [list('AAAA'), list('BBB'), list('CC'), list('D')]) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
994 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
995 def test_filter(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
996 self.assertEqual(list(filter(lambda x: x%2, range(10))), [1,3,5,7,9]) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
997 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
998 def test_filterfalse(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
999 self.assertEqual(list(filterfalse(lambda x: x%2, range(10))), [0,2,4,6,8]) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1000 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1001 def test_map(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1002 self.assertEqual(list(map(pow, (2,3,10), (5,2,3))), [32, 9, 1000]) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1003 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1004 def test_islice(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1005 self.assertEqual(list(islice('ABCDEFG', 2)), list('AB')) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1006 self.assertEqual(list(islice('ABCDEFG', 2, 4)), list('CD')) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1007 self.assertEqual(list(islice('ABCDEFG', 2, None)), list('CDEFG')) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1008 self.assertEqual(list(islice('ABCDEFG', 0, None, 2)), list('ACEG')) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1009 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1010 def test_zip(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1011 self.assertEqual(list(zip('ABCD', 'xy')), [('A', 'x'), ('B', 'y')]) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1012 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1013 def test_zip_longest(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1014 self.assertEqual(list(zip_longest('ABCD', 'xy', fillvalue='-')), |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1015 [('A', 'x'), ('B', 'y'), ('C', '-'), ('D', '-')]) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1016 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1017 def test_permutations(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1018 self.assertEqual(list(permutations('ABCD', 2)), |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1019 list(map(tuple, 'AB AC AD BA BC BD CA CB CD DA DB DC'.split()))) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1020 self.assertEqual(list(permutations(range(3))), |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1021 [(0,1,2), (0,2,1), (1,0,2), (1,2,0), (2,0,1), (2,1,0)]) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1022 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1023 def test_product(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1024 self.assertEqual(list(product('ABCD', 'xy')), |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1025 list(map(tuple, 'Ax Ay Bx By Cx Cy Dx Dy'.split()))) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1026 self.assertEqual(list(product(range(2), repeat=3)), |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1027 [(0,0,0), (0,0,1), (0,1,0), (0,1,1), |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1028 (1,0,0), (1,0,1), (1,1,0), (1,1,1)]) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1029 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1030 def test_repeat(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1031 self.assertEqual(list(repeat(10, 3)), [10, 10, 10]) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1032 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1033 def test_stapmap(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1034 self.assertEqual(list(starmap(pow, [(2,5), (3,2), (10,3)])), |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1035 [32, 9, 1000]) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1036 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1037 def test_takewhile(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1038 self.assertEqual(list(takewhile(lambda x: x<5, [1,4,6,4,1])), [1,4]) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1039 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1040 |
| 30764 | 1041 class TestGC(unittest.TestCase): |
| 1042 | |
| 1043 def makecycle(self, iterator, container): | |
| 1044 container.append(iterator) | |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
1045 next(iterator) |
| 30764 | 1046 del container, iterator |
| 1047 | |
|
66589
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
1048 def test_accumulate(self): |
|
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
1049 a = [] |
|
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
1050 self.makecycle(accumulate([1,2,a,3]), a) |
|
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
1051 |
| 30764 | 1052 def test_chain(self): |
| 1053 a = [] | |
| 1054 self.makecycle(chain(a), a) | |
| 1055 | |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1056 def test_chain_from_iterable(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1057 a = [] |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1058 self.makecycle(chain.from_iterable([a]), a) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1059 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1060 def test_combinations(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1061 a = [] |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1062 self.makecycle(combinations([1,2,a,3], 3), a) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1063 |
|
51100
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
1064 def test_combinations_with_replacement(self): |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
1065 a = [] |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
1066 self.makecycle(combinations_with_replacement([1,2,a,3], 3), a) |
|
4005011d3a09
Forward port r69001: itertools.combinations_with_replacement().
Raymond Hettinger <python@rcn.com>
parents:
51063
diff
changeset
|
1067 |
|
51063
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
1068 def test_compress(self): |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
1069 a = [] |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
1070 self.makecycle(compress('ABCDEF', [1,0,1,0,1,0]), a) |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
1071 |
|
51661
003e00797e91
Add GC support to count() objects.
Raymond Hettinger <python@rcn.com>
parents:
51624
diff
changeset
|
1072 def test_count(self): |
|
003e00797e91
Add GC support to count() objects.
Raymond Hettinger <python@rcn.com>
parents:
51624
diff
changeset
|
1073 a = [] |
|
003e00797e91
Add GC support to count() objects.
Raymond Hettinger <python@rcn.com>
parents:
51624
diff
changeset
|
1074 Int = type('Int', (int,), dict(x=a)) |
|
003e00797e91
Add GC support to count() objects.
Raymond Hettinger <python@rcn.com>
parents:
51624
diff
changeset
|
1075 self.makecycle(count(Int(0), Int(1)), a) |
|
003e00797e91
Add GC support to count() objects.
Raymond Hettinger <python@rcn.com>
parents:
51624
diff
changeset
|
1076 |
| 30764 | 1077 def test_cycle(self): |
| 1078 a = [] | |
| 1079 self.makecycle(cycle([a]*2), a) | |
| 1080 | |
|
33624
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1081 def test_dropwhile(self): |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1082 a = [] |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1083 self.makecycle(dropwhile(bool, [0, a, a]), a) |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1084 |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1085 def test_groupby(self): |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1086 a = [] |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1087 self.makecycle(groupby([a]*2, lambda x:x), a) |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1088 |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1089 def test_issue2246(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1090 # Issue 2246 -- the _grouper iterator was not included in GC |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1091 n = 10 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1092 keyfunc = lambda x: x |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1093 for i, j in groupby(range(n), key=keyfunc): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1094 keyfunc.__dict__.setdefault('x',[]).append(j) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1095 |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1096 def test_filter(self): |
| 30764 | 1097 a = [] |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1098 self.makecycle(filter(lambda x:True, [a]*2), a) |
| 30764 | 1099 |
|
45736
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
1100 def test_filterfalse(self): |
| 30764 | 1101 a = [] |
|
45736
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
1102 self.makecycle(filterfalse(lambda x:False, a), a) |
| 30764 | 1103 |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1104 def test_zip(self): |
| 30764 | 1105 a = [] |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1106 self.makecycle(zip([a]*2, [a]*3), a) |
| 30764 | 1107 |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1108 def test_zip_longest(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1109 a = [] |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1110 self.makecycle(zip_longest([a]*2, [a]*3), a) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1111 b = [a, None] |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1112 self.makecycle(zip_longest([a]*2, [a]*3, fillvalue=b), a) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1113 |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1114 def test_map(self): |
| 30764 | 1115 a = [] |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1116 self.makecycle(map(lambda x:x, [a]*2), a) |
| 30764 | 1117 |
| 1118 def test_islice(self): | |
| 1119 a = [] | |
| 1120 self.makecycle(islice([a]*2, None), a) | |
| 1121 | |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1122 def test_permutations(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1123 a = [] |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1124 self.makecycle(permutations([1,2,a,3], 3), a) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1125 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1126 def test_product(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1127 a = [] |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1128 self.makecycle(product([1,2,a,3], repeat=3), a) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1129 |
|
33624
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1130 def test_repeat(self): |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1131 a = [] |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1132 self.makecycle(repeat(a), a) |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1133 |
| 30764 | 1134 def test_starmap(self): |
| 1135 a = [] | |
| 1136 self.makecycle(starmap(lambda *t: t, [(a,a)]*2), a) | |
| 1137 | |
|
33624
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1138 def test_takewhile(self): |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1139 a = [] |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1140 self.makecycle(takewhile(bool, [1, 0, a, a]), a) |
|
5730f77e37e4
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
1141 |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1142 def R(seqn): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1143 'Regular generator' |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1144 for i in seqn: |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1145 yield i |
|
28936
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
1146 |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1147 class G: |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1148 'Sequence using __getitem__' |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1149 def __init__(self, seqn): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1150 self.seqn = seqn |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1151 def __getitem__(self, i): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1152 return self.seqn[i] |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1153 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1154 class I: |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1155 'Sequence using iterator protocol' |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1156 def __init__(self, seqn): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1157 self.seqn = seqn |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1158 self.i = 0 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1159 def __iter__(self): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1160 return self |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
1161 def __next__(self): |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1162 if self.i >= len(self.seqn): raise StopIteration |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1163 v = self.seqn[self.i] |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1164 self.i += 1 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1165 return v |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1166 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1167 class Ig: |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1168 'Sequence using iterator protocol defined with a generator' |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1169 def __init__(self, seqn): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1170 self.seqn = seqn |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1171 self.i = 0 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1172 def __iter__(self): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1173 for val in self.seqn: |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1174 yield val |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1175 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1176 class X: |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1177 'Missing __getitem__ and __iter__' |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1178 def __init__(self, seqn): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1179 self.seqn = seqn |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1180 self.i = 0 |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
1181 def __next__(self): |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1182 if self.i >= len(self.seqn): raise StopIteration |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1183 v = self.seqn[self.i] |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1184 self.i += 1 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1185 return v |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1186 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1187 class N: |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
1188 'Iterator missing __next__()' |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1189 def __init__(self, seqn): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1190 self.seqn = seqn |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1191 self.i = 0 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1192 def __iter__(self): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1193 return self |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1194 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1195 class E: |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1196 'Test propagation of exceptions' |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1197 def __init__(self, seqn): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1198 self.seqn = seqn |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1199 self.i = 0 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1200 def __iter__(self): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1201 return self |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
1202 def __next__(self): |
|
33597
355b15e4a4da
Use floor division operator.
Raymond Hettinger <python@rcn.com>
parents:
33546
diff
changeset
|
1203 3 // 0 |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1204 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1205 class S: |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1206 'Test immediate stop' |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1207 def __init__(self, seqn): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1208 pass |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1209 def __iter__(self): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1210 return self |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
1211 def __next__(self): |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1212 raise StopIteration |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1213 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1214 def L(seqn): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1215 'Test multiple tiers of iterators' |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1216 return chain(map(lambda x:x, R(Ig(G(seqn))))) |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1217 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1218 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1219 class TestVariousIteratorArgs(unittest.TestCase): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1220 |
|
66589
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
1221 def test_accumulate(self): |
|
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
1222 s = [1,2,3,4,5] |
|
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
1223 r = [1,3,6,10,15] |
|
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
1224 n = len(s) |
|
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
1225 for g in (G, I, Ig, L, R): |
|
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
1226 self.assertEqual(list(accumulate(g(s))), r) |
|
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
1227 self.assertEqual(list(accumulate(S(s))), []) |
|
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
1228 self.assertRaises(TypeError, accumulate, X(s)) |
|
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
1229 self.assertRaises(TypeError, accumulate, N(s)) |
|
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
1230 self.assertRaises(ZeroDivisionError, list, accumulate(E(s))) |
|
5da093b49c2a
Add itertools.accumulate().
Raymond Hettinger <python@rcn.com>
parents:
66546
diff
changeset
|
1231 |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1232 def test_chain(self): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
1233 for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)): |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1234 for g in (G, I, Ig, S, L, R): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1235 self.assertEqual(list(chain(g(s))), list(g(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1236 self.assertEqual(list(chain(g(s), g(s))), list(g(s))+list(g(s))) |
|
45569
937a3c5af691
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61104,61110-61112,61114-61115,61117-61125 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45548
diff
changeset
|
1237 self.assertRaises(TypeError, list, chain(X(s))) |
|
45584
9b8fc405f9b0
Fix failing itertools test: since revision 61118,
Mark Dickinson <dickinsm@gmail.com>
parents:
45569
diff
changeset
|
1238 self.assertRaises(TypeError, list, chain(N(s))) |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1239 self.assertRaises(ZeroDivisionError, list, chain(E(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1240 |
|
51063
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
1241 def test_compress(self): |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
1242 for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)): |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
1243 n = len(s) |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
1244 for g in (G, I, Ig, S, L, R): |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
1245 self.assertEqual(list(compress(g(s), repeat(1))), list(g(s))) |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
1246 self.assertRaises(TypeError, compress, X(s), repeat(1)) |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
1247 self.assertRaises(TypeError, compress, N(s), repeat(1)) |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
1248 self.assertRaises(ZeroDivisionError, list, compress(E(s), repeat(1))) |
|
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
1249 |
|
45435
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
1250 def test_product(self): |
|
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
1251 for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)): |
|
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
1252 self.assertRaises(TypeError, product, X(s)) |
|
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
1253 self.assertRaises(TypeError, product, N(s)) |
|
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
1254 self.assertRaises(ZeroDivisionError, product, E(s)) |
|
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44881
diff
changeset
|
1255 |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1256 def test_cycle(self): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
1257 for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)): |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1258 for g in (G, I, Ig, S, L, R): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1259 tgtlen = len(s) * 3 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1260 expected = list(g(s))*3 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1261 actual = list(islice(cycle(g(s)), tgtlen)) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1262 self.assertEqual(actual, expected) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1263 self.assertRaises(TypeError, cycle, X(s)) |
|
37737
0a13a96585c3
Fix tests that were trying to make iteration blow up, on broken iterators.
Thomas Wouters <thomas@python.org>
parents:
35636
diff
changeset
|
1264 self.assertRaises(TypeError, cycle, N(s)) |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1265 self.assertRaises(ZeroDivisionError, list, cycle(E(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1266 |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1267 def test_groupby(self): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
1268 for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)): |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1269 for g in (G, I, Ig, S, L, R): |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1270 self.assertEqual([k for k, sb in groupby(g(s))], list(g(s))) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1271 self.assertRaises(TypeError, groupby, X(s)) |
|
37737
0a13a96585c3
Fix tests that were trying to make iteration blow up, on broken iterators.
Thomas Wouters <thomas@python.org>
parents:
35636
diff
changeset
|
1272 self.assertRaises(TypeError, groupby, N(s)) |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1273 self.assertRaises(ZeroDivisionError, list, groupby(E(s))) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1274 |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1275 def test_filter(self): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
1276 for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)): |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1277 for g in (G, I, Ig, S, L, R): |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1278 self.assertEqual(list(filter(isEven, g(s))), |
|
42142
a53e48be6df9
Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1279 [x for x in g(s) if isEven(x)]) |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1280 self.assertRaises(TypeError, filter, isEven, X(s)) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1281 self.assertRaises(TypeError, filter, isEven, N(s)) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1282 self.assertRaises(ZeroDivisionError, list, filter(isEven, E(s))) |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1283 |
|
45736
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
1284 def test_filterfalse(self): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
1285 for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)): |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1286 for g in (G, I, Ig, S, L, R): |
|
45736
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
1287 self.assertEqual(list(filterfalse(isEven, g(s))), |
|
42142
a53e48be6df9
Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1288 [x for x in g(s) if isOdd(x)]) |
|
45736
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
1289 self.assertRaises(TypeError, filterfalse, isEven, X(s)) |
|
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
1290 self.assertRaises(TypeError, filterfalse, isEven, N(s)) |
|
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
1291 self.assertRaises(ZeroDivisionError, list, filterfalse(isEven, E(s))) |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1292 |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1293 def test_zip(self): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
1294 for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)): |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1295 for g in (G, I, Ig, S, L, R): |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1296 self.assertEqual(list(zip(g(s))), lzip(g(s))) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1297 self.assertEqual(list(zip(g(s), g(s))), lzip(g(s), g(s))) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1298 self.assertRaises(TypeError, zip, X(s)) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1299 self.assertRaises(TypeError, zip, N(s)) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1300 self.assertRaises(ZeroDivisionError, list, zip(E(s))) |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1301 |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1302 def test_ziplongest(self): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
1303 for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)): |
|
40893
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
1304 for g in (G, I, Ig, S, L, R): |
|
45736
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
1305 self.assertEqual(list(zip_longest(g(s))), list(zip(g(s)))) |
|
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
1306 self.assertEqual(list(zip_longest(g(s), g(s))), list(zip(g(s), g(s)))) |
|
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
1307 self.assertRaises(TypeError, zip_longest, X(s)) |
|
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
1308 self.assertRaises(TypeError, zip_longest, N(s)) |
|
7752a57f9c49
Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().
Raymond Hettinger <python@rcn.com>
parents:
45735
diff
changeset
|
1309 self.assertRaises(ZeroDivisionError, list, zip_longest(E(s))) |
|
40893
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40809
diff
changeset
|
1310 |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1311 def test_map(self): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
1312 for s in (range(10), range(0), range(100), (7,11), range(20,50,5)): |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1313 for g in (G, I, Ig, S, L, R): |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1314 self.assertEqual(list(map(onearg, g(s))), |
|
42142
a53e48be6df9
Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1315 [onearg(x) for x in g(s)]) |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1316 self.assertEqual(list(map(operator.pow, g(s), g(s))), |
|
42142
a53e48be6df9
Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1317 [x**x for x in g(s)]) |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1318 self.assertRaises(TypeError, map, onearg, X(s)) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1319 self.assertRaises(TypeError, map, onearg, N(s)) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1320 self.assertRaises(ZeroDivisionError, list, map(onearg, E(s))) |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1321 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1322 def test_islice(self): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
1323 for s in ("12345", "", range(1000), ('do', 1.2), range(2000,2200,5)): |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1324 for g in (G, I, Ig, S, L, R): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1325 self.assertEqual(list(islice(g(s),1,None,2)), list(g(s))[1::2]) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1326 self.assertRaises(TypeError, islice, X(s), 10) |
|
37737
0a13a96585c3
Fix tests that were trying to make iteration blow up, on broken iterators.
Thomas Wouters <thomas@python.org>
parents:
35636
diff
changeset
|
1327 self.assertRaises(TypeError, islice, N(s), 10) |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1328 self.assertRaises(ZeroDivisionError, list, islice(E(s), 10)) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1329 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1330 def test_starmap(self): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
1331 for s in (range(10), range(0), range(100), (7,11), range(20,50,5)): |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1332 for g in (G, I, Ig, S, L, R): |
|
39656
08005ba6ceb9
Make built-in zip() equal to itertools.izip().
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
1333 ss = lzip(s, s) |
|
42142
a53e48be6df9
Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1334 self.assertEqual(list(starmap(operator.pow, g(ss))), |
|
a53e48be6df9
Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1335 [x**x for x in g(s)]) |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1336 self.assertRaises(TypeError, starmap, operator.pow, X(ss)) |
|
37737
0a13a96585c3
Fix tests that were trying to make iteration blow up, on broken iterators.
Thomas Wouters <thomas@python.org>
parents:
35636
diff
changeset
|
1337 self.assertRaises(TypeError, starmap, operator.pow, N(ss)) |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1338 self.assertRaises(ZeroDivisionError, list, starmap(operator.pow, E(ss))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1339 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1340 def test_takewhile(self): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
1341 for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)): |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1342 for g in (G, I, Ig, S, L, R): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1343 tgt = [] |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1344 for elem in g(s): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1345 if not isEven(elem): break |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1346 tgt.append(elem) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1347 self.assertEqual(list(takewhile(isEven, g(s))), tgt) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1348 self.assertRaises(TypeError, takewhile, isEven, X(s)) |
|
37737
0a13a96585c3
Fix tests that were trying to make iteration blow up, on broken iterators.
Thomas Wouters <thomas@python.org>
parents:
35636
diff
changeset
|
1349 self.assertRaises(TypeError, takewhile, isEven, N(s)) |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1350 self.assertRaises(ZeroDivisionError, list, takewhile(isEven, E(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1351 |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1352 def test_dropwhile(self): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
1353 for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)): |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1354 for g in (G, I, Ig, S, L, R): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1355 tgt = [] |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1356 for elem in g(s): |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1357 if not tgt and isOdd(elem): continue |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1358 tgt.append(elem) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1359 self.assertEqual(list(dropwhile(isOdd, g(s))), tgt) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1360 self.assertRaises(TypeError, dropwhile, isOdd, X(s)) |
|
37737
0a13a96585c3
Fix tests that were trying to make iteration blow up, on broken iterators.
Thomas Wouters <thomas@python.org>
parents:
35636
diff
changeset
|
1361 self.assertRaises(TypeError, dropwhile, isOdd, N(s)) |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1362 self.assertRaises(ZeroDivisionError, list, dropwhile(isOdd, E(s))) |
|
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1363 |
| 30764 | 1364 def test_tee(self): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
1365 for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)): |
| 30764 | 1366 for g in (G, I, Ig, S, L, R): |
| 1367 it1, it2 = tee(g(s)) | |
| 1368 self.assertEqual(list(it1), list(g(s))) | |
| 1369 self.assertEqual(list(it2), list(g(s))) | |
| 1370 self.assertRaises(TypeError, tee, X(s)) | |
|
37737
0a13a96585c3
Fix tests that were trying to make iteration blow up, on broken iterators.
Thomas Wouters <thomas@python.org>
parents:
35636
diff
changeset
|
1371 self.assertRaises(TypeError, tee, N(s)) |
| 30764 | 1372 self.assertRaises(ZeroDivisionError, list, tee(E(s))[0]) |
| 1373 | |
|
31501
14f860b03be2
Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents:
31401
diff
changeset
|
1374 class LengthTransparency(unittest.TestCase): |
|
14f860b03be2
Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents:
31401
diff
changeset
|
1375 |
|
14f860b03be2
Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents:
31401
diff
changeset
|
1376 def test_repeat(self): |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
34814
diff
changeset
|
1377 from test.test_iterlen import len |
|
31501
14f860b03be2
Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents:
31401
diff
changeset
|
1378 self.assertEqual(len(repeat(None, 50)), 50) |
|
14f860b03be2
Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents:
31401
diff
changeset
|
1379 self.assertRaises(TypeError, len, repeat(None)) |
|
14f860b03be2
Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents:
31401
diff
changeset
|
1380 |
|
30238
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1381 class RegressionTests(unittest.TestCase): |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1382 |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1383 def test_sf_793826(self): |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1384 # Fix Armin Rigo's successful efforts to wreak havoc |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1385 |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1386 def mutatingtuple(tuple1, f, tuple2): |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1387 # this builds a tuple t which is a copy of tuple1, |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1388 # then calls f(t), then mutates t to be equal to tuple2 |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1389 # (needs len(tuple1) == len(tuple2)). |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1390 def g(value, first=[1]): |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1391 if first: |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1392 del first[:] |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
1393 f(next(z)) |
|
30238
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1394 return value |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1395 items = list(tuple2) |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1396 items[1:1] = list(tuple1) |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1397 gen = map(g, items) |
|
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1398 z = zip(*[gen]*len(tuple1)) |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
1399 next(z) |
|
30238
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1400 |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1401 def f(t): |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1402 global T |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1403 T = t |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1404 first[:] = list(T) |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1405 |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1406 first = [] |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1407 mutatingtuple((1,2,3), f, (4,5,6)) |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1408 second = list(T) |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1409 self.assertEqual(first, second) |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1410 |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1411 |
|
32010
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1412 def test_sf_950057(self): |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1413 # Make sure that chain() and cycle() catch exceptions immediately |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1414 # rather than when shifting between input sources |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1415 |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1416 def gen1(): |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1417 hist.append(0) |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1418 yield 1 |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1419 hist.append(1) |
|
32747
e4575519ec26
test_sf_950057's gen1() used an assert statement, which caused the test
Tim Peters <tim.peters@gmail.com>
parents:
32010
diff
changeset
|
1420 raise AssertionError |
|
32010
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1421 hist.append(2) |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1422 |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1423 def gen2(x): |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1424 hist.append(3) |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1425 yield 2 |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1426 hist.append(4) |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1427 if x: |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1428 raise StopIteration |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1429 |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1430 hist = [] |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1431 self.assertRaises(AssertionError, list, chain(gen1(), gen2(False))) |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1432 self.assertEqual(hist, [0,1]) |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1433 |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1434 hist = [] |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1435 self.assertRaises(AssertionError, list, chain(gen1(), gen2(True))) |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1436 self.assertEqual(hist, [0,1]) |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1437 |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1438 hist = [] |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1439 self.assertRaises(AssertionError, list, cycle(gen1())) |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1440 self.assertEqual(hist, [0,1]) |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31501
diff
changeset
|
1441 |
|
40737
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40450
diff
changeset
|
1442 class SubclassWithKwargsTest(unittest.TestCase): |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40450
diff
changeset
|
1443 def test_keywords_in_subclass(self): |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40450
diff
changeset
|
1444 # count is not subclassable... |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1445 for cls in (repeat, zip, filter, filterfalse, chain, map, |
|
51063
ad1db0ae98c6
Forward port r68941 adding itertools.compress().
Raymond Hettinger <python@rcn.com>
parents:
51062
diff
changeset
|
1446 starmap, islice, takewhile, dropwhile, cycle, compress): |
|
40737
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40450
diff
changeset
|
1447 class Subclass(cls): |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40450
diff
changeset
|
1448 def __init__(self, newarg=None, *args): |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40450
diff
changeset
|
1449 cls.__init__(self, *args) |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40450
diff
changeset
|
1450 try: |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40450
diff
changeset
|
1451 Subclass(newarg=1) |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40450
diff
changeset
|
1452 except TypeError as err: |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40450
diff
changeset
|
1453 # we expect type errors because of wrong argument count |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57525
diff
changeset
|
1454 self.assertNotIn("does not take keyword arguments", err.args[0]) |
|
40737
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40450
diff
changeset
|
1455 |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40450
diff
changeset
|
1456 |
|
29303
e3aefeba4116
Test ability to handle various type of iterators.
Raymond Hettinger <python@rcn.com>
parents:
29139
diff
changeset
|
1457 libreftest = """ Doctest for examples in the library reference: libitertools.tex |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1458 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1459 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1460 >>> amounts = [120.15, 764.05, 823.14] |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1461 >>> for checknum, amount in zip(count(1200), amounts): |
|
40785
22760d53d4da
Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
1462 ... print('Check %d is for $%.2f' % (checknum, amount)) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
1463 ... |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1464 Check 1200 is for $120.15 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1465 Check 1201 is for $764.05 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1466 Check 1202 is for $823.14 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1467 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1468 >>> import operator |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1469 >>> for cube in map(operator.pow, range(1,4), repeat(3)): |
|
40785
22760d53d4da
Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
1470 ... print(cube) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
1471 ... |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1472 1 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1473 8 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1474 27 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1475 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1476 >>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele'] |
|
29627
e1fa07bf8548
Add take() to examples. Tighten the islice() example
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
1477 >>> for name in islice(reportlines, 3, None, 2): |
|
40785
22760d53d4da
Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
1478 ... print(name.title()) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
1479 ... |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1480 Alex |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1481 Laura |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1482 Martin |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1483 Walter |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1484 Samuele |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1485 |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1486 >>> from operator import itemgetter |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1487 >>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3) |
|
40809
f340cb045bf9
- PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents:
40785
diff
changeset
|
1488 >>> di = sorted(sorted(d.items()), key=itemgetter(1)) |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1489 >>> for k, g in groupby(di, itemgetter(1)): |
|
42142
a53e48be6df9
Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1490 ... print(k, list(map(itemgetter(0), g))) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
1491 ... |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1492 1 ['a', 'c', 'e'] |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1493 2 ['b', 'd', 'f'] |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1494 3 ['g'] |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
1495 |
|
31401
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1496 # Find runs of consecutive numbers using groupby. The key to the solution |
|
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1497 # is differencing with a range so that consecutive numbers all appear in |
|
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1498 # same group. |
|
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1499 >>> data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28] |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
1500 >>> for k, g in groupby(enumerate(data), lambda t:t[0]-t[1]): |
|
42142
a53e48be6df9
Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1501 ... print(list(map(operator.itemgetter(1), g))) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
1502 ... |
|
31401
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1503 [1] |
|
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1504 [4, 5, 6] |
|
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1505 [10] |
|
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1506 [15, 16, 17, 18] |
|
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1507 [22] |
|
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1508 [25, 26, 27, 28] |
|
897de9208199
Add a Guido inspired example for groupby().
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
1509 |
|
48563
b51cd5d3795c
Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47201
diff
changeset
|
1510 >>> def take(n, iterable): |
|
b51cd5d3795c
Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47201
diff
changeset
|
1511 ... "Return first n items of the iterable as a list" |
|
b51cd5d3795c
Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47201
diff
changeset
|
1512 ... return list(islice(iterable, n)) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1513 |
|
48563
b51cd5d3795c
Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47201
diff
changeset
|
1514 >>> def enumerate(iterable, start=0): |
|
b51cd5d3795c
Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47201
diff
changeset
|
1515 ... return zip(count(start), iterable) |
|
b51cd5d3795c
Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47201
diff
changeset
|
1516 |
|
b51cd5d3795c
Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47201
diff
changeset
|
1517 >>> def tabulate(function, start=0): |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1518 ... "Return function(0), function(1), ..." |
|
48563
b51cd5d3795c
Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47201
diff
changeset
|
1519 ... return map(function, count(start)) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1520 |
|
51722
497eab79b1be
Add some cross-references to the docs. Simplify the python code equivalent for zip(). Supply an optional argument for the nth() recipe.
Raymond Hettinger <python@rcn.com>
parents:
51719
diff
changeset
|
1521 >>> def nth(iterable, n, default=None): |
|
497eab79b1be
Add some cross-references to the docs. Simplify the python code equivalent for zip(). Supply an optional argument for the nth() recipe.
Raymond Hettinger <python@rcn.com>
parents:
51719
diff
changeset
|
1522 ... "Returns the nth item or a default value" |
|
497eab79b1be
Add some cross-references to the docs. Simplify the python code equivalent for zip(). Supply an optional argument for the nth() recipe.
Raymond Hettinger <python@rcn.com>
parents:
51719
diff
changeset
|
1523 ... return next(islice(iterable, n, None), default) |
| 27769 | 1524 |
|
48563
b51cd5d3795c
Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47201
diff
changeset
|
1525 >>> def quantify(iterable, pred=bool): |
|
b51cd5d3795c
Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47201
diff
changeset
|
1526 ... "Count how many times the predicate is true" |
|
b51cd5d3795c
Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47201
diff
changeset
|
1527 ... return sum(map(pred, iterable)) |
| 27769 | 1528 |
|
48563
b51cd5d3795c
Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47201
diff
changeset
|
1529 >>> def padnone(iterable): |
|
b51cd5d3795c
Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47201
diff
changeset
|
1530 ... "Returns the sequence elements and then returns None indefinitely" |
|
b51cd5d3795c
Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47201
diff
changeset
|
1531 ... return chain(iterable, repeat(None)) |
|
30092
d4cce553478a
Re-sync doc tests with the doc updates.
Raymond Hettinger <python@rcn.com>
parents:
29690
diff
changeset
|
1532 |
|
48563
b51cd5d3795c
Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47201
diff
changeset
|
1533 >>> def ncycles(iterable, n): |
|
68585
bf94b6a73fc8
#11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66548
diff
changeset
|
1534 ... "Returns the sequence elements n times" |
|
48563
b51cd5d3795c
Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47201
diff
changeset
|
1535 ... return chain(*repeat(iterable, n)) |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1536 |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1537 >>> def dotproduct(vec1, vec2): |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1538 ... return sum(map(operator.mul, vec1, vec2)) |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1539 |
| 30764 | 1540 >>> def flatten(listOfLists): |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1541 ... return list(chain.from_iterable(listOfLists)) |
| 30764 | 1542 |
| 1543 >>> def repeatfunc(func, times=None, *args): | |
| 1544 ... "Repeat calls to func with specified arguments." | |
| 1545 ... " Example: repeatfunc(random.random)" | |
| 1546 ... if times is None: | |
| 1547 ... return starmap(func, repeat(args)) | |
| 1548 ... else: | |
| 1549 ... return starmap(func, repeat(args, times)) | |
| 1550 | |
|
30787
ea058504104c
Replace the window() example with pairwise() which demonstrates tee().
Raymond Hettinger <python@rcn.com>
parents:
30786
diff
changeset
|
1551 >>> def pairwise(iterable): |
|
ea058504104c
Replace the window() example with pairwise() which demonstrates tee().
Raymond Hettinger <python@rcn.com>
parents:
30786
diff
changeset
|
1552 ... "s -> (s0,s1), (s1,s2), (s2, s3), ..." |
|
ea058504104c
Replace the window() example with pairwise() which demonstrates tee().
Raymond Hettinger <python@rcn.com>
parents:
30786
diff
changeset
|
1553 ... a, b = tee(iterable) |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
1554 ... try: |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40893
diff
changeset
|
1555 ... next(b) |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
1556 ... except StopIteration: |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30787
diff
changeset
|
1557 ... pass |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1558 ... return zip(a, b) |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1559 |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1560 >>> def grouper(n, iterable, fillvalue=None): |
|
48584
008342519e86
Neaten-up the itertools recipes.
Raymond Hettinger <python@rcn.com>
parents:
48563
diff
changeset
|
1561 ... "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx" |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1562 ... args = [iter(iterable)] * n |
|
51103
48b7037ff20e
Beautify grouper() recipe in docs.
Raymond Hettinger <python@rcn.com>
parents:
51100
diff
changeset
|
1563 ... return zip_longest(*args, fillvalue=fillvalue) |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1564 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1565 >>> def roundrobin(*iterables): |
|
48584
008342519e86
Neaten-up the itertools recipes.
Raymond Hettinger <python@rcn.com>
parents:
48563
diff
changeset
|
1566 ... "roundrobin('ABC', 'D', 'EF') --> A D E B F C" |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1567 ... # Recipe credited to George Sakkis |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1568 ... pending = len(iterables) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1569 ... nexts = cycle(iter(it).__next__ for it in iterables) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1570 ... while pending: |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1571 ... try: |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1572 ... for next in nexts: |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1573 ... yield next() |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1574 ... except StopIteration: |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1575 ... pending -= 1 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1576 ... nexts = cycle(islice(nexts, pending)) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1577 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1578 >>> def powerset(iterable): |
|
51062
35c6937c0127
Backport r68942: update powerset() recipe.
Raymond Hettinger <python@rcn.com>
parents:
50635
diff
changeset
|
1579 ... "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)" |
|
35c6937c0127
Backport r68942: update powerset() recipe.
Raymond Hettinger <python@rcn.com>
parents:
50635
diff
changeset
|
1580 ... s = list(iterable) |
|
35c6937c0127
Backport r68942: update powerset() recipe.
Raymond Hettinger <python@rcn.com>
parents:
50635
diff
changeset
|
1581 ... return chain.from_iterable(combinations(s, r) for r in range(len(s)+1)) |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1582 |
|
50468
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1583 >>> def unique_everseen(iterable, key=None): |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1584 ... "List unique elements, preserving order. Remember all elements ever seen." |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1585 ... # unique_everseen('AAAABBBCCDAABBB') --> A B C D |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1586 ... # unique_everseen('ABBCcAD', str.lower) --> A B C D |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1587 ... seen = set() |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1588 ... seen_add = seen.add |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1589 ... if key is None: |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1590 ... for element in iterable: |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1591 ... if element not in seen: |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1592 ... seen_add(element) |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1593 ... yield element |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1594 ... else: |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1595 ... for element in iterable: |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1596 ... k = key(element) |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1597 ... if k not in seen: |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1598 ... seen_add(k) |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1599 ... yield element |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1600 |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1601 >>> def unique_justseen(iterable, key=None): |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1602 ... "List unique elements, preserving order. Remember only the element just seen." |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1603 ... # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1604 ... # unique_justseen('ABBCcAD', str.lower) --> A B C A D |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1605 ... return map(next, map(itemgetter(1), groupby(iterable, key))) |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1606 |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1607 This is not part of the examples but it tests to make sure the definitions |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1608 perform as purported. |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1609 |
|
30381
a52a074e8ae5
Add an example to address a common question of how to split iterators.
Raymond Hettinger <python@rcn.com>
parents:
30238
diff
changeset
|
1610 >>> take(10, count()) |
|
a52a074e8ae5
Add an example to address a common question of how to split iterators.
Raymond Hettinger <python@rcn.com>
parents:
30238
diff
changeset
|
1611 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
|
a52a074e8ae5
Add an example to address a common question of how to split iterators.
Raymond Hettinger <python@rcn.com>
parents:
30238
diff
changeset
|
1612 |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1613 >>> list(enumerate('abc')) |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1614 [(0, 'a'), (1, 'b'), (2, 'c')] |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1615 |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1616 >>> list(islice(tabulate(lambda x: 2*x), 4)) |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1617 [0, 2, 4, 6] |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1618 |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1619 >>> nth('abcde', 3) |
| 51320 | 1620 'd' |
| 1621 | |
| 1622 >>> nth('abcde', 9) is None | |
| 1623 True | |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1624 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
1625 >>> quantify(range(99), lambda x: x%2==0) |
|
30093
0ac4396b1894
Modified itertools.izip() to match the behavior of __builtin__.zip()
Raymond Hettinger <python@rcn.com>
parents:
30092
diff
changeset
|
1626 50 |
|
0ac4396b1894
Modified itertools.izip() to match the behavior of __builtin__.zip()
Raymond Hettinger <python@rcn.com>
parents:
30092
diff
changeset
|
1627 |
| 30764 | 1628 >>> a = [[1, 2, 3], [4, 5, 6]] |
| 1629 >>> flatten(a) | |
| 1630 [1, 2, 3, 4, 5, 6] | |
| 1631 | |
| 1632 >>> list(repeatfunc(pow, 5, 2, 3)) | |
| 1633 [8, 8, 8, 8, 8] | |
| 1634 | |
| 1635 >>> import random | |
|
45738
740b5808c57a
Move itertools izip() code to builtins as zip(). Complete the renaming.
Raymond Hettinger <python@rcn.com>
parents:
45736
diff
changeset
|
1636 >>> take(5, map(int, repeatfunc(random.random))) |
| 30764 | 1637 [0, 0, 0, 0, 0] |
| 1638 | |
|
30787
ea058504104c
Replace the window() example with pairwise() which demonstrates tee().
Raymond Hettinger <python@rcn.com>
parents:
30786
diff
changeset
|
1639 >>> list(pairwise('abcd')) |
|
ea058504104c
Replace the window() example with pairwise() which demonstrates tee().
Raymond Hettinger <python@rcn.com>
parents:
30786
diff
changeset
|
1640 [('a', 'b'), ('b', 'c'), ('c', 'd')] |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1641 |
|
30787
ea058504104c
Replace the window() example with pairwise() which demonstrates tee().
Raymond Hettinger <python@rcn.com>
parents:
30786
diff
changeset
|
1642 >>> list(pairwise([])) |
|
ea058504104c
Replace the window() example with pairwise() which demonstrates tee().
Raymond Hettinger <python@rcn.com>
parents:
30786
diff
changeset
|
1643 [] |
|
ea058504104c
Replace the window() example with pairwise() which demonstrates tee().
Raymond Hettinger <python@rcn.com>
parents:
30786
diff
changeset
|
1644 |
|
ea058504104c
Replace the window() example with pairwise() which demonstrates tee().
Raymond Hettinger <python@rcn.com>
parents:
30786
diff
changeset
|
1645 >>> list(pairwise('a')) |
| 29535 | 1646 [] |
| 1647 | |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1648 >>> list(islice(padnone('abc'), 0, 6)) |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1649 ['a', 'b', 'c', None, None, None] |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1650 |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1651 >>> list(ncycles('abc', 3)) |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1652 ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'] |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1653 |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1654 >>> dotproduct([1,2,3], [4,5,6]) |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1655 32 |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1656 |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1657 >>> list(grouper(3, 'abcdefg', 'x')) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1658 [('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')] |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1659 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1660 >>> list(roundrobin('abc', 'd', 'ef')) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1661 ['a', 'd', 'e', 'b', 'f', 'c'] |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1662 |
|
51062
35c6937c0127
Backport r68942: update powerset() recipe.
Raymond Hettinger <python@rcn.com>
parents:
50635
diff
changeset
|
1663 >>> list(powerset([1,2,3])) |
|
35c6937c0127
Backport r68942: update powerset() recipe.
Raymond Hettinger <python@rcn.com>
parents:
50635
diff
changeset
|
1664 [(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)] |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1665 |
|
51120
fc51026abde6
Add more tests for the powerset() recipe.
Raymond Hettinger <python@rcn.com>
parents:
51115
diff
changeset
|
1666 >>> all(len(list(powerset(range(n)))) == 2**n for n in range(18)) |
|
fc51026abde6
Add more tests for the powerset() recipe.
Raymond Hettinger <python@rcn.com>
parents:
51115
diff
changeset
|
1667 True |
|
fc51026abde6
Add more tests for the powerset() recipe.
Raymond Hettinger <python@rcn.com>
parents:
51115
diff
changeset
|
1668 |
|
fc51026abde6
Add more tests for the powerset() recipe.
Raymond Hettinger <python@rcn.com>
parents:
51115
diff
changeset
|
1669 >>> list(powerset('abcde')) == sorted(sorted(set(powerset('abcde'))), key=len) |
|
fc51026abde6
Add more tests for the powerset() recipe.
Raymond Hettinger <python@rcn.com>
parents:
51115
diff
changeset
|
1670 True |
|
fc51026abde6
Add more tests for the powerset() recipe.
Raymond Hettinger <python@rcn.com>
parents:
51115
diff
changeset
|
1671 |
|
50468
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1672 >>> list(unique_everseen('AAAABBBCCDAABBB')) |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1673 ['A', 'B', 'C', 'D'] |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1674 |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1675 >>> list(unique_everseen('ABBCcAD', str.lower)) |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1676 ['A', 'B', 'C', 'D'] |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1677 |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1678 >>> list(unique_justseen('AAAABBBCCDAABBB')) |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1679 ['A', 'B', 'C', 'D', 'A', 'B'] |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1680 |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1681 >>> list(unique_justseen('ABBCcAD', str.lower)) |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1682 ['A', 'B', 'C', 'A', 'D'] |
|
0e38c70b9776
Issue #4615. Document how to use itertools for de-duping.
Raymond Hettinger <python@rcn.com>
parents:
48596
diff
changeset
|
1683 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1684 """ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1685 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1686 __test__ = {'libreftest' : libreftest} |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1687 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1688 def test_main(verbose=None): |
|
30238
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
1689 test_classes = (TestBasicOps, TestVariousIteratorArgs, TestGC, |
|
40737
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40450
diff
changeset
|
1690 RegressionTests, LengthTransparency, |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45738
diff
changeset
|
1691 SubclassWithKwargsTest, TestExamples) |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
45773
diff
changeset
|
1692 support.run_unittest(*test_classes) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1693 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1694 # verify reference counting |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1695 if verbose and hasattr(sys, "gettotalrefcount"): |
| 29690 | 1696 import gc |
|
28936
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
1697 counts = [None] * 5 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
1698 for i in range(len(counts)): |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
45773
diff
changeset
|
1699 support.run_unittest(*test_classes) |
| 29690 | 1700 gc.collect() |
|
28936
2eee9655d71b
Add StopIteration tests.
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
1701 counts[i] = sys.gettotalrefcount() |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1702 print(counts) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1703 |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1704 # doctest the examples in the library reference |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
45773
diff
changeset
|
1705 support.run_doctest(sys.modules[__name__], verbose) |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
1706 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1707 if __name__ == "__main__": |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1708 test_main(verbose=True) |
