Neat. I've bem using a simpler approach for a few years now: https://github.com/rcarmo/python-utils/blob/master/core.py#L... ...but this takes that notion much further.
Box: Python dictionaries with recursive dot notation access
51–60 of 124 posts
Re: Box: Python dictionaries with recursive dot notation access
#52Just yesterday I commented that if generators could refer to themselves in generator comprehension (recursion) I could express some algorithm as a single expression.
http://stackoverflow.com/a/41617394/180464
... anyway, this just turns out to be what looks like an "addict" clone.
Re: Box: Python dictionaries with recursive dot notation access
#53A single class that: * De/serializes JSON and YAML * De/mangles, de/encodes keys * Provides automatic, expensive hashcode * Blacklists/transforms a bunch of likely keys because they conflict with reserved words * Overlays attrs (__box_heritage) * All in pretty complex code that obfuscates what you're really doing (especially to a maintainer) For the ability to avoid importing json/PyYAML and use clear key lookups? Th…
Why so negative here? I like Python a lot, and I don't write much Javascript, but one thing I wish I could do in Python is the dot notation from a dictionary. I sometimes used namedtuple as a cheap (but "immutable") class, so I can simply use dot notation when I am passing my object around my functions, instead of always stuffing the data into a dictionary.
Re: Box: Python dictionaries with recursive dot notation access
#54Earlier quoted context omitted.
Nope. It's called developer ergonomics.
The annoyance here is a feature not a bug. Deeply nested dictionaries, in my experience, are a source of infinite KeyErrors. The problem with using a dictionary as a cheap object is that it's very easy for it to be an object you weren't expecting. Don't nest dictionaries in Python!
Re: Box: Python dictionaries with recursive dot notation access
#55For all its faults, JavaScript has some pretty sensible defaults. Accessing a missing key returns what is basically its equivalent of nil. Extra parameters to function calls are simply ignored. Unsupplied parameters default to nil.
Re: Box: Python dictionaries with recursive dot notation access
#56Earlier quoted context omitted.
The annoyance here is a feature not a bug. Deeply nested dictionaries, in my experience, are a source of infinite KeyErrors. The problem with using a dictionary as a cheap object is that it's very easy for it to be an object you weren't expecting. Don't nest dictionaries in Python!
So then what are you supposed to deserialize nested data structures like JSON into? Dictionaries are the obvious "right" way, Python's annoying tooling aside.
Re: Box: Python dictionaries with recursive dot notation access
#57A single class that: * De/serializes JSON and YAML * De/mangles, de/encodes keys * Provides automatic, expensive hashcode * Blacklists/transforms a bunch of likely keys because they conflict with reserved words * Overlays attrs (__box_heritage) * All in pretty complex code that obfuscates what you're really doing (especially to a maintainer) For the ability to avoid importing json/PyYAML and use clear key lookups? Th…
Re: Box: Python dictionaries with recursive dot notation access
#58I think the real reason not to do this is one that as of this writing still hasn't been mentioned yet, which is that when using dot notation, your key names have to fit the Python grammar for identifiers, so you can't have a key "the thing" with a space, or anything else that isn't a Python identifier. So you can't just say "I can access this dictionary with dots", it has to be "I can access some keys of this dictionary with dots, but others I have to access another way". This greatly, greatly reduces its utility. There's a variety of ways of trying to address this; this package appears to try to normalize key names, which is very prone to surprising behaviors and makes it difficult to reason about what will go into what bucket, and also seriously mitigates the virtue of this entire approach because you, the programmer, must also run the normalization algorithm yourself in order to use the dot notation, which rapidly eats away the gains of typing a dot instead of two brackets and two quote characters. Rewriting keys like that is really icky.
The other problem is that the "dot" namespace, as it were, is as others have noted here used for method and property resolution, so you also end up with "dictionaries with random values they can't really contain because they're being used as method names" or "dictionaries that if you put the wrong key in them override a method" or something else like that. Also, consider the ability to subclass these things, making many of the things you might think to do to hack around this break in subclasses.
It's very superficially tempting but it has a looooot of issues that become evident over time. This isn't even a complete list.
Re: Box: Python dictionaries with recursive dot notation access
#59Earlier quoted context omitted.
Why so negative here? I like Python a lot, and I don't write much Javascript, but one thing I wish I could do in Python is the dot notation from a dictionary. I sometimes used namedtuple as a cheap (but "immutable") class, so I can simply use dot notation when I am passing my object around my functions, instead of always stuffing the data into a dictionary.
"I like Python a lot, and I don't write much Javascript, but one thing I wish I could do in Python is the dot notation from a dictionary" Why though?
a['b'] = 'c' # means i must use a dict a.b = 'c' # means must be an obj
if they were interchangeable then I could have more reusable code.
Re: Box: Python dictionaries with recursive dot notation access
#60Earlier quoted context omitted.
Saving chars is often a poor reason to design a language feature.
Nope. It's called developer ergonomics.