Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Neaten-up
  • Loading branch information
rhettinger committed Oct 17, 2022
commit 4c25341c56b8f3a9eb7df55e1f4964e5b6328522
2 changes: 1 addition & 1 deletion Doc/library/itertools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ loops that truncate the stream.
"Batch data into lists of length n. The last batch may be shorter."
# batched('ABCDEFG', 3) --> ABC DEF G
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe tweak the example so it cannot be misunderstood as "return 3 batches"?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# batched('ABCDEFG', 3) --> ABC DEF G
# list(batched('ABCDEFG', 3)) --> [['A', 'B', 'C'], ['D', 'E', 'F'], ['G']]

if n < 1:
raise ValueError('n must be >= 1')
raise ValueError('n must be at least one')
it = iter(iterable)
while (batch := list(islice(it, n))):
yield batch
Expand Down
8 changes: 6 additions & 2 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ static PyTypeObject pairwise_type;

typedef struct {
PyObject_HEAD
Py_ssize_t batch_size;
PyObject *it;
Py_ssize_t batch_size;
} batchedobject;

/*[clinic input]
Expand Down Expand Up @@ -98,7 +98,11 @@ batched_new_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t n)
batchedobject *bo;

if (n < 1) {
PyErr_SetString(PyExc_ValueError, "n must be >= 1");
/* We could define the n==0 case to return an empty iterator
but that is add odds with the idea that batching should
never throw-away input data.
*/
PyErr_SetString(PyExc_ValueError, "n must be at least one");
return NULL;
}
it = PyObject_GetIter(iterable);
Expand Down