Skip to main content

DictDeeper: Easy traversal of deeply nested dicts.

Project description

DictDeeper: The easy way to manipulate deeply-nested dicts like JSON-style objects.

What you can do with DictDeeper?

  1. You can navigate to any depth with a simple dotted-string key notation.
  2. You can even transparently navigate within lists inside dicts.
  3. You can do full and partial validate of your dict structure in a declarative way.
  4. You can do full and partial value comparison in a declarative way.
  5. You can arrange a variety of strategies to merge dicts into a single one.

How to use it?

To access values nested in an object using a dotted-key notation, wrap the object using DeepDict, then use the standard dict interface:

from dictdeeper import DeepDict


request_body = DeepDict(request.json())
is_scheduled = request_body.get("data.attributes.date_scheduled") is not None

How to match the structure of a deeply nested dict?

Use DeepDict to compare the values within a dict or list against a partial specification.

Both DeepDict and DeepList objects automatically use deep matching in their __eq__ method.

When a comparison fails, a MatcherError is raised, with the location and context of the mismatch.

Here are the things you can use in the matching spec:

  1. some_key in a dict exists of any value: Include "some_key": ... in the spec.
  2. Other keys may exist in a dict: Include ...: ... in the spec.
  3. Matching items in an unordered list: Include ... as an item in the spec.
  4. Matching a string against a regex: Use re.compile("your-regex-here") as a value in the spec.

Example:

data = {
    "1": "one",
    "2": {
        "a": "A",
        "b": {
            "i": "I",
            "ii": "II",
        },
    },
    "3": ["index0", "index1", "index2"],
    "4": [{"id": 1, "name": "foo"}, {"id": 2, "name": "bar"}, {"id": 3, "name": "baz"}],
}

assert DeepDict(data) == {
    "1": ...,  # [1]
    "3": ["index1", ...],  # [3]
    "4": [{"name": re.compile("ba."), ...: ...,}, ...],  # [4]
    ...: ...,  # [2]
}

Thank you to Routable

Routable sponsored the development of this library. Working at Routable is an awesome experience, with a developer-first culture that fosters innovation and growth. If you're interested in joining a dynamic team, check out our job opportunities here!

Authors

Project details


Release history Release notifications | RSS feed

This version

1.2

Download files

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

Source Distribution

dictdeeper-1.2.tar.gz (26.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dictdeeper-1.2-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file dictdeeper-1.2.tar.gz.

File metadata

  • Download URL: dictdeeper-1.2.tar.gz
  • Upload date:
  • Size: 26.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for dictdeeper-1.2.tar.gz
Algorithm Hash digest
SHA256 c7a5e3ae926ae2976762ffbfbd85a83d401a34ae4c82755b98f949a8a1f53a37
MD5 5c9e8d4ccac93ce5b49c4046e93868fd
BLAKE2b-256 e10b35a57efecf93d64dd40fd06d5488e441e97955d5168bb912647b543bad7c

See more details on using hashes here.

Provenance

The following attestation bundles were made for dictdeeper-1.2.tar.gz:

Publisher: publish.yml on henriquebastos/dictdeeper

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dictdeeper-1.2-py3-none-any.whl.

File metadata

  • Download URL: dictdeeper-1.2-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for dictdeeper-1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ed2dc55c74d92ff4cfc3b1279e1d4bd163aa5ae2ba46326728f04fed89dae77a
MD5 bc6f6a4bc2f613419e1f8fb10d5d5dfc
BLAKE2b-256 1a7f52cef29033ece693a3b63b579d9b4c78a9044da12ef8e13501c103cfeedd

See more details on using hashes here.

Provenance

The following attestation bundles were made for dictdeeper-1.2-py3-none-any.whl:

Publisher: publish.yml on henriquebastos/dictdeeper

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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