3 from itertools import islice
4 from typing import Any, Callable, Dict, Iterator, Tuple
12 inc_function: Callable[..., Any] = lambda x: x + 1
15 d[key] = inc_function(d[key])
21 def shard(d: Dict[Any, Any], size: int) -> Iterator[Dict[Any, Any]]:
23 for x in range(0, len(d), size):
24 yield {key: value for (key, value) in islice(items, x, x + size)}
27 def coalesce_by_creating_list(key, v1, v2):
28 from list_utils import flatten
29 return flatten([v1, v2])
32 def coalesce_by_creating_set(key, v1, v2):
33 return set(coalesce_by_creating_list(key, v1, v2))
36 def raise_on_duplicated_keys(key, v1, v2):
37 raise Exception(f'Key {key} is duplicated in more than one input dict.')
41 inputs: Iterator[Dict[Any, Any]],
43 aggregation_function: Callable[[Any, Any], Any] = coalesce_by_creating_list
45 out: Dict[Any, Any] = {}
49 value = aggregation_function(d[key], out[key])
56 def item_with_max_value(d: Dict[Any, Any]) -> Tuple[Any, Any]:
57 return max(d.items(), key=lambda _: _[1])
60 def item_with_min_value(d: Dict[Any, Any]) -> Tuple[Any, Any]:
61 return min(d.items(), key=lambda _: _[1])
64 def key_with_max_value(d: Dict[Any, Any]) -> Any:
65 return item_with_max_value(d)[0]
68 def key_with_min_value(d: Dict[Any, Any]) -> Any:
69 return item_with_min_value(d)[0]
72 def max_value(d: Dict[Any, Any]) -> Any:
73 return item_with_max_value(d)[1]
76 def min_value(d: Dict[Any, Any]) -> Any:
77 return item_with_min_value(d)[1]
80 def max_key(d: Dict[Any, Any]) -> Any:
84 def min_key(d: Dict[Any, Any]) -> Any: