diff options
Diffstat (limited to 'uploader/datautils.py')
-rw-r--r-- | uploader/datautils.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/uploader/datautils.py b/uploader/datautils.py index b95a9e0..46a55c4 100644 --- a/uploader/datautils.py +++ b/uploader/datautils.py @@ -1,6 +1,13 @@ """Generic data utilities: Rename module.""" import math from functools import reduce +from typing import Union, Sequence + +def enumerate_sequence(seq: Sequence[dict], start:int = 1) -> Sequence[dict]: + """Enumerate sequence beginning at 1""" + return tuple({**item, "sequence_number": seqno} + for seqno, item in enumerate(seq, start=start)) + def order_by_family(items: tuple[dict, ...], family_key: str = "Family", @@ -19,3 +26,13 @@ def order_by_family(items: tuple[dict, ...], return sorted(tuple(reduce(__order__, items, {}).items()), key=lambda item: item[0][0]) + + +def safe_int(val: Union[str, int, float]) -> int: + """ + Convert val into an integer: if val cannot be converted, return a zero. + """ + try: + return int(val) + except ValueError: + return 0 |