Live data from Hacker News

Box: Python dictionaries with recursive dot notation access

github.com

11–20 of 124 posts

Re: Box: Python dictionaries with recursive dot notation access

#11
post #6

What happens if the dictionary has the keys 'John Doe' and 'John_Doe'? Can you use 'self' as a key? Ive used a class to provide this kind of dot notation fererencing of hierarchical data. If you could enclose the keys in quotes I might feel better about it but that's probably not possible.

>Can you use 'self' as a key?

Without looking at the code: yes. `self` in python is just convention.

Re: Box: Python dictionaries with recursive dot notation access

#13
It used to irritate me having to use alternative grammar for accessing values in a mapping vs accessing object attributes. However this is an area where I learned to appreciate Python's hardline on consistency. Guido in particular has been responsible for keeping the language predictable and concise by disallowing patterns like this in the language itself. So on one hand it's beautiful that it's so easy to implement things like this (which I am guilty of doing too), but on the other it is a perversion of the intentional distinction between attributes and mappings.

I've since learned to love the distinction between object attributes and items in a mapping and happily implement ['this style'] accessors without complaint now.

That being said, if the library just did difflib.get_close_matches() on the key lookup that would be neat for some use cases where you want fuzzy keep lookup.

Re: Box: Python dictionaries with recursive dot notation access

#14
This looks really nice. I might be reading the DefaultBox docs wrong, but does it support this:

    box.might_exist.might_exist.might_exist.desired_key
where desired_key would return a default value if one of the keys doesn't exist?

I find the bulk of my dict code is checking for keys before access, or using .get('', default) recursively. Gets really hairy for deeply nested dicts.

Re: Box: Python dictionaries with recursive dot notation access

#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.

Re: Box: Python dictionaries with recursive dot notation access

#16
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.

[deleted]

Re: Box: Python dictionaries with recursive dot notation access

#17

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)

I am guilty of writing classes which are really simple, but only writing a class for the sake of consistency across my codebase.

Although there is this PyCon talk from 2012 advocating not to write classes if there is only one or two methods. [1]

[1]: https://news.ycombinator.com/item?id=3717715

Re: Box: Python dictionaries with recursive dot notation access

#18
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.

"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"

Why though?

Re: Box: Python dictionaries with recursive dot notation access

#19
I like using NamedTuple for dot notation access, it's a good middle ground between dicts and custom classes.

Pandas must use something similar under the hood to provide dot notation access to columns. I wish h5py did the same for hdf5 objects. In py3, I find myself needing to type list(X.items()) and then list(X['Y'].items()) and so on when I'm exploring a new dataset... fairly awkward for interactive use.

Re: Box: Python dictionaries with recursive dot notation access

#20

This looks really nice. I might be reading the DefaultBox docs wrong, but does it support this: box.might_exist.might_exist.might_exist.desired_key where desired_key would return a default value if one of the keys doesn't exist? I find the bulk of my dict code is checking for keys before access, or using .get('', default) recursively. Gets really hairy for deeply nested dicts.

I also found myself doing that pretty often, so I wrote this:

    def nget(d, *ks, **kwargs):
        for k in ks:
            d = d.get(k)
            if d is None:
                return kwargs.get('default')
        return d

   >>> d = {'a': {'b': {'c': 12 }}}
   >>> nget(d, 'a', 'd', 'c', default='Not Found!')
   'Not Found!'
   >>> nget(d, 'a', 'b', 'c')
   12
Post reply on HN