3 ms·
Nested json dictionary lookups with default fallback, for scenarios where any of the sub-dictionaries could be null or missing a key. Write parseDateTime(config
by Too 4y ago
Nested json dictionary lookups with default fallback, for scenarios where any of the sub-dictionaries could be null or missing a key. Write parseDateTime(config["a"]["b"]["c"]) safely as a oneliner. Haven't seen any language solve this in a neat way, without catching indexerror exceptions or ugly chaining of null-conditionals and empty dictionaries as fallback-values.
- hansvm 4y agoIt's not language-level, but I keep a Python class handy that wraps objects like that and has that behavior. You can also access items with config.a.b.c if desired (and if they're valid identifiers).