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: 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('@')

View File

@ -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