flat_nest_pydict/dict_tools.py
def nest(flat_dict, sep=':'):
    """Returns nested dictionary. Flat dictionary must follow convention that each key is a path to nested leaf.

        example: nested_dict = {'0-0':
                                    {'1-0': val
                                     '1-1': {'2-0': 'val'}},
                                '0-1': 'val',
                                '0-2': ['val', 'val']}

                 flat = {'0-0:1-0': 'val',
                         '0-0:1-1:2-0': 'val',
                         '0-1': 'val',
                         '0-2': ['val', 'val']}"""

    def _nest(nested_dict, aggregated_key, val):
        try:
            leaf_key, aggregated_key = aggregated_key.split(sep, 1)
        except ValueError:
            leaf_key = aggregated_key
            dict_ = type(nested_dict)([(leaf_key, val)])
            nested_dict.update(dict_)
        else:
            if leaf_key not in nested_dict:
                nested_dict[leaf_key] = type(nested_dict)()
            _nest(nested_dict[leaf_key], aggregated_key, val)

    nested_dict = type(flat_dict)()
    for aggregated_key, val in flat_dict.items():
        _nest(nested_dict, aggregated_key, val)
    return nested_dict
Returns nested dictionary. Flat dictionary must follow convention that each key is a path to nested leaf.

        example: nested_dict = {'0-0':
                                    {'1-0': val
                                     '1-1': {'2-0': 'val'}},
                                '0-1': 'val',
                                '0-2': ['val', 'val']}

                 flat = {'0-0:1-0': 'val',
                         '0-0:1-1:2-0': 'val',
                         '0-1': 'val',
                         '0-2': ['val', 'val']}
Returns nested dictionary. Flat dictionary must follow convention that each key is a path to nested leaf.


Returns nested dictionary . Flat dictionary must follow convention that each key is a path to nested leaf .
def nest(flat_dict, sep=':'):
    def _nest(nested_dict, aggregated_key, val):
        try:
            leaf_key, aggregated_key = aggregated_key.split(sep, 1)
        except ValueError:
            leaf_key = aggregated_key
            dict_ = type(nested_dict)([(leaf_key, val)])
            nested_dict.update(dict_)
        else:
            if leaf_key not in nested_dict:
                nested_dict[leaf_key] = type(nested_dict)()
            _nest(nested_dict[leaf_key], aggregated_key, val)
    nested_dict = type(flat_dict)()
    for aggregated_key, val in flat_dict.items():
        _nest(nested_dict, aggregated_key, val)
    return nested_dict
def nest ( flat_dict , sep = ':' ) : def _nest ( nested_dict , aggregated_key , val ) : try : leaf_key , aggregated_key = aggregated_key . split ( sep , 1 ) except ValueError : leaf_key = aggregated_key dict_ = type ( nested_dict ) ( [ ( leaf_key , val ) ] ) nested_dict . update ( dict_ ) else : if leaf_key not in nested_dict : nested_dict [ leaf_key ] = type ( nested_dict ) ( ) _nest ( nested_dict [ leaf_key ] , aggregated_key , val ) nested_dict = type ( flat_dict ) ( ) for aggregated_key , val in flat_dict . items ( ) : _nest ( nested_dict , aggregated_key , val ) return nested_dict
Returns nested dictionary.
Returns nested dictionary .
"""Returns nested dictionary. Flat dictionary must follow convention that each key is a path to nested leaf.

        example: nested_dict = {'0-0':
                                    {'1-0': val
                                     '1-1': {'2-0': 'val'}},
                                '0-1': 'val',
                                '0-2': ['val', 'val']}

                 flat = {'0-0:1-0': 'val',
                         '0-0:1-1:2-0': 'val',
                         '0-1': 'val',
                         '0-2': ['val', 'val']}"""
flat_dict
sep