[Python-checkins] [3.11] Sync the batched() recipe with the 3.12 implementation (GH-98446)

rhettinger webhook-mailer at python.org
Wed Oct 19 10:21:53 EDT 2022


https://github.com/python/cpython/commit/07cc997e00507e5cd7d242ff881ff7d7837cd817
commit: 07cc997e00507e5cd7d242ff881ff7d7837cd817
branch: 3.11
author: Raymond Hettinger <rhettinger at users.noreply.github.com>
committer: rhettinger <rhettinger at users.noreply.github.com>
date: 2022-10-19T09:21:14-05:00
summary:

[3.11] Sync the batched() recipe with the 3.12 implementation (GH-98446)

files:
M Doc/library/itertools.rst

diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index 0f295741e655..eb4c8088c771 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



More information about the Python-checkins mailing list