I think every Python programmer implements this at some point. I 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 w…
Author here: I totally agree with the fact it has shortcomings and definitely isn't aimed to replace everyone's dict everywhere. I actually created the first iteration of this around 3 years ago, aimed primarily for sysadmins and others who use the console more than actual developing. As I find it extremely convenient to tab complete keys and a lot less typing. From there it has obviously evolved to try and fit more…
Box: Python dictionaries with recursive dot notation access
81–90 of 124 posts
Re: Box: Python dictionaries with recursive dot notation access
#82I think every Python programmer implements this at some point. I 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 w…
Author here: I totally agree with the fact it has shortcomings and definitely isn't aimed to replace everyone's dict everywhere. I actually created the first iteration of this around 3 years ago, aimed primarily for sysadmins and others who use the console more than actual developing. As I find it extremely convenient to tab complete keys and a lot less typing. From there it has obviously evolved to try and fit more…
Re: Box: Python dictionaries with recursive dot notation access
#83I think every Python programmer implements this at some point. I 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 w…
Author here: I totally agree with the fact it has shortcomings and definitely isn't aimed to replace everyone's dict everywhere. I actually created the first iteration of this around 3 years ago, aimed primarily for sysadmins and others who use the console more than actual developing. As I find it extremely convenient to tab complete keys and a lot less typing. From there it has obviously evolved to try and fit more…
Re: Box: Python dictionaries with recursive dot notation access
#84Re: Box: Python dictionaries with recursive dot notation access
#85I think every Python programmer implements this at some point. I 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 w…
Agreed. This is not the sort of code you want to be dealing with in a few years time. Also, I seldom find that string based dicts are what I need. One of the best things about Python is being able to use virtually anything as a key.
I find that to be the thing one of the worst thing ever made into a programming language.
You are facing a program/API/library and you can have absolutely no clue of what it will give or expect without extensive reverse engineering.
Re: Box: Python dictionaries with recursive dot notation access
#86I think every Python programmer implements this at some point. I 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 w…
In the past I have had to deal with very complex configuration systems; several thousands of lines of XML which were supposed to direct many applications.
And writing:
value['this']['that']['the_other']
vs value.this.that.the_other
Really starts to matter for code legibility. Of course if you change your XML configs your Python will break - but it would likely break in any case.The other case where I have found this applicable is when I have complicated JSON structures in the form of user input. Typically I augment this style by using a JSONSchema that ensures attributes do exist and have defaults (or None).
Re: Box: Python dictionaries with recursive dot notation access
#87I think every Python programmer implements this at some point. I 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 w…
There are quite a few nay-votes here, so I'll give a yay vote. In the past I have had to deal with very complex configuration systems; several thousands of lines of XML which were supposed to direct many applications. And writing: value['this']['that']['the_other'] vs value.this.that.the_other Really starts to matter for code legibility. Of course if you change your XML configs your Python will break - but it would l…
value['this.that.the_other']
Bonus: Make it support JSONPath and you can use it to extract lots of things at arbitrarily nested levels.Re: Box: Python dictionaries with recursive dot notation access
#88Re: Box: Python dictionaries with recursive dot notation access
#89Re: Box: Python dictionaries with recursive dot notation access
#90Earlier 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.