Skip to content

Commit 67d13bb

Browse files
Ensure compatibility with Python versions <= 3.12 by providing a fallback implementation of batched.
1 parent fdcda69 commit 67d13bb

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

colour_clf_io/elements.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from __future__ import annotations
1010

11-
import itertools
11+
import sys
1212
import typing
1313
from dataclasses import dataclass
1414

@@ -51,6 +51,21 @@
5151
"ExponentParams",
5252
]
5353

54+
if sys.version_info >= (3, 12):
55+
from itertools import batched
56+
else:
57+
from itertools import islice
58+
59+
T = typing.TypeVar("T")
60+
61+
def batched(iterable: typing.Iterable[T], n: int) -> typing.Iterator[tuple[T, ...]]:
62+
if n < 1:
63+
err = "n must be at least one"
64+
raise ValueError(err)
65+
it = iter(iterable)
66+
while batch := tuple(islice(it, n)):
67+
yield batch
68+
5469

5570
@dataclass
5671
class Array(XMLParsable, XMLWritable):
@@ -142,8 +157,7 @@ def to_xml(self) -> lxml.etree._Element:
142157
else:
143158
row_length = self.dim[-1]
144159
text = "\n".join(
145-
" ".join(map(str, row))
146-
for row in itertools.batched(self.values, row_length)
160+
" ".join(map(str, row)) for row in batched(self.values, row_length)
147161
)
148162
xml.text = text
149163
return xml

0 commit comments

Comments
 (0)