News for hackers. Stuff that matters.
Re:Box – Python dictionary with dot notation by eulenteufel on Friday December 17, 2021 @05:39PM (#29599231)
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.

Re:Box – Python dictionary with dot notation by UncleEntity on Friday December 17, 2021 @04:47PM (#29598754)
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).

Re:Box – Python dictionary with dot notation by wodenokoto on Friday December 17, 2021 @05:59PM (#29599385)
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

Re:Box – Python dictionary with dot notation by mchenier on Friday December 17, 2021 @10:57PM (#29601487)
This looks like a duplicate of the existing Munch Python library. https://github.com/Infinidat/munch
Re:Box – Python dictionary with dot notation by greenie_beans on Friday December 17, 2021 @04:29PM (#29598563)
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?
Re:Box – Python dictionary with dot notation by cricalix on Saturday December 18, 2021 @08:19AM (#29604280)
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.
Re:Box – Python dictionary with dot notation by rak1507 on Friday December 17, 2021 @04:39PM (#29598662)
Not to be harsh but why would anyone ever use this? It's just some syntax sugar? (and over 3K loc... yikes)
Re:Box – Python dictionary with dot notation by jackbrookes on Friday December 17, 2021 @05:16PM (#29599042)
It's useful for APIs that return dictionaries that have a reliable structure
Re:Box – Python dictionary with dot notation by rak1507 on Friday December 17, 2021 @05:23PM (#29599106)
But.. why not use foo['bar'] vs foo.bar? Are the extra 3 characters so annoying you have to rely on a new library?
Re:Box – Python dictionary with dot notation by aqeelat on Friday December 17, 2021 @05:40PM (#29599233)
It might be useful for auto completion when you’re using versions older than 3.8 and can’t use TypedDict.
Re:Box – Python dictionary with dot notation by rak1507 on Friday December 17, 2021 @06:02PM (#29599404)
Auto completion? So saving a fraction of a second? Again... not sure I see the benefit here.
Re:Box – Python dictionary with dot notation by aqeelat on Friday December 17, 2021 @06:07PM (#29599432)
You do know that code completion is a selling feature for IDEs, right?
Re:Box – Python dictionary with dot notation by peakaboo on Saturday December 18, 2021 @09:41AM (#29604779)
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.

Re:Box – Python dictionary with dot notation by aqeelat on Friday December 17, 2021 @06:10PM (#29599452)
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.
Re:Box – Python dictionary with dot notation by MaxMoney on Friday December 17, 2021 @06:08PM (#29599437)
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

Re:Box – Python dictionary with dot notation by kkirsche on Saturday December 18, 2021 @03:12PM (#29608086)
If you don’t mind the heavier weight / newer version requirements, dataclasses from the standard lib also seem to solve this problem.
Re:Box – Python dictionary with dot notation by whalesalad on Friday December 17, 2021 @10:16PM (#29601246)
namedtuple is my usual goto for this but problem but I am glad to learn of SimpleNamespace
Re:Box – Python dictionary with dot notation by youssefabdelm on Saturday December 18, 2021 @03:53AM (#29602969)
Exactly my thought...I wonder how the Box library differs.
Re:Box – Python dictionary with dot notation by MaxMoney on Sunday December 19, 2021 @09:42PM (#29621010)
it probably does nested dicts