philiprehberger-dotpath
Access and mutate deeply nested dicts using dot-notation paths.
Installation
pip install philiprehberger-dotpath
Usage
from philiprehberger_dotpath import (
get, set, has, delete, flatten, unflatten, pop, merge, paths, search,
)
data = {"users": [{"name": "Alice", "email": "alice@example.com"}]}
# Get a value
get(data, "users[0].name") # "Alice"
# Get with default
get(data, "users[0].phone", default=None) # None
# Set a value (creates intermediate dicts)
set(data, "users[0].address.city", "Berlin")
# Check existence
has(data, "users[0].address.city") # True
# Delete a key
delete(data, "users[0].address.city")
Wildcards
data = {"users": [{"email": "a@b.com"}, {"email": "c@d.com"}]}
get(data, "users[*].email") # ["a@b.com", "c@d.com"]
# Set a field on all items
set(data, "users[*].active", True)
# data["users"] == [{"email": "a@b.com", "active": True}, {"email": "c@d.com", "active": True}]
# Delete a field from all items
delete(data, "users[*].active")
Negative Indexing
data = {"items": [{"name": "a"}, {"name": "b"}, {"name": "c"}]}
get(data, "items[-1].name") # "c"
set(data, "items[-2].name", "B") # updates second-to-last
Pop
data = {"a": {"b": 1, "c": 2}}
pop(data, "a.b") # 1 (key removed)
pop(data, "a.missing", default=0) # 0 (no error)
Merge
data = {"config": {"db": {"host": "localhost", "port": 3306}}}
merge(data, "config.db", {"port": 5432, "name": "mydb"})
# data["config"]["db"] == {"host": "localhost", "port": 5432, "name": "mydb"}
Search
data = {"a": 1, "b": {"c": 2, "d": [10, 20]}}
search(data, lambda v: isinstance(v, int) and v > 5)
# ["b.d[0]", "b.d[1]"]
List All Paths
paths() is a lazy iterator over every leaf path — the keys of flatten()
without materialising the dict.
data = {"a": {"b": 1}, "c": [10, 20]}
list(paths(data))
# ["a.b", "c[0]", "c[1]"]
# Useful for streaming over very large structures
for p in paths(huge_config):
print(p)
Flatten and Unflatten
nested = {"a": {"b": {"c": 1}}, "d": [10, 20]}
flatten(nested)
# {"a.b.c": 1, "d[0]": 10, "d[1]": 20}
unflatten({"a.b.c": 1, "d[0]": 10, "d[1]": 20})
# {"a": {"b": {"c": 1}}, "d": [10, 20]}
Listing keys and counting leaves
data = {"a": {"b": 1}, "c": 2}
keys(data) # ["a.b", "c"]
keys(data, depth=1) # ["a", "c"]
count({"a": 1, "b": {"c": 2, "d": 3}}) # 3
count({"a": []}) # 0 (empty containers don't count)
API
| Function | Description |
|---|---|
get(data, path, *, default=_MISSING) |
Get value at dot path; raises KeyError if missing and no default |
set(data, path, value) |
Set value at dot path, creating intermediate dicts; supports wildcards |
delete(data, path) |
Delete the key at dot path; supports wildcards |
has(data, path) -> bool |
Check whether a path exists |
pop(data, path, *, default=_MISSING) |
Remove and return value at path; like dict.pop() |
merge(data, path, value) |
Deep-merge a dict into the dict at path |
search(data, predicate) -> list[str] |
Find all dot-paths where predicate(value) is True |
paths(data, *, separator=".") -> Iterator[str] |
Lazy iterator over every leaf path |
flatten(data, *, separator=".") -> dict |
Flatten nested dict to single level |
unflatten(data, *, separator=".") -> dict |
Restore flattened dict to nested structure |
keys(data, depth=None) -> list[str] |
List all dot-paths, optionally capped at depth |
count(data) -> int |
Total leaf count (non-dict, non-list values) |
Development
pip install -e .
python -m pytest tests/ -v
Support
If you find this project useful:
License
Metadata
Release files for philiprehberger-dotpath 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| philiprehberger_dotpath-0.4.0.tar.gz | 190.2 kB | Without provenance | Details for philiprehberger_dotpath-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| philiprehberger_dotpath-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for philiprehberger_dotpath-0.4.0-py3-none-any.whl |
Total release size: 198.1 kB
Release files / philiprehberger_dotpath-0.4.0.tar.gz
| Download URL | philiprehberger_dotpath-0.4.0.tar.gz |
|---|---|
| Size | 190.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0e1cc37a3906257aaad09c0aa6019c367e6a03285325d3f14a64d95df86050b9
|
|
BLAKE2b-256 checksum How to use checksums |
3c0ea3563776363bc6a7822afceba19bb6eeb8eb769dcb8cb608454fece2cc3d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Release files / philiprehberger_dotpath-0.4.0-py3-none-any.whl
| Download URL | philiprehberger_dotpath-0.4.0-py3-none-any.whl |
|---|---|
| Size | 7.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4fc16273ba1564d45c9aebdb431984a8f354043668189401ebb3e98272d9c3d5
|
|
BLAKE2b-256 checksum How to use checksums |
03835d8627acb16ba5c0cea0d83ca9895894b3acc3548ee383aa9bf97a4e833e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Security for philiprehberger-dotpath 0.4.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.