Box: Python dictionaries with recursive dot notation access
31–40 of 124 posts
Re: Box: Python dictionaries with recursive dot notation access
#32I dislike these structures. Yes, Python lets you pull this trick. Everything, yes, can be represented as a dict, a list, or one of the primitive str/int/etc. types. But I find it's a lot clearer in the long run (and even in the short run), if you have a collection of heterogenous attributes, to define a class for them. Leave dicts (and the subscript notation) for homogenous collections of k/v pairs.
A class gives you the benefit of a type: you get a name, so you can recognize this bag-of-attributes from other, different bags-of-attributes, because it's been given a name. A class also — usually — gets you a list of attributes, and hopefully documentation about what those attribute's types and expected values are.
If you don't like typing (on a keyboard), the attrs package makes things easier[1]; it has the advantage, however, that you get a real class at the end.
The only place I've seen something like Box or Bunch work well is in config files, and even then, only at the uppermost layers of the config (some of the leaves, esp. when you start having a "list of X" in a config — X needs a type).
Python lets you do magic, but with great power and all. IMO, but this is one of those times: Explicit is better than implicit. Simple is better than complex.
Re: Box: Python dictionaries with recursive dot notation access
#33So 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 err…
Re: Box: Python dictionaries with recursive dot notation access
#34Earlier quoted context omitted.
"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
#35Earlier quoted context omitted.
"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.
A) Giving off a signal that you're dealing with an object rather than a dict.
B) Making it cumbersome to swap out some of those selectors with variables.
C) Making it difficult to deal with the attribute not being there (with a dict you can say .get("attr2", {}) and it returns a default.
Re: Box: Python dictionaries with recursive dot notation access
#36 class Box(UserDict):
def __getattr__(self, key):
return self.__getitem__(key)
def __setattr__(self, key, value):
return self.__setitem__(key, value)
I know a few folks who prefer this method of access, so I can't naysay against it too much, but personally I just prefer plain dictionaries.Re: Box: Python dictionaries with recursive dot notation access
#37Seems like a lot of work and magic when compared to, say: class Box(UserDict): def __getattr__(self, key): return self.__getitem__(key) def __setattr__(self, key, value): return self.__setitem__(key, value) I know a few folks who prefer this method of access, so I can't naysay against it too much, but personally I just prefer plain dictionaries.
There's no reason to use UserDict, just extend `dict` directly, or implement `MutableMapping` instead. UserDict hasn't been useful since the types/class unification back in… Python 2.3 I think?
Re: Box: Python dictionaries with recursive dot notation access
#38A 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…
Re: Box: Python dictionaries with recursive dot notation access
#39It 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…
Re: Box: Python dictionaries with recursive dot notation access
#40Earlier quoted context omitted.
"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.
x.y == x.__getattr__("y")
x["y"] == x.__getitem__("y")
assignment == set{attr,item}
del x.y, del x["y"], etc
len() and slices of items, not attrs
etc, etc. If you start messing with those semantics, it can become very confusing very quickly in Python and you probably want a data type. Remember that Python gets nervous about cleverness.One way I've approached your problem before is a recursive helper where I borrowed some concepts from jq:
val = fetch(result_dict, "foo.bar.baz.plonk[0]")
Because then you can also isolate the missing key handling and all that stuff into your helper, rather than changing the semantics of an untyped dict. Aside from typing the responses from APIs -- the way better option -- I found this a reasonably Pythonic approach toward dealing with the annoyance.