1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
#!/usr/bin/env python3
from itertools import islice
from typing import Any, Callable, Dict, Iterator, Tuple
def init_or_inc(
d: Dict[Any, Any],
key: Any,
*,
init_value: Any = 1,
inc_function: Callable[..., Any] = lambda x: x + 1
) -> bool:
if key in d.keys():
d[key] = inc_function(d[key])
return True
d[key] = init_value
return False
def shard(d: Dict[Any, Any], size: int) -> Iterator[Dict[Any, Any]]:
items = d.items()
for x in range(0, len(d), size):
yield {key: value for (key, value) in islice(items, x, x + size)}
def coalesce_by_creating_list(key, v1, v2):
from list_utils import flatten
return flatten([v1, v2])
def coalesce_by_creating_set(key, v1, v2):
return set(coalesce_by_creating_list(key, v1, v2))
def raise_on_duplicated_keys(key, v1, v2):
raise Exception(f'Key {key} is duplicated in more than one input dict.')
def coalesce(
inputs: Iterator[Dict[Any, Any]],
*,
aggregation_function: Callable[[Any, Any, Any], Any] = coalesce_by_creating_list
) -> Dict[Any, Any]:
out = {}
for d in inputs:
for key in d:
if key in out:
value = aggregation_function(d[key], out[key])
else:
value = d[key]
out[key] = value
return out
def item_with_max_value(d: Dict[Any, Any]) -> Tuple[Any, Any]:
return max(d.items(), key=lambda _: _[1])
def item_with_min_value(d: Dict[Any, Any]) -> Tuple[Any, Any]:
return min(d.items(), key=lambda _: _[1])
def key_with_max_value(d: Dict[Any, Any]) -> Any:
return item_with_max_value(d)[0]
def key_with_min_value(d: Dict[Any, Any]) -> Any:
return item_with_min_value(d)[0]
def max_value(d: Dict[Any, Any]) -> Any:
return item_with_max_value(d)[1]
def min_value(d: Dict[Any, Any]) -> Any:
return item_with_min_value(d)[1]
def max_key(d: Dict[Any, Any]) -> Any:
return max(d.keys())
def min_key(d: Dict[Any, Any]) -> Any:
return min(d.keys())
|