On Tue, Apr 14, 2020 at 9:26 PM David Mertz <mertz@gnosis.cx> wrote:
I've written AttributeDict a fair number of times. Each time I write it from scratch, which is only a few lines. And I only make a silly wore about 50% of the time when I do so.
I've also written it a number of times, and never found a way to do it that I was really happy with. (In particular, converting all sub-dicts into AttributeDict is necessary to support a.b.c-style access, but if c is itself a dict, then you end up leaking AttributeDict objects into other parts of the code that might just be expecting a regular dict.) These days I've given up on that approach and use Mahmoud's 'glom' library instead: https://glom.readthedocs.io/ It has a ton of super-fancy features, but mostly I ignore those and just write stuff like 'glom(json_obj, "a.b.c")' or maybe 'glom(json_obj, "a.b.c", default=None)'. Like anything there are probably trade-offs and situations where something like AttributeDict is better, but figured I'd throw that out there as another option to consider. -n -- Nathaniel J. Smith -- https://vorpus.org