NestedDict / ndict

NestedDict/ndict s are designed to be light-weight wrappers around existing nested dictionaries, providing additional methods for nested operations.

class sndict.nesteddict.NestedDict(*args, **kwargs)[source]

Extension of OrderedDict that exposes operations on nested dicts

args kwargs

convert(dict_type=None, sort_keys=True, key=None, reverse=False)[source]

Convert all nested dictionaries to desired type.

dict_type: [‘ndict’, ‘dict’, ‘odict’]
Dict-type in string format
sort_keys: bool
Whether to sort keys
key: function, optional
Key function
reverse: bool, optional
Whether to sort in reverse

dict, OrderedDict or NestedDict

filter_values(criteria, filter_out=False)[source]

Filter NestedDict values by criteria.

The criteria used in the following ways, based on type:
  1. slice(None): Keep all
  2. function: Keep if function(key) is True
  3. list, set: Keep if key in list/set
  4. other: Keep if key==other
criteria: See above
Filter based on criteria
filter_out: bool
Whether to filter in or out

NestedDict

flatten(max_depth=None)[source]
Iterate over a flattened NestedDict. For each value, keys along the
DFS are accumulated as a tuple
max_depth: int, optional
Maximum depth to flatten by, i.e. the maximum key-tuple length
list
List of (key-tuple, value) pairs
flatten_keys(max_depth=None)[source]

Iterate over a flattened NestedDict, and expose only keys. Keys along the DFS are accumulated as a tuple

max_depth: int, optional
Maximum depth to flatten by, i.e. the maximum key-tuple length
list
List of key-tuples
flatten_values(max_depth=None)[source]

Iterate over a flattened NestedDict, and expose only values.

max_depth: int, optional
Maximum depth to flatten by
list
List of values
classmethod from_flat(data, dict_type='ndict')[source]

Initialize from a dict keyed by tuples Each tuple-element is taken as a key for each level in the NestedDict

data: dict, list
Dictionary keyed by tuples
dict_type: [‘ndict’, ‘dict’, ‘odict’]
Dict-type in string format, for initializing dicts at depth if they don’t exist yet

NestedDict

has_nested_key(key_list)[source]

Check if nested keys are valid

key_list: list
List of keys, one for each dict depth

bool

iterflatten(max_depth=None)[source]
Iterate over a flattened NestedDict. For each value, keys along the
DFS are accumulated as a tuple
max_depth: int, optional
Maximum depth to flatten by, i.e. the maximum key-tuple length
iterator
Iterator of (key-tuple, value) pairs
iterflatten_keys(max_depth=None)[source]

Iterate over a flattened NestedDict, and expose only keys. Keys along the DFS are accumulated as a tuple

max_depth: int, optional
Maximum depth to flatten by, i.e. the maximum key-tuple length
iterator
Iterator of key-tuples
iterflatten_values(max_depth=None)[source]

Iterate over a flattened NestedDict, and expose only values.

max_depth: int, optional
Maximum depth to flatten by
iterator
Iterator of values
ix

Indexer that allows for indexing by nested key list e.g.

my_ndict.ix[“key1”, “key2”, “key3”]

Also supports:

my_ndict.ix[“key1”]

If behavior is ambiguous, use ndict.ixkeys[key_list] and ndict[key] directly instead

Indexable

ixkeys

Indexer that allows for indexing by nested key list e.g.

my_ndict.ix[“key1”, “key2”, “key3”]

Note that for a single key, a tuple/list needs to be provided, e.g.

my_ndict.ix[“key1”,]

Indexable

map_values(val_func)[source]

Apply transformations to keys and values

val_func: function
Function to transform values

NestedDict

nested_get(key_list)[source]

Get value at depth

key_list: list
List of keys, one for each dict depth

obj

nested_set(key_list, value, dict_type='ndict')[source]

Set a value within nested dicts, creating dicts at depth if they don’t exist yet

key_list: list
List of keys, one for each dict depth
value: object
Value to set nested
dict_type: [‘ndict’, ‘dict’, ‘odict’]
Dict-type in string format, for initializing dicts at depth if they don’t exist yet
nested_setdefault(key_list, default=None, dict_type='ndict')[source]

Nested version of dict.setdefault, where a value is set only if it doesn’t already exist, creating dicts at depth if they don’t exist yet

key_list: list
List of keys, one for each dict depth
default: object
Value to set nested
dict_type: [‘ndict’, ‘dict’, ‘odict’]
Dict-type in string format, for initializing dicts at depth if they don’t exist yet
nested_update(other_dict)[source]

Nested version of dict.update. Changes NestedDict in-place

other_dict: dict
dict to update by.
sort_nested_keys(key=None, reverse=False)[source]

Sort keys in every nested dictionary

key: function, optional
Key function
reverse: bool, optional
Whether to sort in reverse

NestedDict

to_tree_string(indent=' - ', key_mode='str', val_mode='str')[source]

Returns structure of NestedDict in tree format string

indent: str
Indentation string for levels
key_mode: “type”, “str” or “repr”
How to serialize key
val_mode: “type”, “str” or “repr”
How to serialize terminal value
str