@@ -2436,6 +2436,21 @@ def test_permutations_sizeof(self):
2436
2436
... else:
2437
2437
... return starmap(func, repeat(args, times))
2438
2438
2439
+ >>> def grouper(iterable, n, *, incomplete='fill', fillvalue=None):
2440
+ ... "Collect data into non-overlapping fixed-length chunks or blocks"
2441
+ ... # grouper('ABCDEFG', 3, fillvalue='x') --> ABC DEF Gxx
2442
+ ... # grouper('ABCDEFG', 3, incomplete='strict') --> ABC DEF ValueError
2443
+ ... # grouper('ABCDEFG', 3, incomplete='ignore') --> ABC DEF
2444
+ ... args = [iter(iterable)] * n
2445
+ ... if incomplete == 'fill':
2446
+ ... return zip_longest(*args, fillvalue=fillvalue)
2447
+ ... if incomplete == 'strict':
2448
+ ... return zip(*args, strict=True)
2449
+ ... if incomplete == 'ignore':
2450
+ ... return zip(*args)
2451
+ ... else:
2452
+ ... raise ValueError('Expected fill, strict, or ignore')
2453
+
2439
2454
>>> def triplewise(iterable):
2440
2455
... "Return overlapping triplets from an iterable"
2441
2456
... # pairwise('ABCDEFG') -> ABC BCD CDE DEF EFG
@@ -2453,11 +2468,6 @@ def test_permutations_sizeof(self):
2453
2468
... window.append(x)
2454
2469
... yield tuple(window)
2455
2470
2456
- >>> def grouper(n, iterable, fillvalue=None):
2457
- ... "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
2458
- ... args = [iter(iterable)] * n
2459
- ... return zip_longest(*args, fillvalue=fillvalue)
2460
-
2461
2471
>>> def roundrobin(*iterables):
2462
2472
... "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
2463
2473
... # Recipe credited to George Sakkis
@@ -2626,9 +2636,22 @@ def test_permutations_sizeof(self):
2626
2636
>>> dotproduct([1,2,3], [4,5,6])
2627
2637
32
2628
2638
2629
- >>> list(grouper(3, 'abcdefg', 'x'))
2639
+ >>> list(grouper('abcdefg', 3, fillvalue= 'x'))
2630
2640
[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')]
2631
2641
2642
+ >>> it = grouper('abcdefg', 3, incomplete='strict')
2643
+ >>> next(it)
2644
+ ('a', 'b', 'c')
2645
+ >>> next(it)
2646
+ ('d', 'e', 'f')
2647
+ >>> next(it)
2648
+ Traceback (most recent call last):
2649
+ ...
2650
+ ValueError: zip() argument 2 is shorter than argument 1
2651
+
2652
+ >>> list(grouper('abcdefg', n=3, incomplete='ignore'))
2653
+ [('a', 'b', 'c'), ('d', 'e', 'f')]
2654
+
2632
2655
>>> list(triplewise('ABCDEFG'))
2633
2656
[('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'E'), ('D', 'E', 'F'), ('E', 'F', 'G')]
2634
2657
0 commit comments