3 ms·
My thoughts also turned to toolz. Here's a example comparison: glom glom(target, ('system.planets', ['name'])) # ['earth', 'jupiter'] toolz list(pluc
by radmin 8y ago
My thoughts also turned to toolz.
Here's a example comparison:
glom
glom(target, ('system.planets', ['name']))
# ['earth', 'jupiter']
toolz
list(pluck('name', get_in(('system', 'planets'), target)))
# ['earth', 'jupiter']
- mhashemi 8y agoThe real advantage over toolz/get_in is in the breadth of types glom can support (http://glom.readthedocs.io/en/latest/api.html#setup-and-registration http://glom.readthedocs.io/en/latest/api.html#setup-and-regi...), the extensive fallback behavior (http://glom.readthedocs.io/en/latest/api.html#advanced-specifiers http://glom.readthedocs.io/en/latest/api.html#advanced-speci...), and "T" specifier (https://sedimental.org/glom_restructured_data.html#true-python-native https://sedimental.org/glom_restructured_data.html#true-pyth...), which allows performing object-oriented traversals and calls. Check the post for more examples of where path-based access falls short of the mark! (Sidenote: I find it very clunky that get_in() takes a "default" kwarg and _also_ a n"o_default" kwarg. Use a Sentinel object! http://boltons.readthedocs.io/en/latest/typeutils.html#boltons.typeutils.make_sentinel http://boltons.readthedocs.io/en/latest/typeutils.html#bolto... )
- nerdponx 8y agoSentinel is one of my favorite objects in Python.
- Sean1708 8y agoWhat's the advantage of make_sentinel vs just doing _MISSING = object()?
- nerdponx 8y agoA Sentinel is guaranteed unique and distinct from all other objects.
- Sean1708 8y agoI'm fairly sure object() also returns an object which is unique and distinct from all other objects. The only difference as far as I can see is that make_sentinel returns an object that has a unique and distinct type from all other objects, but I don't see why you'd be checking the type of your sentinels in Python.
- mhashemi 8y agoHehe, boltons.typeutils.make_sentinel should really be documented better for more experienced developers. A few small advantages: a nice repr, pickleability, and (back to the first advantage really) good rendering in a Sphinx autodoc context. :)
- doublereedkurt 8y agoa nice __repr__ is the main thing
- nerdponx 8y agoSorry to double reply, but yes the "T" spec is exactly what I would want. IMO it's yet another step better than using a "spec" made of untyped nested lists and dicts. DSL in strings = bad DSL in native syntax = good