Skip to main content

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:

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

objectutils-0.3.0.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

objectutils-0.3.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file objectutils-0.3.0.tar.gz.

File metadata

  • Download URL: objectutils-0.3.0.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for objectutils-0.3.0.tar.gz
Algorithm Hash digest
SHA256 0c5cca2bf74ef0d0c1a41a7c50eb9b8d73e7cf0f60fc16bf869833954d8ab63c
MD5 b6eef54ecf59108015d804423b66deb9
BLAKE2b-256 2160c45ee483cda738a887e4adb0738646a502806f53800419979e3ddb48c25a

See more details on using hashes here.

File details

Details for the file objectutils-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: objectutils-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for objectutils-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c37ef04b23cba6736137e34797ebef6829f90b1e2dc92128e7b7f856f04a3c0b
MD5 7977abb2b94d34bce6173f3c2fa17e00
BLAKE2b-256 9d37e92c61c5c0c8f06ac148992a654d7a38ea2fc6c3a729cae7f09f8ed92afe

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page