@@ -2436,6 +2436,21 @@ def test_permutations_sizeof(self):
24362436... else:
24372437... return starmap(func, repeat(args, times))
24382438
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+
24392454>>> def triplewise(iterable):
24402455... "Return overlapping triplets from an iterable"
24412456... # pairwise('ABCDEFG') -> ABC BCD CDE DEF EFG
@@ -2453,11 +2468,6 @@ def test_permutations_sizeof(self):
24532468... window.append(x)
24542469... yield tuple(window)
24552470
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-
24612471>>> def roundrobin(*iterables):
24622472... "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
24632473... # Recipe credited to George Sakkis
@@ -2626,9 +2636,22 @@ def test_permutations_sizeof(self):
26262636>>> dotproduct([1,2,3], [4,5,6])
2627263732
26282638
2629- >>> list(grouper(3, 'abcdefg', 'x'))
2639+ >>> list(grouper('abcdefg', 3, fillvalue= 'x'))
26302640[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')]
26312641
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+
26322655>>> list(triplewise('ABCDEFG'))
26332656[('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'E'), ('D', 'E', 'F'), ('E', 'F', 'G')]
26342657
0 commit comments