mirror of
https://github.com/stevenrobertson/cuburn.git
synced 2025-02-05 11:40:04 -05:00
Harmonize flatten and unflatten
This commit is contained in:
parent
31234b986e
commit
f156b18ce7
@ -55,7 +55,7 @@ def resolve(gdb, item):
|
|||||||
else:
|
else:
|
||||||
r = vs[-1]
|
r = vs[-1]
|
||||||
out[k] = r
|
out[k] = r
|
||||||
return unflatten(out.items())
|
return unflatten(out)
|
||||||
|
|
||||||
def _split_ref_id(s):
|
def _split_ref_id(s):
|
||||||
sp = s.split('@')
|
sp = s.split('@')
|
||||||
|
@ -32,12 +32,12 @@ def flatten(src):
|
|||||||
yield '.'.join(ctx + (k,)), v
|
yield '.'.join(ctx + (k,)), v
|
||||||
return dict(go(src))
|
return dict(go(src))
|
||||||
|
|
||||||
def unflatten(kvlist):
|
def unflatten(dct):
|
||||||
"""
|
"""
|
||||||
Given a flattened dict, return a nested dict, where every dot-separated
|
Given a flattened dict, return a nested dict, where every dot-separated
|
||||||
key is converted into a sub-dict.
|
key is converted into a sub-dict.
|
||||||
|
|
||||||
>>> (unflatten([('ab.xy.zw', 1), ('4', 5)) ==
|
>>> (unflatten({'ab.xy.zw': 1, '4': 5}) ==
|
||||||
... {'ab': {'xy': {'zw': 1}}, '4': 5})
|
... {'ab': {'xy': {'zw': 1}}, '4': 5})
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
@ -47,7 +47,7 @@ def unflatten(kvlist):
|
|||||||
else:
|
else:
|
||||||
go(d.setdefault(k[0], {}), k[1:], v)
|
go(d.setdefault(k[0], {}), k[1:], v)
|
||||||
out = {}
|
out = {}
|
||||||
for k, v in kvlist:
|
for k, v in dct.items():
|
||||||
go(out, k.split('.'), v)
|
go(out, k.split('.'), v)
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user