this is neat. my first thought was, “why not just use a dataclass?” but i guess this would do some different things than a dataclass? and be easier to create than a dataclass?
Box appears to date back to 2017 or so. Dataclasses came with Python 3.7 in 2018. So it probably scratched an itch for someone, and has continued to be useful with the extended capabilities.
Maybe if you remember all the keys in your dictionary in your head. Otherwise you would have to look them up while coding if you don't have auto completion.
Not sure I like how it converts spaces and drops non-text characters, could possibly result in all sorts of hard to track down problems methinks.
When I wrapped boost::property_tree using the C-API I did the whole dot notation thing but it doesn’t do any sort of conversion — key names with spaces or whatever have to be looked up with prop[“name”] or the getter function(s).
I thought I wanted dictionaries with dot notation for a while. After some time I realized I didn't care about dictionary features like iterating over keys at all, I just needed a namespace.
`from types import SimpleNamespace` works for me.
Of course I don't want to say that there's no need for box, just that it's good to consider if you're actually looking for a dictionary.
Huh, I've always seen those called attribute dict, and seen a few implementation in different projects over the years.
They are quite fun to build yourself, if you are a novice in Python classes and inheritance.
A working implementation doesn't have to be as big as box, it can just be a few lines, if you don't need the class to import yaml files, etc. by itself
19 comments
[ 4.7 ms ] story [ 59.8 ms ] threadMaybe if you remember all the keys in your dictionary in your head. Otherwise you would have to look them up while coding if you don't have auto completion.
When I wrapped boost::property_tree using the C-API I did the whole dot notation thing but it doesn’t do any sort of conversion — key names with spaces or whatever have to be looked up with prop[“name”] or the getter function(s).
`from types import SimpleNamespace` works for me.
Of course I don't want to say that there's no need for box, just that it's good to consider if you're actually looking for a dictionary.
They are quite fun to build yourself, if you are a novice in Python classes and inheritance.
A working implementation doesn't have to be as big as box, it can just be a few lines, if you don't need the class to import yaml files, etc. by itself
from types import SimpleNamespace
mydict = { "name": "moe", "age": 3, "test": {"name": "josh"} # wont work }
dotted = SimpleNamespace(*mydict)
print(dotted.name, dotted.age) # moe 3