7 ms·
Box – Python dictionary with dot notation
- greenie_beans 5y agothis 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?
- cricalix 5y agoBox 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.
- rak1507 5y agoNot to be harsh but why would anyone ever use this? It's just some syntax sugar? (and over 3K loc... yikes)
- jackbrookes 5y agoIt's useful for APIs that return dictionaries that have a reliable structure
- rak1507 5y agoBut.. why not use foo['bar'] vs foo.bar? Are the extra 3 characters so annoying you have to rely on a new library?
- aqeelat 5y agoIt might be useful for auto completion when you’re using versions older than 3.8 and can’t use TypedDict.
- rak1507 5y agoAuto completion? So saving a fraction of a second? Again... not sure I see the benefit here.
- aqeelat 5y agoYou do know that code completion is a selling feature for IDEs, right?
- aqeelat 5y agoYou know what .. I’ll need to give it a try before I can actually discuss it further. I don’t want my ignorance to cloud other people’s judgement.
- peakaboo 5y agoFraction of a second? 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.
- UncleEntity 5y agoNot 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).
- eulenteufel 5y agoI 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.
- wodenokoto 5y agoHuh, 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
- MaxMoney 5y agoYou can get away with a single hashed dict: from types import SimpleNamespace mydict = { "name": "moe", "age": 3, "test": {"name": "josh"} # wont work } dotted = SimpleNamespace(*mydict) print(dotted.name, dotted.age) # moe 3
- whalesalad 5y agonamedtuple is my usual goto for this but problem but I am glad to learn of SimpleNamespace
- youssefabdelm 5y agoExactly my thought...I wonder how the Box library differs.
- MaxMoney 5y agoit probably does nested dicts
- kkirsche 5y agoIf you don’t mind the heavier weight / newer version requirements, dataclasses from the standard lib also seem to solve this problem.
- mchenier 5y agoThis looks like a duplicate of the existing Munch Python library. https://github.com/Infinidat/munch https://github.com/Infinidat/munch