Skip to main content

objectutils

Installation

pip install objectutils

About

Tiny functions that extend python json-like objects functionality as highly customizable:

operations on json-like python objects(lists, dicts)

Allows writing comprehensions without comprehensions 🙃

Only python 3.10+ supported

Provided as python library and made to be used from python directly.

Inspired by:

Examples

Having the following response from some API:

obj = {"computers": [
        {
            "computername": "1",
            "software": ["s1", "s2"],
        },
        {
            "computername": "2",
            "software": ["s2", "s3"],
        },
        {
            "computername": "3",
            "software": ["s1", "s3"],
        },
    ]
}

traverse

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 traverse method provided by this tiny lib you should do the following to get the same Counter:

from objectutils import traverse

c = traverse(obj, [Counter, chain.from_iterable, "computers", [], "software"])

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.

Transformation

It is also possible to transform the data selected by traverse using dicts on the path, but this may be a bit tricky:

traverse(
    {"old1": {"old2": "old3"}, "old4": "old5"}, 
    [
        {"new2": []}, 
        [{"new3": ["old1", "old2"], "new4": ["old4"]}, 
        {"anothernew4": ["old4"]}]
    ]
)

#Result {'new2': [{'new3': 'old3', 'new4': 'old5'}, {'anothernew4': 'old5'}]}

flatten

from objectutils import flatten

Use flatten(obj) to get a flat dictionary in form {path: plain value} For the data above, the result is the following:

{
    ("computers", 0, "computername"): "1",
    ("computers", 0, "software", 0): "s1",
    ("computers", 0, "software", 1): "s2",
    ("computers", 1, "computername"): "2",
    ("computers", 1, "software", 0): "s2",
    ("computers", 1, "software", 1): "s3",
    ("computers", 2, "computername"): "3",
    ("computers", 2, "software", 0): "s1",
    ("computers", 2, "software", 1): "s3",
}

zip_dicts

Used to join values of two similar dicts on same paths. May be used to find a diff betweens two dicts, join values as a sum

from objectutils import zip_dicts


d1 = {
    1: {
        2: 3,
        3: 3,
    }
}
d2 = {
    1: {
        2: 4,
        3: 3,
    }
}
zip_dicts(d1, d2, lambda a, b: a+b, lambda a, b: a==b) # {1: {3: 6}} - "find a sum on all same paths where values are equal"

Third argument is a function for two items on the same paths, fourth is the filter for leaf dict values.

Using as diff (default mapping and filter)

d1 = {
    1: {
        3: 3,
        4: 2,
    }
}
d2 = {
    1: {
        3: 3,
        4: 3,
    }
}
zip_dicts(d1, d2) # {1: {4: (2, 3)}} - elements on path [1, 4] not equal(2 and 3 correspondingly)

The keys are the paths that may be used in traverse to get the value next to them. However intended usage is to reduce the path somehow, using ".".join() or something like that

Metadata

Release files for objectutils 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for objectutils 0.3.0
File Size Uploaded Provenance
objectutils-0.3.0.tar.gz 9.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for objectutils 0.3.0
File Interpreter ABI Platform
objectutils-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.9 kB

Release files / objectutils-0.3.0.tar.gz

Download URL objectutils-0.3.0.tar.gz
Size 9.6 kB
Tags Source
SHA-256 checksum
How to use checksums
0c5cca2bf74ef0d0c1a41a7c50eb9b8d73e7cf0f60fc16bf869833954d8ab63c
BLAKE2b-256 checksum
How to use checksums
2160c45ee483cda738a887e4adb0738646a502806f53800419979e3ddb48c25a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release files / objectutils-0.3.0-py3-none-any.whl

Download URL objectutils-0.3.0-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c37ef04b23cba6736137e34797ebef6829f90b1e2dc92128e7b7f856f04a3c0b
BLAKE2b-256 checksum
How to use checksums
9d37e92c61c5c0c8f06ac148992a654d7a38ea2fc6c3a729cae7f09f8ed92afe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Security for objectutils 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page