From 07cc997e00507e5cd7d242ff881ff7d7837cd817 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 19 Oct 2022 09:21:14 -0500 Subject: [3.11] Sync the batched() recipe with the 3.12 implementation (GH-98446) --- Doc/library/itertools.rst | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index 0f29574..eb4c808 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -887,6 +887,8 @@ which incur interpreter overhead. def batched(iterable, n): "Batch data into lists of length n. The last batch may be shorter." # batched('ABCDEFG', 3) --> ABC DEF G + if n < 1: + raise ValueError('n must be at least one') it = iter(iterable) while (batch := list(islice(it, n))): yield batch @@ -1272,12 +1274,6 @@ which incur interpreter overhead. [['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 -- cgit v0.12