Skip to content

Instantly share code, notes, and snippets.

@ZekunZh
Created February 16, 2022 10:05
Show Gist options
  • Select an option

  • Save ZekunZh/7b2dac1df61f1ba1f4262ab4ceabee5d to your computer and use it in GitHub Desktop.

Select an option

Save ZekunZh/7b2dac1df61f1ba1f4262ab4ceabee5d to your computer and use it in GitHub Desktop.

Revisions

  1. ZekunZh created this gist Feb 16, 2022.
    18 changes: 18 additions & 0 deletions iter_batches.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    def iter_batches(iterable, batch_size):
    """Iterates over the given iterable in batches.
    Args:
    iterable: an iterable
    batch_size: the desired batch size, or None to return the contents in
    a single batch
    Returns:
    a generator that emits tuples of elements of the requested batch size
    from the input
    """
    it = iter(iterable)
    while True:
    chunk = tuple(itertools.islice(it, batch_size))
    if not chunk:
    return
    yield chunk