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

KeyError when delimiter is in a key #8

Closed yofreke closed 6 years ago

yofreke commented 8 years ago
x = {'as_df':{'qw':123}}
y = flatdict.FlatDict(x)
y.set_delimiter('_')
{k:v for k,v in y.iteritems()}

output

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-14-d77df634c8fa> in <module>()
----> 1 {k:v for k,v in y.iteritems()}

<ipython-input-14-d77df634c8fa> in <dictcomp>(***failed resolving arguments***)
----> 1 {k:v for k,v in y.iteritems()}

/Users/Brown/env/lib/python2.7/site-packages/flatdict.pyc in iteritems(self)
    183
    184         """
--> 185         for item in self.items():
    186             yield item
    187

/Users/Brown/env/lib/python2.7/site-packages/flatdict.pyc in items(self)
    168         items = list()
    169         for key in self.keys():
--> 170             items.append((key, self.__getitem__(key)))
    171         return items
    172

/Users/Brown/env/lib/python2.7/site-packages/flatdict.pyc in __getitem__(self, key)
     51             return self._values[parent][child]
     52         else:
---> 53             raise KeyError(key)
     54
     55     def __iter__(self):

KeyError: 'as_df_qw'
yofreke commented 8 years ago

I started adding support for this on a fork: https://github.com/jsio-private/flatdict

It's not done yet (still some failing tests), but is a good deal of the way there.

gmr commented 6 years ago

It will now prevent you from changing to a delimiter that conflicts with pre-existing keys. Hopefully that's closer to what you had in mind (than reshuffling the internal structure when that condition is detected).