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?
- You can navigate to any depth with a simple dotted-string key notation.
- You can even transparently navigate within lists inside dicts.
- You can do full and partial validate of your dict structure in a declarative way.
- You can do full and partial value comparison in a declarative way.
- 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:
some_keyin adictexists of any value: Include"some_key": ...in the spec.- Other keys may exist in a
dict: Include...: ...in the spec. - Matching items in an unordered
list: Include...as an item in the spec. - 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
- Henrique Bastos henrique@bastos.net
- Matthew Scott matt@11craft.com
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c7a5e3ae926ae2976762ffbfbd85a83d401a34ae4c82755b98f949a8a1f53a37
|
|
| MD5 |
5c9e8d4ccac93ce5b49c4046e93868fd
|
|
| BLAKE2b-256 |
e10b35a57efecf93d64dd40fd06d5488e441e97955d5168bb912647b543bad7c
|
Provenance
The following attestation bundles were made for dictdeeper-1.2.tar.gz:
Publisher:
publish.yml on henriquebastos/dictdeeper
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dictdeeper-1.2.tar.gz -
Subject digest:
c7a5e3ae926ae2976762ffbfbd85a83d401a34ae4c82755b98f949a8a1f53a37 - Sigstore transparency entry: 175438005
- Sigstore integration time:
-
Permalink:
henriquebastos/dictdeeper@df177a441f049259e6ce64061892c5442766ed5f -
Branch / Tag:
refs/tags/1.2 - Owner: https://github.com/henriquebastos
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@df177a441f049259e6ce64061892c5442766ed5f -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ed2dc55c74d92ff4cfc3b1279e1d4bd163aa5ae2ba46326728f04fed89dae77a
|
|
| MD5 |
bc6f6a4bc2f613419e1f8fb10d5d5dfc
|
|
| BLAKE2b-256 |
1a7f52cef29033ece693a3b63b579d9b4c78a9044da12ef8e13501c103cfeedd
|
Provenance
The following attestation bundles were made for dictdeeper-1.2-py3-none-any.whl:
Publisher:
publish.yml on henriquebastos/dictdeeper
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dictdeeper-1.2-py3-none-any.whl -
Subject digest:
ed2dc55c74d92ff4cfc3b1279e1d4bd163aa5ae2ba46326728f04fed89dae77a - Sigstore transparency entry: 175438007
- Sigstore integration time:
-
Permalink:
henriquebastos/dictdeeper@df177a441f049259e6ce64061892c5442766ed5f -
Branch / Tag:
refs/tags/1.2 - Owner: https://github.com/henriquebastos
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@df177a441f049259e6ce64061892c5442766ed5f -
Trigger Event:
release
-
Statement type: