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

Development

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

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.5.tar.gz (5.0 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.5-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: philiprehberger_dotpath-0.1.5.tar.gz
  • Upload date:
  • Size: 5.0 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.5.tar.gz
Algorithm Hash digest
SHA256 5c702f23a1251fae3304f1df11c7d72210cb87da47ebb3c593f1d7a58a799f1e
MD5 8f4191f182fba71a031a826d6b0bebd1
BLAKE2b-256 e9538adacf8c889c2a8779dfa06d0f7dc65f6b8d7f219cd383fbb25122c1a75f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_dotpath-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 aab39d9a05dcfe84986fecdf5877671d8219f3d6b92fb7f53943f992e9fbbbff
MD5 0f9a5b6471fc9cdd229de5bb9f7f3c1b
BLAKE2b-256 580bc28d5c7e0f8bece8f6ae3ef4a41c9f25da17932d179015faafd2e5a5cec9

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