gmr / flatdict

Python module for interacting with nested dicts as a single level dict with delimited keys.
https://flatdict.readthedocs.io
BSD 3-Clause "New" or "Revised" License
112 stars 32 forks source link

Add `guess_lists` option when unflattening FlatterDict #50

Open whophil opened 2 years ago

whophil commented 2 years ago

Add a guess_lists option to FlatterDict.as_dict() which, when unflattening a FlatterDict containing no information about original_types, attempts to guess which of its sub-dicts should actually be lists.

Addresses #49

A sub-dict is guessed to be a list if:

This is only a heuristic, as a dict may actually have keys satisfying the above criteria.

whophil commented 2 years ago

Thanks @gmr for your work on this project! This PR is ready for review.

whophil commented 2 years ago

Bump @gmr - sorry for the squeaky wheel treatment here.

I know it's only been a week or so, but is this a project you still have time/interest in maintaining?

Thanks again!

whophil commented 2 years ago

One more bump! @gmr

gmr commented 2 years ago

I don't understand the use case of the MR.

It looks to me like you want the .values() of a dict in certain cases, and not others?

Is the goal to turn:

foo:bar:baz:0 = 'a'
foo:bar:baz:1 = 'b'
foo.bar.baz:2 = 'c'

into:

foo:bar:baz = ['a','b','c']
whophil commented 2 years ago

Hi @gmr , that is indeed the desired behavior. This is further described in https://github.com/gmr/flatdict/issues/49, but not sure if that's any more clear.

Here is a complete snippet

import flatdict

d = {}
d['foo:bar:baz:0'] = 'a'
d['foo:bar:baz:1'] = 'b'
d['foo:bar:baz:2'] = 'c'

print(flatdict.FlatterDict(d).as_dict(guess_lists=False))
print(flatdict.FlatterDict(d).as_dict(guess_lists=True))

which outputs

{'foo': {'bar': {'baz': {'0': 'a', '1': 'b', '2': 'c'}}}}
{'foo': {'bar': {'baz': ['a', 'b', 'c']}}}

Setting guess_lists=False makes FlatterDict.as_dict() act the same as it currently does on the master branch. As you can see, values which are list-like in the FlatterDict do not come back list-like after "unflattening." With guess_lists=True, they do.

The use case: Serialize a "flatter" dictionary to JSON and deserialize it elsewhere.

ttamg commented 1 year ago

This PR is a great addition in my view and I have pulled this branch down and used in my project as this parsing into lists is exactly what was missing from flatdict.FlatterDict.

Are updates being added to this package? It seems not. Shame as flattening and unflattening dictionaries is a core capability needed in my use cases. I would consider accepting the PRs and submitting to a new pipy namespace.