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

casting FlatDict to dict results in empty dict #6

Closed lebedov closed 6 years ago

lebedov commented 8 years ago

While investigating how to save the flattened dictionary computed by FlatDict, I noticed that the result of casting a FlatDict to a dict is always empty. Not sure if this is was by design, but wouldn't it be more appropriate for a dict cast of a FlatDict to return what dict(f.iteritems()) returns below?

>>> import flatdict
>>> a = {'a': 1, 'b': {'x': 2, 'y': 3}}
>>> f = flatdict.FlatDict(a)
>>> print dict(f)
{}
>>> print dict(f.iteritems())
{'a': 1, 'b:x': 2, 'b:y': 3}

Relatedly, it would be nice to add a FlatDict.as_flat_dict() method that would return the flattened dict (rather than the original unflattened dict).

I'm using flatdict 1.2.0 with Python 2.7.11 on Ubuntu 14.04.3.

gmr commented 6 years ago

Thanks for the issue and sorry for the long delay. I just pushed a cleanup of the library to master and will release v2.0.0 soon. I added a test case for your expected behavior.