3 from itertools import islice
4 from typing import Any, Callable, Dict, Iterator, Tuple
11 inc_function: Callable[..., Any] = lambda x: x + 1
14 d[key] = inc_function(d[key])
20 def shard(d: Dict[Any, Any], size: int) -> Iterator[Dict[Any, Any]]:
22 for x in range(0, len(d), size):
23 yield {key: value for (key, value) in islice(items, x, x + size)}
26 def coalesce_by_creating_list(key, v1, v2):
27 from list_utils import flatten
28 return flatten([v1, v2])
31 def coalesce_by_creating_set(key, v1, v2):
32 return set(coalesce_by_creating_list(key, v1, v2))
35 def raise_on_duplicated_keys(key, v1, v2):
36 raise Exception(f'Key {key} is duplicated in more than one input dict.')
40 inputs: Iterator[Dict[Any, Any]],
42 aggregation_function: Callable[[Any, Any], Any] = coalesce_by_creating_list
44 out: Dict[Any, Any] = {}
48 value = aggregation_function(d[key], out[key])
55 def item_with_max_value(d: Dict[Any, Any]) -> Tuple[Any, Any]:
56 return max(d.items(), key=lambda _: _[1])
59 def item_with_min_value(d: Dict[Any, Any]) -> Tuple[Any, Any]:
60 return min(d.items(), key=lambda _: _[1])
63 def key_with_max_value(d: Dict[Any, Any]) -> Any:
64 return item_with_max_value(d)[0]
67 def key_with_min_value(d: Dict[Any, Any]) -> Any:
68 return item_with_min_value(d)[0]
71 def max_value(d: Dict[Any, Any]) -> Any:
72 return item_with_max_value(d)[1]
75 def min_value(d: Dict[Any, Any]) -> Any:
76 return item_with_min_value(d)[1]
79 def max_key(d: Dict[Any, Any]) -> Any:
83 def min_key(d: Dict[Any, Any]) -> Any: