Live data from Hacker News

Box: Python dictionaries with recursive dot notation access

github.com

41–50 of 124 posts

Re: Box: Python dictionaries with recursive dot notation access

#41
post #21

Earlier quoted context omitted.

Because it saves me a lot of chars. foo['name']['attr1']['attr2']['morefuckingattr'] vs foo.name.atr1.attr2.morefuckingattr More of a personal preference.

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

Nope. It's called developer ergonomics.

Re: Box: Python dictionaries with recursive dot notation access

#43
If you want readonly access using dot notation instead of keys then I have a simple gist [1] I use that works well for tracking nested dicts - even nested inside lists. It's a good way to pass a dict into a function expecting an object in some scenarios.

[1] https://gist.github.com/mmerickel/ff4c6faf867d72c1f19c

Re: Box: Python dictionaries with recursive dot notation access

#44

A single class that: * De/serializes JSON and YAML * De/mangles, de/encodes keys * Provides automatic, expensive hashcode * Blacklists/transforms a bunch of likely keys because they conflict with reserved words * Overlays attrs (__box_heritage) * All in pretty complex code that obfuscates what you're really doing (especially to a maintainer) For the ability to avoid importing json/PyYAML and use clear key lookups? Th…

I think if 'Box' was broken down into smaller 'ZipLockBags', it would be better; it is doing too much things.

Re: Box: Python dictionaries with recursive dot notation access

#45
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, I had a legacy code base with this as well. It was a giant pain both in prod as you say but also for green developers. Really slowed down development.

Re: Box: Python dictionaries with recursive dot notation access

#46
post #15

A single class that: * De/serializes JSON and YAML * De/mangles, de/encodes keys * Provides automatic, expensive hashcode * Blacklists/transforms a bunch of likely keys because they conflict with reserved words * Overlays attrs (__box_heritage) * All in pretty complex code that obfuscates what you're really doing (especially to a maintainer) For the ability to avoid importing json/PyYAML and use clear key lookups? Th…

Why so negative here? I like Python a lot, and I don't write much Javascript, but one thing I wish I could do in Python is the dot notation from a dictionary. I sometimes used namedtuple as a cheap (but "immutable") class, so I can simply use dot notation when I am passing my object around my functions, instead of always stuffing the data into a dictionary.

Nested namedtuples are way, way better than the proposed approach. I've found that nested dictionaries in Python are fragile in two dimensions: not only do you have no guarantee that the values in your dictionaries are appropriately typed, you have no guarantee that the keys even exist! The whole thing just collapses in a messy heap if you look at it sideways. Furthermore, it's very hard to check to see whether anything has been changed deep within your crazy nested dictionary structure. With nested namedtuples, immutability lets you use "is" on the root to see whether anything has changed deep inside. Sure, you can't say foo.bar.baz.quux = 5, but did you really want to do that, knowing how hard it would be to use that information?

Re: Box: Python dictionaries with recursive dot notation access

#47

Earlier quoted context omitted.

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

Nope. It's called developer ergonomics.

The annoyance here is a feature not a bug. Deeply nested dictionaries, in my experience, are a source of infinite KeyErrors. The problem with using a dictionary as a cheap object is that it's very easy for it to be an object you weren't expecting. Don't nest dictionaries in Python!

Re: Box: Python dictionaries with recursive dot notation access

#48
post #26

For those who just want a simple a.b instead of a['b'], use this: class Obj(): def __init__(self, d): self.__dict__ = d d = Obj({ 'a': 1, 'b': 2, }) print(d.a)

These days (Python >= 3.3) SimpleNamespace is a more concise way of doing this: from types import SimpleNamespace d = SimpleNamespace(a=1, b=2) print(d.a)

Haha neat, that means this works very cleanly as a conversion mechanism:

    my_dict = {
        'a': 1,
        'b': 2
    }

    d = SimpleNamespace(**my_dict)
    print(d.a)

Re: Box: Python dictionaries with recursive dot notation access

#50
This kind of thing has been implemented a lot of times by a lot of people. It starts with someone who just thinks that addressing deeply nested dicts is ugly, and ends with people trying to tack on all kinds of features to justify their modification of the basic grammar.

Probably not a huge issue if it's an isolated use-case. Not something I would want to see everywhere in my codebase.

Post reply on HN