"""Generic data utilities: Rename module.""" import math from typing import Sequence from functools import reduce def enumerate_sequence(seq: Sequence[dict]) -> Sequence[dict]: """Enumerate sequence beginning at 1""" return tuple({**item, "sequence_number": seqno} for seqno, item in enumerate(seq, start=1)) def order_by_family(items: tuple[dict, ...], family_key: str = "Family", order_key: str = "FamilyOrderId") -> list: """Order the populations by their families.""" def __family_order__(item): orderval = item[order_key] return math.inf if orderval is None else orderval def __order__(ordered, current): _key = (__family_order__(current), current[family_key]) return { **ordered, _key: ordered.get(_key, tuple()) + (current,) } return sorted(tuple(reduce(__order__, items, {}).items()), key=lambda item: item[0][0])