Skip to main content

Access and mutate deeply nested dicts using dot-notation paths

Project description

philiprehberger-dotpath

Tests PyPI version Last updated

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]}

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

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

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

philiprehberger_dotpath-0.3.0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

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

philiprehberger_dotpath-0.3.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: philiprehberger_dotpath-0.3.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for philiprehberger_dotpath-0.3.0.tar.gz
Algorithm Hash digest
SHA256 12bc22e884e13ca1b984672af8e4abc3d99317a80cdf8398119521dfa43478fb
MD5 b5e136a33a8e0a6ae26d45555a98e79d
BLAKE2b-256 4ed50a3b5dd366b4cda726c2c1e24e7e56b9735f15d1ed299ea79eafa41a9154

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_dotpath-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 030c14c777ff9a1c1ff42c3eba82d1b9a2834ca6b048349317616de7eaaa0d54
MD5 928ee95a84b73ceb90c05c81053ad5b1
BLAKE2b-256 0919747179a18d9f9a23e2b890cd0d409c83caf5d158809adf74b9a5c8b6cb0e

See more details on using hashes here.

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