From 3bc4daf1edc121cd633429187392227f2fa61885 Mon Sep 17 00:00:00 2001 From: Scott Gasch Date: Thu, 8 Jul 2021 19:44:27 -0700 Subject: Lots of changes. --- dict_utils.py | 52 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 19 deletions(-) (limited to 'dict_utils.py') diff --git a/dict_utils.py b/dict_utils.py index 29a5cd0..2362749 100644 --- a/dict_utils.py +++ b/dict_utils.py @@ -1,7 +1,9 @@ #!/usr/bin/env python3 from itertools import islice -from typing import Any, Callable, Dict, Iterator +from typing import Any, Callable, Dict, Iterator, Tuple + +import list_utils def init_or_inc( @@ -24,11 +26,39 @@ def shard(d: Dict[Any, Any], size: int) -> Iterator[Dict[Any, Any]]: yield {key: value for (key, value) in islice(items, x, x + size)} -def item_with_max_value(d: Dict[Any, Any]) -> Any: +def coalesce_by_creating_list(key, v1, v2): + return list_utils.flatten([v1, v2]) + + +def coalesce_by_creating_set(key, v1, v2): + return set(coalesce_by_creating_list(key, v1, v2)) + + +def raise_on_duplicated_keys(key, v1, v2): + raise Exception(f'Key {key} is duplicated in more than one input dict.') + + +def coalesce( + inputs: Iterator[Dict[Any, Any]], + *, + aggregation_function: Callable[[Any, Any, Any], Any] = coalesce_by_creating_list +) -> Dict[Any, Any]: + out = {} + for d in inputs: + for key in d: + if key in out: + value = aggregation_function(d[key], out[key]) + else: + value = d[key] + out[key] = value + return out + + +def item_with_max_value(d: Dict[Any, Any]) -> Tuple[Any, Any]: return max(d.items(), key=lambda _: _[1]) -def item_with_min_value(d: Dict[Any, Any]) -> Any: +def item_with_min_value(d: Dict[Any, Any]) -> Tuple[Any, Any]: return min(d.items(), key=lambda _: _[1]) @@ -54,19 +84,3 @@ def max_key(d: Dict[Any, Any]) -> Any: def min_key(d: Dict[Any, Any]) -> Any: return min(d.keys()) - - -def merge(a: Dict[Any, Any], b: Dict[Any, Any], path=None) -> Dict[Any, Any]: - if path is None: - path = [] - for key in b: - if key in a: - if isinstance(a[key], dict) and isinstance(b[key], dict): - merge(a[key], b[key], path + [str(key)]) - elif a[key] == b[key]: - pass - else: - raise Exception("Conflict at %s" % ".".join(path + [str(key)])) - else: - a[key] = b[key] - return a -- cgit v1.3