20 points · by nothrowaways · 4y ago · 19 comments
`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.
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).
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