♻️ Change _unflatten to operate on dict
_flatten & _unflatten should mirror each other.
This commit is contained in:
1 parent
698dcc2aa1
commit
937abb4840
2 files changed
+40
-40
No files matched your search
+36
-32
@@ -555,35 +555,9 @@ class HierarchicalChainMap:
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def _unflatten(kv_pairs, prefix=""):
|
||||
"""
|
||||
:type kv_pairs: Iterable[Tuple[str, any]]
|
||||
"""
|
||||
result = dict()
|
||||
for key, value in kv_pairs:
|
||||
if not key.startswith(prefix):
|
||||
continue
|
||||
subkeys = key[len(prefix) :].split(_CHAINMAP_SEP)
|
||||
current = result
|
||||
for subkey in subkeys[:-1]:
|
||||
if subkey not in current or current[subkey] is None:
|
||||
current[subkey] = {}
|
||||
current = current[subkey]
|
||||
current[subkeys[-1]] = value
|
||||
def _flatten(d: dict, parent_key: str = "") -> dict:
|
||||
"""Flattens a hierarchical dictionary."""
|
||||
|
||||
return result
|
||||
|
||||
def __init__(self, *maps):
|
||||
self._chainmap = ChainMap(*map(self._flatten, maps))
|
||||
|
||||
@staticmethod
|
||||
def from_layers(*layers):
|
||||
result = HierarchicalChainMap()
|
||||
result._chainmap.maps = layers
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
def _flatten(d, parent_key=""):
|
||||
if d is None:
|
||||
return {}
|
||||
|
||||
@@ -596,8 +570,26 @@ class HierarchicalChainMap:
|
||||
items.append((new_key, v))
|
||||
return dict(items)
|
||||
|
||||
def deep_dict(self):
|
||||
return self._unflatten(self._chainmap.items())
|
||||
@staticmethod
|
||||
def _unflatten(d: dict, prefix: str = "") -> dict:
|
||||
"""Unflattens a flattened dictionary."""
|
||||
|
||||
if d is None:
|
||||
return {}
|
||||
|
||||
result = dict()
|
||||
for key, value in d.items():
|
||||
if not key.startswith(prefix):
|
||||
continue
|
||||
subkeys = key[len(prefix) :].split(_CHAINMAP_SEP)
|
||||
current = result
|
||||
for subkey in subkeys[:-1]:
|
||||
if subkey not in current or current[subkey] is None:
|
||||
current[subkey] = {}
|
||||
current = current[subkey]
|
||||
current[subkeys[-1]] = value
|
||||
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
def _path_to_key(path):
|
||||
@@ -606,6 +598,18 @@ class HierarchicalChainMap:
|
||||
"""
|
||||
return _CHAINMAP_SEP.join(path)
|
||||
|
||||
@staticmethod
|
||||
def from_layers(*layers):
|
||||
result = HierarchicalChainMap()
|
||||
result._chainmap.maps = layers
|
||||
return result
|
||||
|
||||
def __init__(self, *maps):
|
||||
self._chainmap = ChainMap(*map(self._flatten, maps))
|
||||
|
||||
def deep_dict(self):
|
||||
return self._unflatten(self._chainmap)
|
||||
|
||||
def has_path(self, path, only_local=False, only_defaults=False):
|
||||
if only_defaults:
|
||||
current = self._chainmap.parents
|
||||
@@ -643,7 +647,7 @@ class HierarchicalChainMap:
|
||||
# we might be trying to grab a dict, look for children
|
||||
key = key + _CHAINMAP_SEP
|
||||
result = self._unflatten(
|
||||
((k, v) for k, v in current.items() if k.startswith(key)), prefix=key
|
||||
{k: v for k, v in current.items() if k.startswith(key)}, prefix=key
|
||||
)
|
||||
if not result:
|
||||
raise KeyError("Could not find entry for " + str(path))
|
||||
@@ -729,7 +733,7 @@ class HierarchicalChainMap:
|
||||
@property
|
||||
def top_map(self):
|
||||
"""This is the layer that is written to"""
|
||||
return self._unflatten(self._chainmap.maps[0].items())
|
||||
return self._unflatten(self._chainmap.maps[0])
|
||||
|
||||
@top_map.setter
|
||||
def top_map(self, value):
|
||||
|
||||
@@ -56,23 +56,19 @@ class ChainmapTest(unittest.TestCase):
|
||||
|
||||
@ddt.data(
|
||||
(
|
||||
[
|
||||
(_key("a"), 1),
|
||||
],
|
||||
{_key("a"): 1},
|
||||
{"a": 1},
|
||||
),
|
||||
(
|
||||
[
|
||||
(_key("a", "b"), "b"),
|
||||
],
|
||||
{_key("a", "b"): "b"},
|
||||
{"a": {"b": "b"}},
|
||||
),
|
||||
(
|
||||
[(_key("a", "b"), "b"), (_key("a", "c"), "c"), (_key("a", "d", "e"), "e")],
|
||||
{_key("a", "b"): "b", _key("a", "c"): "c", _key("a", "d", "e"): "e"},
|
||||
{"a": {"b": "b", "c": "c", "d": {"e": "e"}}},
|
||||
),
|
||||
(
|
||||
[(_key("a"), None), (_key("a", "b"), "b")],
|
||||
{_key("a"): None, _key("a", "b"): "b"},
|
||||
{"a": {"b": "b"}},
|
||||
),
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user