Skip to content

Can't flatten a nested OrderedDict #56

Description

@breaker-of-stone

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions