Created
September 5, 2023 18:15
-
-
Save thehale/77734a52b1729efdf48a44165cdef3ee to your computer and use it in GitHub Desktop.
Python Batching Function with Generic Type Hints
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Iterable, TypeVar | |
T = TypeVar("T") | |
def next_x(_i: Iterable[T], x: int) -> Iterable[Iterable[T]]: | |
gen = (i for i in _i) | |
batch = [] | |
for item in gen: | |
batch.append(item) | |
if len(batch) == x: | |
yield batch | |
batch = [] | |
yield batch # len(_i) % x items |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LICENSE: MPL-2.0