3 from itertools import islice
4 from typing import Any, Callable, Dict, Iterator
12 inc_function: Callable[..., Any] = lambda x: x + 1
15 d[key] = inc_function(d[key])
21 def shard(d: Dict[Any, Any], size: int) -> Iterator[Dict[Any, Any]]:
23 for x in range(0, len(d), size):
24 yield {key: value for (key, value) in islice(items, x, x + size)}
27 def item_with_max_value(d: Dict[Any, Any]) -> Any:
28 return max(d.items(), key=lambda _: _[1])
31 def item_with_min_value(d: Dict[Any, Any]) -> Any:
32 return min(d.items(), key=lambda _: _[1])
35 def key_with_max_value(d: Dict[Any, Any]) -> Any:
36 return item_with_max_value(d)[0]
39 def key_with_min_value(d: Dict[Any, Any]) -> Any:
40 return item_with_min_value(d)[0]
43 def max_value(d: Dict[Any, Any]) -> Any:
44 return item_with_max_value(d)[1]
47 def min_value(d: Dict[Any, Any]) -> Any:
48 return item_with_min_value(d)[1]
51 def max_key(d: Dict[Any, Any]) -> Any:
55 def min_key(d: Dict[Any, Any]) -> Any:
59 def merge(a: Dict[Any, Any], b: Dict[Any, Any], path=None) -> Dict[Any, Any]:
64 if isinstance(a[key], dict) and isinstance(b[key], dict):
65 merge(a[key], b[key], path + [str(key)])
66 elif a[key] == b[key]:
69 raise Exception("Conflict at %s" % ".".join(path + [str(key)]))