Implement itertools.batched with a function to drop requirement of Python 3.12+ in InfluxDB demo
tisonkun opened this issue · 2 comments
tisonkun commented
demo-scene/influxdb-lineprotocol/ingest.py
Lines 27 to 31 in 2f46a3d
Ref Python Doc - https://docs.python.org/3/library/itertools.html#itertools.batched
frostming commented
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)), ())
tisonkun commented
@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.