GreptimeTeam / demo-scene

👾Scripts and samples to support Greptime Demos and Talks.
https://greptime.com
Apache License 2.0
25 stars 8 forks source link

Implement itertools.batched with a function to drop requirement of Python 3.12+ in InfluxDB demo #7

Closed tisonkun closed 7 months ago

tisonkun commented 7 months ago

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

frostming commented 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)), ())
tisonkun commented 7 months ago

@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.