-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyfunc.py
90 lines (71 loc) · 2.14 KB
/
keyfunc.py
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
85
86
87
88
89
90
from yutility.ytypes import check_hints, List, Either, Any
Key = Either(str, List(str))
@check_hints
def get_subkeys(headkey: Key, all_keys: List(Key)):
if headkey == '*':
return all_keys
headkey = parse_key(headkey)
subkeys = []
for key in all_keys:
key = parse_key(key)
for keypart, headkeypart in zip(key, headkey):
if keypart != headkeypart:
break
else:
subkeys.append(key_to_string(key))
return subkeys
@check_hints
def parse_key(key: Key) -> List(str):
'''
Parses a key from specific format.
Keys are given as key1:key2:...:keyn or as an iterable [key1, key2, ..., keyn].
This function ensures it is given in list format.
'''
if isinstance(key, str):
return key.split(':')
else:
return key
@check_hints
def key_to_string(key: Key) -> str:
'''
Returns `key` in string format with key parts separated by `:`.
'''
key = parse_key(key)
return ':'.join(key)
@check_hints
def keys_to_dict(keys: List(Key), values=None) -> dict:
if values is None:
values = [None] * len(keys)
else:
assert len(values) == len(keys)
d = {}
for key, val in zip(keys, values):
key = parse_key(key)
d_ = d
for keypart in key[:-1]:
d_ = d_.setdefault(keypart, {})
d_[key[-1]] = val
return d
@check_hints
def add_to_dict(key, dictionary, value):
d_ = dictionary
for keypart in parse_key(key):
key = parse_key(key)
for keypart in key[:-1]:
d_ = d_.setdefault(keypart, {})
d_[key[-1]] = value
@check_hints
def get_from_dict(key: Key, dictionary: dict) -> Any:
for keypart in parse_key(key):
if keypart not in dictionary:
raise KeyError(f'Could not find key {key}')
dictionary = dictionary.get(keypart)
return dictionary
if __name__ == '__main__':
from pprint import pprint
keys = ['test:test1:test2', 'test:a:b', 'test:a:c']
values = [1, 2, 3]
d = keys_to_dict(keys, values)
add_to_dict('test:a:a', d, 'HLLO')
val = get_from_dict('test:a:a', d)
print(val)