Skip to content

Commit

Permalink
add itertools batched
Browse files Browse the repository at this point in the history
  • Loading branch information
mightqxc committed Nov 7, 2024
1 parent 2de601c commit f2c4264
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pandacommon/pandautils/PandaUtils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import itertools

import pytz

Expand Down Expand Up @@ -77,3 +78,18 @@ def naive_utcfromtimestamp(timestamp: float) -> datetime.datetime:
datetime: current UTC date and time, without tzinfo
"""
return aware_utcfromtimestamp(timestamp).replace(tzinfo=None)


def batched(iterable, n, *, strict=False):
"""
Batch data from the iterable into tuples of length n. The last batch may be shorter than n
If strict is true, will raise a ValueError if the final batch is shorter than n
Note this function is for Python <= 3.11 as it mimics itertools.batched() in Python 3.13
"""
if n < 1:
raise ValueError("n must be at least one")
iterator = iter(iterable)
while batch := tuple(itertools.islice(iterator, n)):
if strict and len(batch) != n:
raise ValueError("batched(): incomplete batch")
yield batch

0 comments on commit f2c4264

Please sign in to comment.