Skip to main content

Access and mutate deeply nested dicts using dot-notation paths

Project description

philiprehberger-dotpath

Tests PyPI version License

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


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.1.2.tar.gz (4.8 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.1.2-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

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

Hashes for philiprehberger_dotpath-0.1.2.tar.gz
Algorithm Hash digest
SHA256 db4a9d412f853793387bd80b71a8a7c0c15830c5971ee4ce53364f9072f53dc3
MD5 9fa44a5e51bac0791987fafc39aaf945
BLAKE2b-256 75c120c474a00a3654b034ca5394caa8cf80b6f34c99ba7901c0ed1b795f5130

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_dotpath-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2f73c7c9b95346852bd3f390e63b4851c3ccc6361e5207b789b5155c4e1b1940
MD5 ad173556863fb33c518e1eed75a22e35
BLAKE2b-256 7a038ac1c4584d0a9120af03ce6f66396f0112472e576f96c2477de3888935bf

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