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
111 stars 32 forks source link

Can't flatten a nested OrderedDict #56

Open breaker-of-stone opened 1 year ago

breaker-of-stone commented 1 year ago

... or possibly any OrderedDict.

collections.OrderedDict provides one option of protecting JSON loads that contain duplicate keys, via the object_pairs_hook=OrderedDict modifier. The consequent process of parsing would be much easier if the payload could be fed through flatdict, however it passes through unchanged.

It may be that there was never an intention to support this, however I believe it could be accommodated without significant effort.

I would also point to the excellent DotMap structure, a widely used and slightly lower-friction alternative to OrderedDicts. Testing with nested DotMap output from JSON loads encounters the exact same pass-through phenomenon.

A sample result follows:

Flattened playscript is (unchanged):
<FlatDict id=4577850192 {'version': 'playlist_0.1', 'payload': [OrderedDict([('name', 'Honk It')]), OrderedDict([('configuration', OrderedDict([('console', True), ('log_level', 'TRACE'), ('devices', [OrderedDict([('device_name', 'MacBook Pro Speakers')])]), ('output', True), ('silent', False), ('blocksize', 2048)]))]), OrderedDict([('sample', OrderedDict([('file', 'honk.wav'), ('end', 0.9), ('repeats', 2), ('buffers', 20), ('start', 0)]))]), OrderedDict([('configuration', OrderedDict([('console', True), ('log_level', 'TRACE'), ('devices', [OrderedDict([('device_name', 'MacBook Pro Speakers')])]), ('output', True), ('silent', False), ('blocksize', 4096)]))]), OrderedDict([('sample', OrderedDict([('file', 'honk.wav'), ('end', 2.4), ('repeats', 1), ('buffers', 20), ('start', 0)]))])]}>"