FlatDict
is a dict object that allows for single level, delimited key/value pair mapping of nested dictionaries. You can interact with FlatDict like a normal dictionary and access child dicts as you normally would or with the composited key.
For example:
is represented as:
And can still be accessed as:
and
Additionally, lists and tuples are also converted into dicts using enumerate().
For example:
Will be flattened as follows:
FlatDict
has the same methods as dict in Python 2.6. In addition, it has a FlatDict.as_dict
method which will return a pure nested dictionary from a FlatDict
value.
Documentation is available at http://flatdict.readthedocs.org
import flatdict
values = {'foo': {'bar': {'baz': 0,
'qux': 1,
'corge': 2},
'grault': {'baz': 3,
'qux': 4,
'corge': 5}},
'garply': {'foo': 0, 'bar': 1, 'baz': 2, 'qux': {'corge': 3}}}
flat = flatdict.FlatDict(values)
print flat['foo:bar:baz']
flat['test:value:key'] = 10
del flat['test']
for key in flat:
print key
for values in flat.itervalues():
print key
print repr(flat.as_dict())