nest_mapping#
- class brainstate.util.nest_mapping(xs, /, *, sep=None)[source]#
Unflatten a mapping by converting tuple/string keys back to nested structure.
This is the inverse operation of
flat_mapping(). It reconstructs a nested mapping from a flattened one by interpreting tuple keys as paths in the nested structure.- Parameters:
xs (
Any) – A flattened mapping with tuple or string keys.sep (
str|None) – Separator used to split string keys into paths. Must match the separator used inflat_mapping(). If None, keys are assumed to be tuples.
- Returns:
A nested mapping reconstructed from the flattened structure.
- Return type:
Example
>>> flat_xs = { ... ('foo',): 1, ... ('bar', 'a'): 2, ... } >>> xs = nest_mapping(flat_xs) >>> xs NestedDict({'foo': 1, 'bar': {'a': 2}})
>>> # With separator >>> flat_xs_str = {'foo': 1, 'bar/a': 2} >>> nest_mapping(flat_xs_str, sep='/') NestedDict({'foo': 1, 'bar': {'a': 2}})
See also
flat_mapping(): The inverse operation that flattens a nested mapping.