3 from itertools import islice
4 from typing import Any, Callable, Dict, Iterator, Tuple
14 inc_function: Callable[..., Any] = lambda x: x + 1
17 d[key] = inc_function(d[key])
23 def shard(d: Dict[Any, Any], size: int) -> Iterator[Dict[Any, Any]]:
25 for x in range(0, len(d), size):
26 yield {key: value for (key, value) in islice(items, x, x + size)}
29 def coalesce_by_creating_list(key, v1, v2):
30 return list_utils.flatten([v1, v2])
33 def coalesce_by_creating_set(key, v1, v2):
34 return set(coalesce_by_creating_list(key, v1, v2))
37 def raise_on_duplicated_keys(key, v1, v2):
38 raise Exception(f'Key {key} is duplicated in more than one input dict.')
42 inputs: Iterator[Dict[Any, Any]],
44 aggregation_function: Callable[[Any, Any, Any], Any] = coalesce_by_creating_list
50 value = aggregation_function(d[key], out[key])
57 def item_with_max_value(d: Dict[Any, Any]) -> Tuple[Any, Any]:
58 return max(d.items(), key=lambda _: _[1])
61 def item_with_min_value(d: Dict[Any, Any]) -> Tuple[Any, Any]:
62 return min(d.items(), key=lambda _: _[1])
65 def key_with_max_value(d: Dict[Any, Any]) -> Any:
66 return item_with_max_value(d)[0]
69 def key_with_min_value(d: Dict[Any, Any]) -> Any:
70 return item_with_min_value(d)[0]
73 def max_value(d: Dict[Any, Any]) -> Any:
74 return item_with_max_value(d)[1]
77 def min_value(d: Dict[Any, Any]) -> Any:
78 return item_with_min_value(d)[1]
81 def max_key(d: Dict[Any, Any]) -> Any:
85 def min_key(d: Dict[Any, Any]) -> Any: