19 comments

[ 4.7 ms ] story [ 59.8 ms ] thread
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.
Not to be harsh but why would anyone ever use this? It's just some syntax sugar? (and over 3K loc... yikes)
It's useful for APIs that return dictionaries that have a reliable structure
But.. why not use foo['bar'] vs foo.bar? Are the extra 3 characters so annoying you have to rely on a new library?
It might be useful for auto completion when you’re using versions older than 3.8 and can’t use TypedDict.
Auto completion? So saving a fraction of a second? Again... not sure I see the benefit here.
You do know that code completion is a selling feature for IDEs, right?
You 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.
Fraction 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.

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

You 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

namedtuple is my usual goto for this but problem but I am glad to learn of SimpleNamespace
Exactly my thought...I wonder how the Box library differs.
it probably does nested dicts
If you don’t mind the heavier weight / newer version requirements, dataclasses from the standard lib also seem to solve this problem.