Skip to content
This repository has been archived by the owner on Feb 10, 2024. It is now read-only.

unflatten method #2

Open
pylang opened this issue Nov 6, 2019 · 0 comments
Open

unflatten method #2

pylang opened this issue Nov 6, 2019 · 0 comments

Comments

@pylang
Copy link

pylang commented Nov 6, 2019

Is your feature request related to a problem? Please describe.

You have an method to flatten nested dict.

f = FlexDict({
    "a": {
        "b": 1,
        "c": {
            "d": 1,
            "e": {
                "a": 3
            }
        }
    },
    "g": 5,
})

flat = f.flatten()
flat
# [(['a', 'b'], 1), (['a', 'c', 'd'], 1), (['a', 'c', 'e', 'a'], 3), (['g'], 5)]

There is no method to undo this and get the original tree.

Describe the solution you'd like

A method to unflatten a flattened structure.

f.unflatten(flat)
# {'a': {'b': 1, 'c': {'d': 1, 'e': {'a': 3}}}, 'g': 5}

Describe alternatives you've considered

Perhaps implement as a classmethod to serve as a secondary constructor.

f.unflatten(flat)
FlexDict.unflatten(flat)
# {'a': {'b': 1, 'c': {'d': 1, 'e': {'a': 3}}}, 'g': 5}

flat2 = [(['a'], 1), (['b', 'c'], 2), (['d'], [3])]
FlexDict.unflatten(flat2)
# {'a': 1, 'b': {'c': 2}, 'd': [3]}

Optionally include a pretty-printer.

FlexDict.unflatten(flat2, pretty=True)

Output

{
    "a": {
        "b": 1,
        "c": {
            "d": 1,
            "e": {
                "a": 3
            }
        }
    },
    "g": 5,
}

Additional context

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant