Live data from Hacker News

Box: Python dictionaries with recursive dot notation access

github.com

71–80 of 124 posts

Re: Box: Python dictionaries with recursive dot notation access

#71
post #56

Earlier quoted context omitted.

So then what are you supposed to deserialize nested data structures like JSON into? Dictionaries are the obvious "right" way, Python's annoying tooling aside.

Namedtuples or classes that represent the data, I'd suggest personally.

I would agree. Namedtuple has an asdict method so that reserialization to JSON is simple; you can do the same for a class.

Re: Box: Python dictionaries with recursive dot notation access

#72

Earlier quoted context omitted.

Saving chars is often a poor reason to design a language feature.

Nope. It's called developer ergonomics.

Part of ergonomics is simplicity about what the thing you're looking at actually is.

Re: Box: Python dictionaries with recursive dot notation access

#73

How is this different from addict? https://github.com/mewwts/addict

Mostly copying and pasting from my previous comment for visibility.

It works similarity to addict, but does have important distinctions and IMO seniority (was in my 'reusables' pypi project named 'Namespace' before addict existed. Just finally spun it into it's own project).

Biggest differences:

* Box will convert items added to the object after creation, addict does not

* addict only acts as a defaultdict, Box can act as either regular or a recursive defaultdict

* Box updates it’s __dir__ so that attributes (keys) are tab completed in stuff like IPython

* Box has a lot of other features, such as being able to auto convert with spaces to attributes, freezing the box, and built in JSON / YAML handling.

Re: Box: Python dictionaries with recursive dot notation access

#74
post #58

I think every Python programmer implements this at some point. I think the real reason not to do this is one that as of this writing still hasn't been mentioned yet, which is that when using dot notation, your key names have to fit the Python grammar for identifiers, so you can't have a key "the thing" with a space, or anything else that isn't a Python identifier. So you can't just say "I can access this dictionary w…

Agreed. This is not the sort of code you want to be dealing with in a few years time.

Also, I seldom find that string based dicts are what I need. One of the best things about Python is being able to use virtually anything as a key.

Re: Box: Python dictionaries with recursive dot notation access

#75
post #60

Earlier quoted context omitted.

Nope. It's called developer ergonomics.

No, it's called being terrible at your job. If you need to add complexity to your project just to save you some typing, you have no concept of what you're doing. It's like an accountant not understanding math.

Complexity can hide in unexpected places and sometimes a dash of complexity in one place can make something in another place much simpler.

I think this library has it's uses. I wouldn't use it uncritically but I wouldn't dismiss it either.

Re: Box: Python dictionaries with recursive dot notation access

#76
post #29

I've worked on codebases that use a similar thing, and I have yet to see one where this wasn't a mistake. Most of the time, this probably gives you exactly what you want, but then there are times where you discover bugs in production because data you assumed is your custom class is a plain old dict, and now you're raising AttributeError all over the place. Another wart is if you are unfortunate enough to have keys th…

I agree, but when I saw this, my first assumption was that it was for personal projects primarily. I very rarely see libraries that change syntax like this in large scale or long term codebases.

Re: Box: Python dictionaries with recursive dot notation access

#77
post #58

I think every Python programmer implements this at some point. I think the real reason not to do this is one that as of this writing still hasn't been mentioned yet, which is that when using dot notation, your key names have to fit the Python grammar for identifiers, so you can't have a key "the thing" with a space, or anything else that isn't a Python identifier. So you can't just say "I can access this dictionary w…

Also dot means attribute means less speed.

Re: Box: Python dictionaries with recursive dot notation access

#78
Though many reasonable rebuttals have been mentioned, the one case in which I find this pattern indispensable is when creating mock-instances for a variety of classes that need to be interleaved amongst genuine instances (say, ORM objects, if you're unlucky enough to have those in your life) so that code operating on collections of the latter doesn't get littered with special cases for the former.

Re: Box: Python dictionaries with recursive dot notation access

#79
post #58

I think every Python programmer implements this at some point. I think the real reason not to do this is one that as of this writing still hasn't been mentioned yet, which is that when using dot notation, your key names have to fit the Python grammar for identifiers, so you can't have a key "the thing" with a space, or anything else that isn't a Python identifier. So you can't just say "I can access this dictionary w…

Also dot means attribute means less speed.

If you have to worry about that, you've taken many wrong turns in the past.

Re: Box: Python dictionaries with recursive dot notation access

#80
post #58

I think every Python programmer implements this at some point. I think the real reason not to do this is one that as of this writing still hasn't been mentioned yet, which is that when using dot notation, your key names have to fit the Python grammar for identifiers, so you can't have a key "the thing" with a space, or anything else that isn't a Python identifier. So you can't just say "I can access this dictionary w…

Yep. Or keys with dots in their names, or keys actually named something like "__eq__" (strange but not impossible). The worst thing about abstractions is when they break. I can always figure out how to fix or extend ugly but straightforward code. I may not even know where the manual is when a fancy wrapper dumps a trace.

>I may not even know where the manual is when a fancy wrapper dumps a trace.

I read somewhere that if it is written in self-documenting code, the problem and its solution will be obvious.

Post reply on HN