|
|
Subscribe / Log in / New account

Improving Python's SimpleNamespace

Improving Python's SimpleNamespace

Posted May 3, 2020 14:35 UTC (Sun) by kleptog (subscriber, #1183)
In reply to: Improving Python's SimpleNamespace by dtlin
Parent article: Improving Python's SimpleNamespace

When dealing with complex data structures from a database or client it's useful to have a kind of "deep get" like you have, but one that gracefully handles missing entries well. Otherwise you end up with horrors like:

a.get('foo', {}).get('bar', {}).get('baz', None)
Ideally you'd like something that also handled arrays, but Python doesn't have an easy way to index an array with a default when you go off the end.

I've often ended up coding methods to do this, but it'd be cool if there was something standard.


to post comments

Improving Python's SimpleNamespace

Posted May 7, 2020 5:45 UTC (Thu) by njs (subscriber, #40338) [Link]

This kind of stuff is exactly what the 'glom' package (linked in the article) is all about.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds