Harmonize flatten and unflatten

This commit is contained in:
Steven Robertson 2012-04-16 01:31:45 -07:00
parent 31234b986e
commit f156b18ce7
2 changed files with 4 additions and 4 deletions

View File

@ -55,7 +55,7 @@ def resolve(gdb, item):
else:
r = vs[-1]
out[k] = r
return unflatten(out.items())
return unflatten(out)
def _split_ref_id(s):
sp = s.split('@')

View File

@ -32,12 +32,12 @@ def flatten(src):
yield '.'.join(ctx + (k,)), v
return dict(go(src))
def unflatten(kvlist):
def unflatten(dct):
"""
Given a flattened dict, return a nested dict, where every dot-separated
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})
True
"""
@ -47,7 +47,7 @@ def unflatten(kvlist):
else:
go(d.setdefault(k[0], {}), k[1:], v)
out = {}
for k, v in kvlist:
for k, v in dct.items():
go(out, k.split('.'), v)
return out