3 from itertools import islice
4 from typing import Any, Callable, Dict, Iterator, List, Tuple
11 inc_function: Callable[..., Any] = lambda x: x + 1
14 Initialize a dict value (if it doesn't exist) or increments it (using the
15 inc_function, which is customizable) if it already does exist. Returns
16 True if the key already existed or False otherwise.
19 >>> init_or_inc(d, "test")
21 >>> init_or_inc(d, "test")
23 >>> 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, new_value, old_value):
47 from list_utils import flatten
48 return flatten([new_value, old_value])
51 def coalesce_by_creating_set(key, new_value, old_value):
52 return set(coalesce_by_creating_list(key, new_value, old_value))
55 def coalesce_last_write_wins(key, new_value, old_value):
59 def coalesce_first_write_wins(key, new_value, old_value):
63 def raise_on_duplicated_keys(key, new_value, old_value):
64 raise Exception(f'Key {key} is duplicated in more than one input dict.')
68 inputs: Iterator[Dict[Any, Any]],
70 aggregation_function: Callable[[Any, Any], Any] = coalesce_by_creating_list
72 """Merge N dicts into one dict containing the union of all keys /
73 values in the input dicts. When keys collide, apply the
74 aggregation_function which, by default, creates a list of values.
75 See also several other alternative functions for coalescing values
76 (coalesce_by_creating_set, coalesce_first_write_wins,
77 coalesce_last_write_wins, raise_on_duplicated_keys) or provide a
78 custom helper function.
80 >>> a = {'a': 1, 'b': 2}
81 >>> b = {'b': 1, 'c': 2, 'd': 3}
82 >>> c = {'c': 1, 'd': 2}
83 >>> coalesce([a, b, c])
84 {'a': 1, 'b': [1, 2], 'c': [1, 2], 'd': [2, 3]}
86 >>> coalesce([a, b, c], aggregation_function=coalesce_last_write_wins)
87 {'a': 1, 'b': 1, 'c': 1, 'd': 2}
89 >>> coalesce([a, b, c], aggregation_function=raise_on_duplicated_keys)
90 Traceback (most recent call last):
92 Exception: Key b is duplicated in more than one input dict.
95 out: Dict[Any, Any] = {}
99 value = aggregation_function(key, d[key], out[key])
106 def item_with_max_value(d: Dict[Any, Any]) -> Tuple[Any, Any]:
107 """Returns the key and value with the max value in a dict.
109 >>> d = {'a': 1, 'b': 2, 'c': 3}
110 >>> item_with_max_value(d)
112 >>> item_with_max_value({})
113 Traceback (most recent call last):
115 ValueError: max() arg is an empty sequence
118 return max(d.items(), key=lambda _: _[1])
121 def item_with_min_value(d: Dict[Any, Any]) -> Tuple[Any, Any]:
122 """Returns the key and value with the min value in a dict.
124 >>> d = {'a': 1, 'b': 2, 'c': 3}
125 >>> item_with_min_value(d)
129 return min(d.items(), key=lambda _: _[1])
132 def key_with_max_value(d: Dict[Any, Any]) -> Any:
133 """Returns the key with the max value in the dict.
135 >>> d = {'a': 1, 'b': 2, 'c': 3}
136 >>> key_with_max_value(d)
140 return item_with_max_value(d)[0]
143 def key_with_min_value(d: Dict[Any, Any]) -> Any:
144 """Returns the key with the min value in the dict.
146 >>> d = {'a': 1, 'b': 2, 'c': 3}
147 >>> key_with_min_value(d)
151 return item_with_min_value(d)[0]
154 def max_value(d: Dict[Any, Any]) -> Any:
155 """Returns the maximum value in the dict.
157 >>> d = {'a': 1, 'b': 2, 'c': 3}
162 return item_with_max_value(d)[1]
165 def min_value(d: Dict[Any, Any]) -> Any:
166 """Returns the minimum value in the dict.
168 >>> d = {'a': 1, 'b': 2, 'c': 3}
173 return item_with_min_value(d)[1]
176 def max_key(d: Dict[Any, Any]) -> Any:
177 """Returns the maximum key in dict (ignoring values totally)
179 >>> d = {'a': 3, 'b': 2, 'c': 1}
187 def min_key(d: Dict[Any, Any]) -> Any:
188 """Returns the minimum key in dict (ignoring values totally)
190 >>> d = {'a': 3, 'b': 2, 'c': 1}
198 def parallel_lists_to_dict(keys: List[Any], values: List[Any]) -> Dict[Any, Any]:
199 """Given two parallel lists (keys and values), create and return
202 >>> k = ['name', 'phone', 'address', 'zip']
203 >>> v = ['scott', '555-1212', '123 main st.', '12345']
204 >>> parallel_lists_to_dict(k, v)
205 {'name': 'scott', 'phone': '555-1212', 'address': '123 main st.', 'zip': '12345'}
208 if len(keys) != len(values):
209 raise Exception("Parallel keys and values lists must have the same length")
210 return dict(zip(keys, values))
213 def dict_to_key_value_lists(d: Dict[Any, Any]) -> Tuple[List[Any], List[Any]]:
215 >>> d = {'name': 'scott', 'phone': '555-1212', 'address': '123 main st.', 'zip': '12345'}
216 >>> (k, v) = dict_to_key_value_lists(d)
218 ['name', 'phone', 'address', 'zip']
220 ['scott', '555-1212', '123 main st.', '12345']
224 for (k, v) in d.items():
230 if __name__ == '__main__':