Source code for util
import re
import math
import itertools
import zipfile
from io import BytesIO
[docs]
def is_non_negative_float(string, *, isnan_check):
try:
value = float(string)
if isnan_check:
return not math.isnan(value) and value >= 0.0
else:
return math.isnan(value) or value >= 0.0
except ValueError:
return False
[docs]
def trim_lines(string):
return "\n".join(
line.strip()
for line in string.splitlines()
if line != ''
)
[docs]
def createzip(csv_data: list[tuple[str, bytes]]):
buf = BytesIO()
with zipfile.ZipFile(buf, 'w', zipfile.ZIP_DEFLATED) as csv_zip:
for (csv_name, csv_bytes) in csv_data:
csv_zip.writestr(csv_name, csv_bytes)
buf.seek(0)
return buf
[docs]
def group_by_unique_name(collection):
return {
name: _one_or_error(name, group)
for (name, group) in itertools.groupby(collection, lambda c: c.name)
}
[docs]
def _one_or_error(key, iterator):
value = next(iterator)
try:
next(iterator)
# If we're here, we have more than one item
raise ValueError(f"Non-unique key: {key}")
except StopIteration:
return value