Skip to content

Commit

Permalink
Define batched iterator internally to support Python 3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyGuthridge committed Jan 25, 2025
1 parent e0ad3a5 commit 38a51cc
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion transdoc/__cli/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,28 @@
Utilities for CLI application.
"""

from collections.abc import Generator, Sequence
import random
from itertools import batched
from typing import TypeVar
from colored import Fore, Style


T = TypeVar("T")


# Manually define itertools.batched for Python 3.11 support
# https://stackoverflow.com/a/8290508/6335363
def batched(iterable: Sequence[T], n=1) -> Generator[Sequence[T], None, None]:
"""
Batched iterator.
>>> list(batched([1, 2, 3, 4, 5], 2))
[[1, 2], [3, 4], [5]]
"""
for ndx in range(0, len(iterable), n):
yield iterable[ndx : min(ndx + n, len(iterable))]


# Flag colours from https://www.flagcolorcodes.com


Expand Down

0 comments on commit 38a51cc

Please sign in to comment.