-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
73 lines (61 loc) · 2.59 KB
/
utils.py
File metadata and controls
73 lines (61 loc) · 2.59 KB
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
def crush(nested_dict: dict | list, parent_key: str = '', sep: str = '.') -> dict:
"""Flatten a nested dict/list into a single-level dict with dotted keys.
Lists produce numeric path segments: ``{"a": [1, 2]} -> {"a.0": 1, "a.1": 2}``.
Args:
nested_dict: The nested structure to flatten.
parent_key: Prefix applied to keys at this recursion level (internal).
sep: Separator used between path segments.
Returns:
A flat dict keyed by dotted paths.
"""
items = []
if isinstance(nested_dict, list):
for i, value in enumerate(nested_dict):
new_key = f"{parent_key}{sep}{i}" if parent_key else str(i)
if isinstance(value, (dict, list)):
items.extend(crush(value, new_key, sep=sep).items())
else:
items.append((new_key, value))
elif isinstance(nested_dict, dict):
for key, value in nested_dict.items():
new_key = f"{parent_key}{sep}{key}" if parent_key else key
if isinstance(value, (dict, list)):
items.extend(crush(value, new_key, sep=sep).items())
else:
items.append((new_key, value))
return dict(items)
def construct(flat_dict: dict) -> dict:
"""Rebuild a nested dict/list from a flat dict with dotted keys.
Inverse of :func:`crush`. Numeric path segments become list indices;
non-numeric segments become dict keys.
Args:
flat_dict: Flat mapping keyed by dotted paths.
Returns:
The reconstructed nested structure.
"""
def recursive_construct(paths, value, base):
if len(paths) == 1:
if isinstance(base, list):
index = int(paths[0])
while len(base) <= index:
base.append(None)
base[index] = value
else:
base[paths[0]] = value
return
current_path, rest_paths = paths[0], paths[1:]
is_index = current_path.isdigit()
if is_index:
current_path = int(current_path)
while len(base) <= current_path:
base.append([] if rest_paths and rest_paths[0].isdigit() else {})
next_base = base[current_path]
else:
if current_path not in base:
base[current_path] = [] if rest_paths and rest_paths[0].isdigit() else {}
next_base = base[current_path]
recursive_construct(rest_paths, value, next_base)
result: dict = {}
for key, value in flat_dict.items():
recursive_construct(key.split('.'), value, result)
return result