Utils that extend default dict|list operations
Project description
objectutils
Installation
pip install objectutils
About
Tiny functions that extend python json-like objects functionality as highly customizable:
- diff
- sum
- flattening
- traversing
operations on json-like python objects(lists, dicts)
Allows writing comprehensions without comprehensions 🙃
For example, having the following response from some API:
obj = {"computers": [
{
"computername": "1",
"software": ["s1", "s2"],
},
{
"computername": "2",
"software": ["s2", "s3"],
},
{
"computername": "3",
"software": ["s1", "s3"],
},
]
}
You should write something like that to get the Counter
of the software installed in total:
from itertools import chain
c = Counter(chain.from_iterable([computer["software"] for computer in obj["computers"]]))
Such expressions getting even worse in more complicated cases.
With deep_traverse
method provided by this tiny lib you should do the following to get the same Counter
:
from objectutils import deep_traverse
c = deep_traverse(obj, [Counter, chain.from_iterable, "computers", [], "software"])
deep_traverse
supports callable objects in its path, as well as the keys of object.
[]
considered as all the possible values in iterable, as 'asterisk'(*).
If applicable, calls the funcs and callable objects with unpacked iterable from the right. On exception that was predicted in this case, tries to call with single argument
As for me, it is much clearer approach than writing comprehensions or loops in such cases.
Only python 3.10+ supported
Provided as python library and made to be used from python directly.
Inspired by:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for objectutils-0.1.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39b4dccd5e82f6cb0d36e7eef1b3416cb20b7e47bee749da0c3054a4389bb70a |
|
MD5 | 768da242a9b4dc3b07a949a55d319b07 |
|
BLAKE2b-256 | 9317702f84fa4f20aaf1dac92f443d0772b299c20a7a9b2fe9b69ebb57b04c93 |