Skip to content

DIterator

Full path: schola.core.utils.dict_helpers.DIterator

Fluent wrapper for iterating and transforming nested dictionaries.

DIterator(source_dict)

Parameters

  • source_dict (dict) - Root nested mapping to traverse.

Methods

init

__init__(source_dict)

Parameters

  • source_dict (Dict)

chain

chain(other)

Chain two DIterators together. Note this function is not robust to reused intermediate keys e.g. dict(a=dict(b=2)) and dict(a=dict(c=3)) will cause errors

Parameters

  • other (DIterator)

flatten

flatten(prefix ='')

Collapse nested keys into underscore-separated leaf keys.

Parameters

  • prefix (str) - Prefix prepended to the first path segment (useful when chaining).

keys

keys()

Yield leaf keys in depth-first order.


kfilter

kfilter(func, any_key = True)

Keep subtrees whose keys satisfy func.

Parameters

  • func (callable) - Predicate on keys at the current level.

  • any_key (bool, default: True) - When True, retain a branch if any descendant key matches; when False, use inclusive-or semantics for nested branches (see implementation).


kmap

kmap(func)

Rename keys at every level using func.

Parameters

  • func (callable) - Unary function applied to each key K.

leaves

leaves()

Yield (key, value) pairs for every leaf under the current iterator.


map

map(func)

Apply func to each leaf value.

Parameters

  • func (callable) - Unary function applied to leaves.

no_prefix_flatten

no_prefix_flatten()

Flatten to leaf keys without accumulating parent prefixes.

Returns

Iterator over (str, V) leaves using only the immediate key names.


to_dict

to_dict(prune = True)

Materialize the current iterator chain back into a nested dict.

Parameters

  • prune (bool) - If True, drop empty child dicts produced while nesting.

unflatten

unflatten(flat_dict, prefix ='')

Replace leaf values by looking them up in flat_dict using flattened paths.

Parameters

  • flat_dict (dict) - Mapping from flattened key strings to replacement values.

  • prefix (str) - Prefix used when resolving keys (must match prior flatten usage).


values

values()

Yield leaf values in depth-first order.