nested_dict¶
nested_dict provides dictionaries with multiple levels of nested-ness.
Class documentation¶
nested_dict.
__init__
([existing_dict | nested_level, value_type])[source]¶
Parameters:
- existing_dict – an existing
dict
to be converted into anested_dict
- nested_level – the level of nestedness in the dictionary
- value_type – the type of the values held in the dictionary
For example,
a = nested_dict(3, list) a['level 1']['level 2']['level 3'].append(1) b = nested_dict(2, int) b['level 1']['level 2']+=3If nested_level and value_type are not defined, the degree of nested-ness is not fixed. For example,
a = nested_dict() a['1']['2']['3'] = 3 a['A']['B'] = 15
nested_dict.
update
(other)¶Updates the dictionary recursively with the key/value pairs from other, overwriting existing keys. Return None.
If the nested_dict has a fixed level of nestedness and a value_type, then this is ignored for the key/value pairs from other but otherwise preserved as far as possible.
nested_dict.
iteritems_flat
()¶python 2.7 style synonym for
items_flat()
nested_dict.
items_flat
()¶iterate through values with nested keys flattened into a tuple
For example,
from nested_dict import nested_dict a = nested_dict() a['1']['2']['3'] = 3 a['A']['B'] = 15print list(a.items_flat())
Produces:
[ (('1', '2', '3'), 3), (('A', 'B'), 15) ]
nested_dict.
iterkeys_flat
()¶python 2.7 style synonym for
keys_flat()
nested_dict.
keys_flat
()¶iterate through values with nested keys flattened into a tuple
For example,
from nested_dict import nested_dict a = nested_dict() a['1']['2']['3'] = 3 a['A']['B'] = 15 print list(a.keys_flat())Produces:
[('1', '2', '3'), ('A', 'B')]
nested_dict.
itervalues_flat
()¶python 2.7 style synonym for
values_flat()
nested_dict.
values_flat
()¶iterate through values as a single list, without considering the degree of nesting
For example,
from nested_dict import nested_dict a = nested_dict() a['1']['2']['3'] = 3 a['A']['B'] = 15 print list(a.values_flat())Produces:
[3, 15]
nested_dict.
to_dict
()¶Converts the nested dictionary to a nested series of standard
dict
objectsFor example,
from nested_dict import nested_dict a = nested_dict() a['1']['2']['3'] = 3 a['A']['B'] = 15 print a.to_dict()
- Produces:
{'1': {'2': {'3': 3}}, 'A': {'B': 15}}
nested_dict.
__str__
([indent])¶The dictionary formatted as a string
Parameters: indent – The level of indentation for each nested level For example,
from nested_dict import nested_dict a = nested_dict() a['1']['2']['3'] = 3 a['A']['B'] = 15 print a print a.__str__(4)
- Produces:
{"1": {"2": {"3": 3}}, "A": {"B": 15}} { "1": { "2": { "3": 3 } }, "A": { "B": 15 } }
Acknowledgements¶
Inspired in part from ideas in: http://stackoverflow.com/questions/635483/what-is-the-best-way-to-implement-nested-dictionaries-in-python contributed by nosklo
Many thanks
Copyright¶
The code is licensed under the MIT Software License http://opensource.org/licenses/MIT
This essentially only asks that the copyright notices in this code be maintained for source distributions.