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')
31 d[key] = inc_function(d[key])
37 def shard(d: Dict[Any, Any], size: int) -> Iterator[Dict[Any, Any]]:
39 Shards a dict into N subdicts which, together, contain all keys/values
40 from the original unsharded dict.
43 for x in range(0, len(d), size):
44 yield {key: value for (key, value) in islice(items, x, x + size)}
47 def coalesce_by_creating_list(key, v1, v2):
48 from list_utils import flatten
49 return flatten([v1, v2])
52 def coalesce_by_creating_set(key, v1, v2):
53 return set(coalesce_by_creating_list(key, v1, v2))
56 def raise_on_duplicated_keys(key, v1, v2):
57 raise Exception(f'Key {key} is duplicated in more than one input dict.')
61 inputs: Iterator[Dict[Any, Any]],
63 aggregation_function: Callable[[Any, Any], Any] = coalesce_by_creating_list
65 """Merge N dicts into one dict containing the union of all keys/values in
66 the input dicts. When keys collide, apply the aggregation_function which,
67 by default, creates a list of values. See also coalesce_by_creating_set or
68 provide a user defined aggregation_function.
70 >>> a = {'a': 1, 'b': 2}
71 >>> b = {'b': 1, 'c': 2, 'd': 3}
72 >>> c = {'c': 1, 'd': 2}
73 >>> coalesce([a, b, c])
74 {'a': 1, 'b': [1, 2], 'c': [1, 2], 'd': [2, 3]}
77 out: Dict[Any, Any] = {}
81 value = aggregation_function(key, d[key], out[key])
88 def item_with_max_value(d: Dict[Any, Any]) -> Tuple[Any, Any]:
89 """Returns the key and value with the max value in a dict.
91 >>> d = {'a': 1, 'b': 2, 'c': 3}
92 >>> item_with_max_value(d)
94 >>> item_with_max_value({})
95 Traceback (most recent call last):
97 ValueError: max() arg is an empty sequence
100 return max(d.items(), key=lambda _: _[1])
103 def item_with_min_value(d: Dict[Any, Any]) -> Tuple[Any, Any]:
104 """Returns the key and value with the min value in a dict.
106 >>> d = {'a': 1, 'b': 2, 'c': 3}
107 >>> item_with_min_value(d)
111 return min(d.items(), key=lambda _: _[1])
114 def key_with_max_value(d: Dict[Any, Any]) -> Any:
115 """Returns the key with the max value in the dict.
117 >>> d = {'a': 1, 'b': 2, 'c': 3}
118 >>> key_with_max_value(d)
122 return item_with_max_value(d)[0]
125 def key_with_min_value(d: Dict[Any, Any]) -> Any:
126 """Returns the key with the min value in the dict.
128 >>> d = {'a': 1, 'b': 2, 'c': 3}
129 >>> key_with_min_value(d)
133 return item_with_min_value(d)[0]
136 def max_value(d: Dict[Any, Any]) -> Any:
137 """Returns the maximum value in the dict.
139 >>> d = {'a': 1, 'b': 2, 'c': 3}
144 return item_with_max_value(d)[1]
147 def min_value(d: Dict[Any, Any]) -> Any:
148 """Returns the minimum value in the dict.
150 >>> d = {'a': 1, 'b': 2, 'c': 3}
155 return item_with_min_value(d)[1]
158 def max_key(d: Dict[Any, Any]) -> Any:
159 """Returns the maximum key in dict (ignoring values totally)
161 >>> d = {'a': 3, 'b': 2, 'c': 1}
169 def min_key(d: Dict[Any, Any]) -> Any:
170 """Returns the minimum key in dict (ignoring values totally)
172 >>> d = {'a': 3, 'b': 2, 'c': 1}
180 if __name__ == '__main__':