3 from itertools import islice
4 from typing import Any, Callable, Dict, Iterator, Tuple
12 inc_function: Callable[..., Any] = lambda x: x + 1
15 Initialize a dict value (if it doesn't exist) or increments it (using the
16 inc_function, which is customizable) if it already does exist. Returns
17 True if the key already existed or False otherwise.
20 >>> init_or_inc(d, "test")
22 >>> init_or_inc(d, "test")
24 >>> init_or_inc(d, 'ing')
30 d[key] = inc_function(d[key])
36 def shard(d: Dict[Any, Any], size: int) -> Iterator[Dict[Any, Any]]:
38 Shards a dict into N subdicts which, together, contain all keys/values
39 from the original unsharded dict.
42 for x in range(0, len(d), size):
43 yield {key: value for (key, value) in islice(items, x, x + size)}
46 def coalesce_by_creating_list(key, v1, v2):
47 from list_utils import flatten
48 return flatten([v1, v2])
51 def coalesce_by_creating_set(key, v1, v2):
52 return set(coalesce_by_creating_list(key, v1, v2))
55 def raise_on_duplicated_keys(key, v1, v2):
56 raise Exception(f'Key {key} is duplicated in more than one input dict.')
60 inputs: Iterator[Dict[Any, Any]],
62 aggregation_function: Callable[[Any, Any], Any] = coalesce_by_creating_list
64 """Merge N dicts into one dict containing the union of all keys/values in
65 the input dicts. When keys collide, apply the aggregation_function which,
66 by default, creates a list of values. See also coalesce_by_creating_set or
67 provide a user defined aggregation_function.
69 >>> a = {'a': 1, 'b': 2}
70 >>> b = {'b': 1, 'c': 2, 'd': 3}
71 >>> c = {'c': 1, 'd': 2}
72 >>> coalesce([a, b, c])
73 {'a': 1, 'b': [1, 2], 'c': [1, 2], 'd': [2, 3]}
75 out: Dict[Any, Any] = {}
79 value = aggregation_function(key, d[key], out[key])
86 def item_with_max_value(d: Dict[Any, Any]) -> Tuple[Any, Any]:
87 """Returns the key and value with the max value in a dict.
89 >>> d = {'a': 1, 'b': 2, 'c': 3}
90 >>> item_with_max_value(d)
92 >>> item_with_max_value({})
93 Traceback (most recent call last):
95 ValueError: max() arg is an empty sequence
97 return max(d.items(), key=lambda _: _[1])
100 def item_with_min_value(d: Dict[Any, Any]) -> Tuple[Any, Any]:
101 """Returns the key and value with the min value in a dict.
103 >>> d = {'a': 1, 'b': 2, 'c': 3}
104 >>> item_with_min_value(d)
107 return min(d.items(), key=lambda _: _[1])
110 def key_with_max_value(d: Dict[Any, Any]) -> Any:
111 """Returns the key with the max value in the dict.
113 >>> d = {'a': 1, 'b': 2, 'c': 3}
114 >>> key_with_max_value(d)
117 return item_with_max_value(d)[0]
120 def key_with_min_value(d: Dict[Any, Any]) -> Any:
121 """Returns the key with the min value in the dict.
123 >>> d = {'a': 1, 'b': 2, 'c': 3}
124 >>> key_with_min_value(d)
127 return item_with_min_value(d)[0]
130 def max_value(d: Dict[Any, Any]) -> Any:
131 """Returns the maximum value in the dict.
133 >>> d = {'a': 1, 'b': 2, 'c': 3}
137 return item_with_max_value(d)[1]
140 def min_value(d: Dict[Any, Any]) -> Any:
141 """Returns the minimum value in the dict.
143 >>> d = {'a': 1, 'b': 2, 'c': 3}
147 return item_with_min_value(d)[1]
150 def max_key(d: Dict[Any, Any]) -> Any:
151 """Returns the maximum key in dict (ignoring values totally)
153 >>> d = {'a': 3, 'b': 2, 'c': 1}
160 def min_key(d: Dict[Any, Any]) -> Any:
161 """Returns the minimum key in dict (ignoring values totally)
163 >>> d = {'a': 3, 'b': 2, 'c': 1}
170 if __name__ == '__main__':