From 41592ffadd397465aee080b9b68b86758eb1038f Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 30 Sep 2022 12:35:33 -0500 Subject: [PATCH 1/4] Remove out of date note in a docstring --- Doc/library/itertools.rst | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index b83163560f4d9a..7ae0ef6ceabcd3 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -775,10 +775,7 @@ which incur interpreter overhead. return sum(map(pred, iterable)) def pad_none(iterable): - """Returns the sequence elements and then returns None indefinitely. - - Useful for emulating the behavior of the built-in map() function. - """ + "Returns the sequence elements and then returns None indefinitely." return chain(iterable, repeat(None)) def ncycles(iterable, n): From df8de06f2fd6d31862101cbdfa896ce03e1870d5 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 30 Sep 2022 12:40:13 -0500 Subject: [PATCH 2/4] Stronger test for sieve() edge cases --- Doc/library/itertools.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index 7ae0ef6ceabcd3..43325bef6e8847 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -1165,8 +1165,8 @@ which incur interpreter overhead. >>> list(sieve(30)) [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] - >>> small_primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59] - >>> all(list(sieve(n)) == [p for p in small_primes if p < n] for n in range(60)) + >>> small_primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] + >>> all(list(sieve(n)) == [p for p in small_primes if p < n] for n in range(101)) True >>> len(list(sieve(100))) 25 From 14ebfa36038c12c4fbbddb97a84b56d5c1e1837d Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 7 Oct 2022 01:11:43 -0500 Subject: [PATCH 3/4] Add batched() recipe --- Doc/library/itertools.rst | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index 43325bef6e8847..ffc71fa0f0d0b0 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -847,6 +847,13 @@ which incur interpreter overhead. else: raise ValueError('Expected fill, strict, or ignore') + def batched(iterable, n): + "Batch data into lists of length n. The last batch may be shorter." + # batched('ABCDEFG', 3) --> ABC DEF G + it = iter(iterable) + while (batch := list(islice(it, n))): + yield batch + def triplewise(iterable): "Return overlapping triplets from an iterable" # triplewise('ABCDEFG') --> ABC BCD CDE DEF EFG @@ -1209,6 +1216,37 @@ which incur interpreter overhead. >>> list(grouper('abcdefg', n=3, incomplete='ignore')) [('a', 'b', 'c'), ('d', 'e', 'f')] + >>> list(batched('ABCDEFG', 3)) + [['A', 'B', 'C'], ['D', 'E', 'F'], ['G']] + >>> list(batched('ABCDEF', 3)) + [['A', 'B', 'C'], ['D', 'E', 'F']] + >>> list(batched('ABCDE', 3)) + [['A', 'B', 'C'], ['D', 'E']] + >>> list(batched('ABCD', 3)) + [['A', 'B', 'C'], ['D']] + >>> list(batched('ABC', 3)) + [['A', 'B', 'C']] + >>> list(batched('AB', 3)) + [['A', 'B']] + >>> list(batched('A', 3)) + [['A']] + >>> list(batched('', 3)) + [] + >>> list(batched('ABCDEFG', 2)) + [['A', 'B'], ['C', 'D'], ['E', 'F'], ['G']] + >>> list(batched('ABCDEFG', 1)) + [['A'], ['B'], ['C'], ['D'], ['E'], ['F'], ['G']] + >>> list(batched('ABCDEFG', 0)) + [] + >>> list(batched('ABCDEFG', -1)) + [] + Traceback (most recent call last): + ... + ValueError: Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize. + >>> s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + >>> all(list(flatten(batched(s[:n], 5))) == list(s[:n]) for n in range(len(s))) + True + >>> list(triplewise('ABCDEFG')) [('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'E'), ('D', 'E', 'F'), ('E', 'F', 'G')] From 97ddf32ece5d635dfdccfeb68de2793a2aff5de0 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 7 Oct 2022 03:25:09 -0500 Subject: [PATCH 4/4] Remove spurious line from output --- Doc/library/itertools.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index ffc71fa0f0d0b0..6571114ef311f9 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -1239,7 +1239,6 @@ which incur interpreter overhead. >>> list(batched('ABCDEFG', 0)) [] >>> list(batched('ABCDEFG', -1)) - [] Traceback (most recent call last): ... ValueError: Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize.