5 ms·
A single class that: * De/serializes JSON and YAML * De/mangles, de/encodes keys * Provides automatic, expensive hashcode * Blacklists/transforms a bunch of
by thesmallestcat 9y ago
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? The author must really like JavaScript syntax, or something, because I'm not seeing the point in this layer at all. It's the sort of library that you discover your inherited codebase is using, and just say, "fuckfuckfuck...", because it implies that the author cares more about pushing round pegs into square holes than writing the freaking application code; it reeks of inexperience.
- johnwheeler 9y ago> For the ability to avoid importing json/PyYAML and use clear key lookups? I think the advantage over json/PyYAML is not having to supply conversion functions for Decimal, datetime, or user defined datatypes.
- thesmallestcat 9y agoI agree that that's useful, but it should be a separate library, and I'd be surprised if there weren't a few already (I'm guilty of writing one-off serializers for datetimes/dates).
- yeukhon 9y agoWhy 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 9y ago[deleted]
- crdoconnor 9y ago"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?
- yeukhon 9y agoBecause it saves me a lot of chars. foo['name']['attr1']['attr2']['morefuckingattr'] vs foo.name.atr1.attr2.morefuckingattr More of a personal preference.
- felipellrocha 9y agoSaving chars is often a poor reason to design a language feature.
- johnwheeler 9y agoNope. It's called developer ergonomics.
- sevensor 9y agoThe 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!
- rajathagasthya 9y agoYou'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.
- sevensor 9y agoNested 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?
- ericfrederich 9y agoCheck out attrs. You can use frozen=True to get the immutable behavior as well, but there are many other benefits over namedtuple. https://attrs.readthedocs.io/en/stable/index.html https://attrs.readthedocs.io/en/stable/index.html
- SmirkingRevenge 9y agoI 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.
- xycodex 9y agoI think the python implementation of jsonpath is more appropriate for doing this sort of untyped access. If clean dots are really preferred, make classes! :)
- ziikutv 9y agoI think if 'Box' was broken down into smaller 'ZipLockBags', it would be better; it is doing too much things.
- swearfu 9y agoAgreed, I have no idea how this is easier. If anything it makes it impossible to write dynamic code. This is a perfect example of a library that (theoretically) helps the programmer at the expensive of more inefficient code.
- omginternets 9y ago>It's the sort of library that you discover your inherited codebase is using, and just say, "fuckfuckfuck...", because it implies that the author cares more about pushing round pegs into square holes than writing the freaking application code Ugh, this really hits home. I just started a new day-job where I'm essentially being asked to un-fuck a python codebase. Your statement neatly captures my feelings when I discovered the presence of hacked-together singleton classes littered throughout the thing. Good luck unit-testing that...