Closed olegbonar closed 3 years ago
Thanks for reporting, I'll look into this
A simple fix might look like:
def iterkv(item, parents=(), join='.'):
""" Iterate over key/values of item recursively.
:param dict item: Item to flatten
:param tuple parents: Running tuple of parent keys
"""
for key, val in item.items():
path = parents + (key,) # Assemble path parts
key = str.join(join, path) # join path parts
if isinstance(val, dict) and val:
yield from iterkv(val, path, join)
else:
yield (key, val)
Then,
list(flatsplode(
{
"test": [0, 0.0, [], "", 123]
}
))
yields
[{'test': 0}, {'test': 0.0}, {'test': []}, {'test': ''}, {'test': 123}]
Would be happy to add a fix if it's ok.
I tweaked your fix to maintain existing behavior around empty dicts, but thanks for suggesting!
Here's the fix
The test code
turns out to return:
which is not expected.
The problem line is https://github.com/amancevice/flatsplode/blob/4ff90650d736a23e8b7ac28921465eb8cbb6810a/flatsplode/flatsplode.py#L72 however I'm not yet sure why it's needed.