flatdict icon indicating copy to clipboard operation
flatdict copied to clipboard

Can't flatten a nested OrderedDict

Open breaker-of-stone opened this issue 1 year ago • 0 comments

... 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)]))])]}>"

breaker-of-stone avatar Mar 16 '23 09:03 breaker-of-stone