Skip to content

Commit

Permalink
feat(util.collection.get_batches): Generator to create batches of a s…
Browse files Browse the repository at this point in the history
…ized collection.
  • Loading branch information
aaronmussig committed May 10, 2022
1 parent d421cae commit 8f19427
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/source/util/collection.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
**********
Collection
**********


.. autofunction:: magna.util.collection.get_file_size_fmt

16 changes: 16 additions & 0 deletions magna/util/collection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing import Sized, Iterator


def get_batches(iterable: Sized, n: int = 1) -> Iterator[Sized]:
"""Generate batches of size n from the iterable.
Args:
The iterable to split into batches.
The size of each batch.
Returns:
An iterator with each batch.
"""
n_iterable = len(iterable)
for ndx in range(0, n_iterable, n):
yield iterable[ndx:min(ndx + n, n_iterable)]

0 comments on commit 8f19427

Please sign in to comment.