from datetime import datetime, timedelta, tzinfo # identity function identity = lambda x: x def human_datetime(time: datetime) -> str: return "{:%Y-%m-%d %H:%M:%S}".format(time) def human_timedelta(delta: timedelta) -> str: if delta.days == 0: days = "" elif delta.days == 1: days = "1 day, " else: days = f"{delta.days} days, " hours = delta.seconds // 3600 minutes = (delta.seconds - (hours * 3600)) // 60 return days + f"{hours:0>2}:{minutes:0>2}" # zero-pad to two digits def group_by_key(data: list, key: str) -> dict: """Groups a list of dicts by the value of their `key` into a dict of lists of dicts.""" keys = set([item[key] for item in data]) # initiate a dict with `keys` as keys and [] as values groups = dict.fromkeys(keys, []) for k in keys: groups[k] = [item for item in data if item[key] == k] return groups