3 # © Copyright 2021-2022, Scott Gasch
5 """Helper functions for dealing with dictionaries."""
7 from itertools import islice
8 from typing import Any, Callable, Dict, Iterator, List, Tuple
16 inc_function: Callable[..., Any] = lambda x: x + 1,
19 Initialize a dict value (if it doesn't exist) or increments it (using the
20 inc_function, which is customizable) if it already does exist. Returns
21 True if the key already existed or False otherwise.
24 >>> init_or_inc(d, "test")
26 >>> init_or_inc(d, "test")
28 >>> init_or_inc(d, 'ing')
35 d[key] = inc_function(d[key])
41 def shard(d: Dict[Any, Any], size: int) -> Iterator[Dict[Any, Any]]:
43 Shards a dict into N subdicts which, together, contain all keys/values
44 from the original unsharded dict.
47 for x in range(0, len(d), size):
48 yield dict(islice(items, x, x + size))
51 def coalesce_by_creating_list(_, new_value, old_value):
52 """Helper for use with :meth:`coalesce` that creates a list on
54 from list_utils import flatten
56 return flatten([new_value, old_value])
59 def coalesce_by_creating_set(key, new_value, old_value):
60 """Helper for use with :meth:`coalesce` that creates a set on
62 return set(coalesce_by_creating_list(key, new_value, old_value))
65 def coalesce_last_write_wins(_, new_value, discarded_old_value):
66 """Helper for use with :meth:`coalsce` that klobbers the old
67 with the new one on collision."""
71 def coalesce_first_write_wins(_, discarded_new_value, old_value):
72 """Helper for use with :meth:`coalsce` that preserves the old
73 value and discards the new one on collision."""
77 def raise_on_duplicated_keys(key, new_value, old_value):
78 """Helper for use with :meth:`coalesce` that raises an exception
79 when a collision is detected.
81 raise Exception(f'Key {key} is duplicated in more than one input dict.')
85 inputs: Iterator[Dict[Any, Any]],
87 aggregation_function: Callable[[Any, Any, Any], Any] = coalesce_by_creating_list,
89 """Merge N dicts into one dict containing the union of all keys /
90 values in the input dicts. When keys collide, apply the
91 aggregation_function which, by default, creates a list of values.
92 See also several other alternative functions for coalescing values:
94 * :meth:`coalesce_by_creating_set`
95 * :meth:`coalesce_first_write_wins`
96 * :meth:`coalesce_last_write_wins`
97 * :meth:`raise_on_duplicated_keys`
98 * or provive your own collision resolution code.
100 >>> a = {'a': 1, 'b': 2}
101 >>> b = {'b': 1, 'c': 2, 'd': 3}
102 >>> c = {'c': 1, 'd': 2}
103 >>> coalesce([a, b, c])
104 {'a': 1, 'b': [1, 2], 'c': [1, 2], 'd': [2, 3]}
106 >>> coalesce([a, b, c], aggregation_function=coalesce_last_write_wins)
107 {'a': 1, 'b': 1, 'c': 1, 'd': 2}
109 >>> coalesce([a, b, c], aggregation_function=raise_on_duplicated_keys)
110 Traceback (most recent call last):
112 Exception: Key b is duplicated in more than one input dict.
115 out: Dict[Any, Any] = {}
119 value = aggregation_function(key, d[key], out[key])
126 def item_with_max_value(d: Dict[Any, Any]) -> Tuple[Any, Any]:
127 """Returns the key and value of the item with the max value in a dict.
129 >>> d = {'a': 1, 'b': 2, 'c': 3}
130 >>> item_with_max_value(d)
132 >>> item_with_max_value({})
133 Traceback (most recent call last):
135 ValueError: max() arg is an empty sequence
138 return max(d.items(), key=lambda _: _[1])
141 def item_with_min_value(d: Dict[Any, Any]) -> Tuple[Any, Any]:
142 """Returns the key and value of the item with the min value in a dict.
144 >>> d = {'a': 1, 'b': 2, 'c': 3}
145 >>> item_with_min_value(d)
149 return min(d.items(), key=lambda _: _[1])
152 def key_with_max_value(d: Dict[Any, Any]) -> Any:
153 """Returns the key with the max value in the dict.
155 >>> d = {'a': 1, 'b': 2, 'c': 3}
156 >>> key_with_max_value(d)
160 return item_with_max_value(d)[0]
163 def key_with_min_value(d: Dict[Any, Any]) -> Any:
164 """Returns the key with the min value in the dict.
166 >>> d = {'a': 1, 'b': 2, 'c': 3}
167 >>> key_with_min_value(d)
171 return item_with_min_value(d)[0]
174 def max_value(d: Dict[Any, Any]) -> Any:
175 """Returns the maximum value in the dict.
177 >>> d = {'a': 1, 'b': 2, 'c': 3}
182 return item_with_max_value(d)[1]
185 def min_value(d: Dict[Any, Any]) -> Any:
186 """Returns the minimum value in the dict.
188 >>> d = {'a': 1, 'b': 2, 'c': 3}
193 return item_with_min_value(d)[1]
196 def max_key(d: Dict[Any, Any]) -> Any:
197 """Returns the maximum key in dict (ignoring values totally)
199 >>> d = {'a': 3, 'b': 2, 'c': 1}
207 def min_key(d: Dict[Any, Any]) -> Any:
208 """Returns the minimum key in dict (ignoring values totally)
210 >>> d = {'a': 3, 'b': 2, 'c': 1}
218 def parallel_lists_to_dict(keys: List[Any], values: List[Any]) -> Dict[Any, Any]:
219 """Given two parallel lists (keys and values), create and return
222 >>> k = ['name', 'phone', 'address', 'zip']
223 >>> v = ['scott', '555-1212', '123 main st.', '12345']
224 >>> parallel_lists_to_dict(k, v)
225 {'name': 'scott', 'phone': '555-1212', 'address': '123 main st.', 'zip': '12345'}
228 if len(keys) != len(values):
229 raise Exception("Parallel keys and values lists must have the same length")
230 return dict(zip(keys, values))
233 def dict_to_key_value_lists(d: Dict[Any, Any]) -> Tuple[List[Any], List[Any]]:
235 >>> d = {'name': 'scott', 'phone': '555-1212', 'address': '123 main st.', 'zip': '12345'}
236 >>> (k, v) = dict_to_key_value_lists(d)
238 ['name', 'phone', 'address', 'zip']
240 ['scott', '555-1212', '123 main st.', '12345']
243 r: Tuple[List[Any], List[Any]] = ([], [])
244 for (k, v) in d.items():
250 if __name__ == '__main__':