Access and mutate deeply nested dicts using dot-notation paths
Project description
philiprehberger-dotpath
Access and mutate deeply nested dicts using dot-notation paths.
Install
pip install philiprehberger-dotpath
Usage
from philiprehberger_dotpath import get, set, has, delete, flatten, unflatten
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"]
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]}
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 |
delete(data, path) |
Delete the key at dot path |
has(data, path) -> bool |
Check whether a path exists |
flatten(data, *, separator=".") -> dict |
Flatten nested dict to single level |
unflatten(data, *, separator=".") -> dict |
Restore flattened dict to nested structure |
License
MIT
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 philiprehberger_dotpath-0.1.2.tar.gz.
File metadata
- Download URL: philiprehberger_dotpath-0.1.2.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
db4a9d412f853793387bd80b71a8a7c0c15830c5971ee4ce53364f9072f53dc3
|
|
| MD5 |
9fa44a5e51bac0791987fafc39aaf945
|
|
| BLAKE2b-256 |
75c120c474a00a3654b034ca5394caa8cf80b6f34c99ba7901c0ed1b795f5130
|
File details
Details for the file philiprehberger_dotpath-0.1.2-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_dotpath-0.1.2-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2f73c7c9b95346852bd3f390e63b4851c3ccc6361e5207b789b5155c4e1b1940
|
|
| MD5 |
ad173556863fb33c518e1eed75a22e35
|
|
| BLAKE2b-256 |
7a038ac1c4584d0a9120af03ce6f66396f0112472e576f96c2477de3888935bf
|