FlatDict¶
FlatDict is a Python module for interacting with nested dicts as a single
level dict with delimited keys. FlatDict
supports Python 2.6+ and 3.2+.
Jump to Installation, Example Use, Class Documentation, or license.
For example:
foo = {'foo': {'bar': 'baz', 'qux': 'corge'}}
is represented as:
{'foo:bar': 'baz',
'foo:qux': 'corge'}
And can still be accessed as:
foo['foo']['bar']
and
foo['foo:bar']
Additionally, lists and tuples are also converted into dicts using enumerate().
For example:
d = {'list': ['a', 'b', 'c',]}
Will be flattened as follows:
flat = {'list:0': 'a', 'list:1': 'b', 'list:2': 'c'}
Installation¶
$ pip install flatdict
Example Use¶
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())
Class Documentation¶
FlatDict is a dict object that allows for single level, delimited key/value pair mapping of nested dictionaries.
-
class
flatdict.
FlatDict
(value=None, delimiter=None, former_type=<type 'dict'>)[source]¶ FlatDict
is a dictionary object that allows for single level, delimited key/value pair mapping of nested dictionaries. The default delimiter value is:
but can be changed in the constructor or by callingFlatDict.set_delimiter
.-
DELIMITER
= ':'¶
-
copy
()[source]¶ Return a shallow copy of the flat dictionary.
Return type: flatdict.FlatDict
-
fromkeys
(S[, v]) → New dict with keys from S and values equal to v.¶ v defaults to None.
-
get
(key, d=None)[source]¶ Return the value for key if key is in the flat dictionary, else default. If default is not given, it defaults to
None
, so that this method never raises aKeyError
.Parameters: - key (mixed) – The key to get
- d (mixed) – The default value
Return type: mixed
-
has_key
(key)[source]¶ Check to see if the flat dictionary has a specific key.
Parameters: key (mixed) – The key to check for Return type: bool
-
items
()[source]¶ Return a copy of the flat dictionary’s list of
(key, value)
pairs.Note
CPython implementation detail: Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the flat dictionary’s history of insertions and deletions.
Return type: list
-
iteritems
()[source]¶ Return an iterator over the flat dictionary’s (key, value) pairs. See the note for
FlatDict.items()
.Using
iteritems()
while adding or deleting entries in the flat dictionary may raise aRuntimeError
or fail to iterate over all entries.Return type: Iterator Raises: RuntimeError
-
iterkeys
()[source]¶ Return an iterator over the flat dictionary’s keys. See the note for
FlatDict.items()
.Using
iterkeys()
while adding or deleting entries in the flat dictionary may raise aRuntimeError
or fail to iterate over all entries.Return type: Iterator Raises: RuntimeError
-
itervalues
()[source]¶ Return an iterator over the flat dictionary’s values. See the note for
FlatDict.items()
.Using
itervalues()
while adding or deleting entries in the flat dictionary may raise aRuntimeError
or fail to iterate over all entries.Return type: Iterator Raises: RuntimeError
-
keys
()[source]¶ Return a copy of the flat dictionary’s list of keys. See the note for
FlatDict.items()
.Return type: list
-
pop
(key, default=None)[source]¶ If key is in the flat dictionary, remove it and return its value, else return default. If default is not given and key is not in the dictionary, a
KeyError
is raised.Parameters: - key (mixed) – The key name
- default (mixed) – The default value
Return type: mixed
-
popitem
() → (k, v), remove and return some (key, value) pair as a¶ 2-tuple; but raise KeyError if D is empty.
-
set_delimiter
(delimiter)[source]¶ Override the default or passed in delimiter with a new value.
Parameters: delimiter (str) – The delimiter to use
-
setdefault
(key, default)[source]¶ If key is in the flat dictionary, return its value. If not, insert key with a value of default and return default. default defaults to
None
.Parameters: - key (mixed) – The key name
- default (mixed) – The default value
Return type: mixed
-
update
(other=None, **kwargs)[source]¶ Update the flat dictionary with the key/value pairs from other, overwriting existing keys.
update()
accepts either another flat dictionary object or an iterable of key/value pairs (as tuples or other iterables of length two). If keyword arguments are specified, the flat dictionary is then updated with those key/value pairs:d.update(red=1, blue=2)
.Return type: None
-
values
()[source]¶ Return a copy of the flat dictionary’s list of values. See the note for
FlatDict.items()
.Return type: list
-
viewitems
() → a set-like object providing a view on D's items¶
-
viewkeys
() → a set-like object providing a view on D's keys¶
-
viewvalues
() → an object providing a view on D's values¶
-