Skip to content

Commit c737713

Browse files
CPython developersyouknowone
authored andcommitted
Update test_itertools from CPython 3.10.5
1 parent 5afa6f7 commit c737713

File tree

1 file changed

+103
-4
lines changed

1 file changed

+103
-4
lines changed

Lib/test/test_itertools.py

Lines changed: 103 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ def test_compress(self):
533533
next(testIntermediate)
534534
self.assertEqual(list(op(testIntermediate)), list(result2))
535535

536+
536537
def test_count(self):
537538
self.assertEqual(lzip('abc',count()), [('a', 0), ('b', 1), ('c', 2)])
538539
self.assertEqual(lzip('abc',count(3)), [('a', 3), ('b', 4), ('c', 5)])
@@ -1477,6 +1478,7 @@ def test_tee(self):
14771478
p = weakref.proxy(a)
14781479
self.assertEqual(getattr(p, '__class__'), type(b))
14791480
del a
1481+
support.gc_collect() # For PyPy or other GCs.
14801482
self.assertRaises(ReferenceError, getattr, p, '__class__')
14811483

14821484
ans = list('abc')
@@ -2435,10 +2437,37 @@ def test_permutations_sizeof(self):
24352437
... else:
24362438
... return starmap(func, repeat(args, times))
24372439
2438-
>>> def grouper(n, iterable, fillvalue=None):
2439-
... "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
2440+
>>> def grouper(iterable, n, *, incomplete='fill', fillvalue=None):
2441+
... "Collect data into non-overlapping fixed-length chunks or blocks"
2442+
... # grouper('ABCDEFG', 3, fillvalue='x') --> ABC DEF Gxx
2443+
... # grouper('ABCDEFG', 3, incomplete='strict') --> ABC DEF ValueError
2444+
... # grouper('ABCDEFG', 3, incomplete='ignore') --> ABC DEF
24402445
... args = [iter(iterable)] * n
2441-
... return zip_longest(*args, fillvalue=fillvalue)
2446+
... if incomplete == 'fill':
2447+
... return zip_longest(*args, fillvalue=fillvalue)
2448+
... if incomplete == 'strict':
2449+
... return zip(*args, strict=True)
2450+
... if incomplete == 'ignore':
2451+
... return zip(*args)
2452+
... else:
2453+
... raise ValueError('Expected fill, strict, or ignore')
2454+
2455+
>>> def triplewise(iterable):
2456+
... "Return overlapping triplets from an iterable"
2457+
... # pairwise('ABCDEFG') -> ABC BCD CDE DEF EFG
2458+
... for (a, _), (b, c) in pairwise(pairwise(iterable)):
2459+
... yield a, b, c
2460+
2461+
>>> import collections
2462+
>>> def sliding_window(iterable, n):
2463+
... # sliding_window('ABCDEFG', 4) -> ABCD BCDE CDEF DEFG
2464+
... it = iter(iterable)
2465+
... window = collections.deque(islice(it, n), maxlen=n)
2466+
... if len(window) == n:
2467+
... yield tuple(window)
2468+
... for x in it:
2469+
... window.append(x)
2470+
... yield tuple(window)
24422471
24432472
>>> def roundrobin(*iterables):
24442473
... "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
@@ -2453,6 +2482,40 @@ def test_permutations_sizeof(self):
24532482
... pending -= 1
24542483
... nexts = cycle(islice(nexts, pending))
24552484
2485+
>>> def partition(pred, iterable):
2486+
... "Use a predicate to partition entries into false entries and true entries"
2487+
... # partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
2488+
... t1, t2 = tee(iterable)
2489+
... return filterfalse(pred, t1), filter(pred, t2)
2490+
2491+
>>> def before_and_after(predicate, it):
2492+
... ''' Variant of takewhile() that allows complete
2493+
... access to the remainder of the iterator.
2494+
...
2495+
... >>> all_upper, remainder = before_and_after(str.isupper, 'ABCdEfGhI')
2496+
... >>> str.join('', all_upper)
2497+
... 'ABC'
2498+
... >>> str.join('', remainder)
2499+
... 'dEfGhI'
2500+
...
2501+
... Note that the first iterator must be fully
2502+
... consumed before the second iterator can
2503+
... generate valid results.
2504+
... '''
2505+
... it = iter(it)
2506+
... transition = []
2507+
... def true_iterator():
2508+
... for elem in it:
2509+
... if predicate(elem):
2510+
... yield elem
2511+
... else:
2512+
... transition.append(elem)
2513+
... return
2514+
... def remainder_iterator():
2515+
... yield from transition
2516+
... yield from it
2517+
... return true_iterator(), remainder_iterator()
2518+
24562519
>>> def powerset(iterable):
24572520
... "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
24582521
... s = list(iterable)
@@ -2574,12 +2637,47 @@ def test_permutations_sizeof(self):
25742637
>>> dotproduct([1,2,3], [4,5,6])
25752638
32
25762639
2577-
>>> list(grouper(3, 'abcdefg', 'x'))
2640+
>>> list(grouper('abcdefg', 3, fillvalue='x'))
25782641
[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')]
25792642
2643+
>>> it = grouper('abcdefg', 3, incomplete='strict')
2644+
>>> next(it)
2645+
('a', 'b', 'c')
2646+
>>> next(it)
2647+
('d', 'e', 'f')
2648+
>>> next(it)
2649+
Traceback (most recent call last):
2650+
...
2651+
ValueError: zip() argument 2 is shorter than argument 1
2652+
2653+
>>> list(grouper('abcdefg', n=3, incomplete='ignore'))
2654+
[('a', 'b', 'c'), ('d', 'e', 'f')]
2655+
2656+
>>> list(triplewise('ABCDEFG'))
2657+
[('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'E'), ('D', 'E', 'F'), ('E', 'F', 'G')]
2658+
2659+
>>> list(sliding_window('ABCDEFG', 4))
2660+
[('A', 'B', 'C', 'D'), ('B', 'C', 'D', 'E'), ('C', 'D', 'E', 'F'), ('D', 'E', 'F', 'G')]
2661+
25802662
>>> list(roundrobin('abc', 'd', 'ef'))
25812663
['a', 'd', 'e', 'b', 'f', 'c']
25822664
2665+
>>> def is_odd(x):
2666+
... return x % 2 == 1
2667+
2668+
>>> evens, odds = partition(is_odd, range(10))
2669+
>>> list(evens)
2670+
[0, 2, 4, 6, 8]
2671+
>>> list(odds)
2672+
[1, 3, 5, 7, 9]
2673+
2674+
>>> it = iter('ABCdEfGhI')
2675+
>>> all_upper, remainder = before_and_after(str.isupper, it)
2676+
>>> ''.join(all_upper)
2677+
'ABC'
2678+
>>> ''.join(remainder)
2679+
'dEfGhI'
2680+
25832681
>>> list(powerset([1,2,3]))
25842682
[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
25852683
@@ -2635,6 +2733,7 @@ def test_main(verbose=None):
26352733
counts[i] = sys.gettotalrefcount()
26362734
print(counts)
26372735

2736+
# doctest the examples in the library reference
26382737
support.run_doctest(sys.modules[__name__], verbose)
26392738

26402739
if __name__ == "__main__":

0 commit comments

Comments
 (0)