]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] Sync the batched() recipe with the 3.12 implementation (GH-98446)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Wed, 19 Oct 2022 14:21:14 +0000 (09:21 -0500)
committerGitHub <noreply@github.com>
Wed, 19 Oct 2022 14:21:14 +0000 (09:21 -0500)
Doc/library/itertools.rst

index 0f295741e655fb360a64521a85f85e68b6d42f71..eb4c8088c771a1dc250781349dbec26d6a1aecde 100644 (file)
@@ -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