Closed tisonkun closed 7 months ago
I realize that there may be more than one solutions in pure Python, and you can choose the best one. Let me start with this:
def batched(iterable: Iterable[T], n: int) -> Iterator[tuple[T, ...]]:
iter_ = iter(iterable)
while True:
batch = tuple(itertools.islice(iter_, n))
if not batch:
break
yield batch
To make it shorter(but not recommended):
def batched(iterable: Iterable[T], n: int) -> Iterator[tuple[T, ...]]:
iter_ = iter(iterable)
return iter(lambda: tuple(itertools.islice(iter_, n)), ())
@frostming Let's rocks :D
Would you send a patch directly or I'd do you a favor to convey this function ...
BTW, I just noticed that I don't add type checks here. And I ever thought of using PDM to build venv and deps but I rush into a working demo first.
https://github.com/GreptimeTeam/demo-scene/blob/2f46a3dc673bf6020ccb8b561ff586925fa67dad/influxdb-lineprotocol/ingest.py#L27-L31
Ref Python Doc - https://docs.python.org/3/library/itertools.html#itertools.batched