Live data from Hacker News

Box: Python dictionaries with recursive dot notation access

github.com

21–30 of 124 posts

Re: Box: Python dictionaries with recursive dot notation access

#21
post #15

Earlier quoted context omitted.

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?

Because it saves me a lot of chars.

foo['name']['attr1']['attr2']['morefuckingattr'] vs foo.name.atr1.attr2.morefuckingattr

More of a personal preference.

Re: Box: Python dictionaries with recursive dot notation access

#22

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.

> Gets really hairy for deeply nested dicts.

Write a function. `def my_deep_get(dict, keys)`

Re: Box: Python dictionaries with recursive dot notation access

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

You're using named tuple correctly, but why would you want dot notation on a dictionary? There's a reason square bracket notation is used to access dict items. Because it's an item of a dictionary, not an attribute.

Re: Box: Python dictionaries with recursive dot notation access

#25
post #2

How is the performance on this for very large dictionaries vs. standard python dictionary lookups?

A really good question. I haven't looked much at the code but I'd imagine still O(1) because it's just converting the keys into a different format.

But that says nothing about whether there is a constant penalty being applied.

Re: Box: Python dictionaries with recursive dot notation access

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

Re: Box: Python dictionaries with recursive dot notation access

#28

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 get the itch its scratching. Working with REST services in particular can become very tedious, real fast, without some sort of de/serialization beyond json -> dict.

Many python REST API client libraries end up rolling their own one-off dict -> "dot dict" transformation helpers, for better or worse.

This sort of thing can be great for interactive use-cases, where terseness really helps (notebooks, REPL's etc). I'd definitely use something like this, there. But I would share the same reservations as you in using this library in a serious project.

Re: Box: Python dictionaries with recursive dot notation access

#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 that match the name of one of dict's methods, then you have to resort to instance['items'], which defeats the purpose of using this in the first place.

This is a fun trick, but if someone one my team tried to introduce this, it won't make it through code review.

Re: Box: Python dictionaries with recursive dot notation access

#30
So Django Templates also use dot notation lookups for dict, lists, and objects[0]

  Dictionary lookup, attribute lookup and list-index lookups are implemented with a dot notation:

  {{ my_dict.key }}
  {{ my_object.attribute }}
  {{ my_list.0 }}
  If a variable resolves to a callable, the 
  template system will call it with no 
  arguments and use its result instead of the callable.
Which leads to some interesting and confusing errors if you start iterating over `.items` and you get a callable and not the list you expect.

  In [17]: a = {"a": 1, "items": {"b": {"c": {}}}}
    ...: a_box = Box(a)
    ...: a_box
    ...: 
  Out[17]: 

  In [18]: a_box
  Out[18]: 

  In [19]: a_box.items
  Out[19]: 

  In [20]: a_box.a
  Out[20]: 1

[0] https://docs.djangoproject.com/en/1.11/topics/templates/#var...

EDIT: This came up because our JSON commonly uses `items` as a key for a list of items, which I expect to be at `a_dict['items']`, and it has nothing to do with python's `a_dict.items`.

Post reply on HN