Skip to content

Commit

Permalink
Ensure compatibility with Python versions <= 3.12 by providing a fall…
Browse files Browse the repository at this point in the history
…back implementation of `batched`.
  • Loading branch information
MichaelMauderer committed Mar 6, 2025
1 parent fdcda69 commit 67d13bb
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions colour_clf_io/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from __future__ import annotations

import itertools
import sys
import typing
from dataclasses import dataclass

Expand Down Expand Up @@ -51,6 +51,21 @@
"ExponentParams",
]

if sys.version_info >= (3, 12):
from itertools import batched
else:
from itertools import islice

T = typing.TypeVar("T")

def batched(iterable: typing.Iterable[T], n: int) -> typing.Iterator[tuple[T, ...]]:
if n < 1:
err = "n must be at least one"
raise ValueError(err)
it = iter(iterable)
while batch := tuple(islice(it, n)):
yield batch


@dataclass
class Array(XMLParsable, XMLWritable):
Expand Down Expand Up @@ -142,8 +157,7 @@ def to_xml(self) -> lxml.etree._Element:
else:
row_length = self.dim[-1]
text = "\n".join(
" ".join(map(str, row))
for row in itertools.batched(self.values, row_length)
" ".join(map(str, row)) for row in batched(self.values, row_length)
)
xml.text = text
return xml
Expand Down

0 comments on commit 67d13bb

Please sign in to comment.