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.3.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.3-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: philiprehberger_dotpath-0.1.3.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.3.tar.gz
Algorithm Hash digest
SHA256 07ef1503cb2aa7227898d7822e455bf330d5254088442ee2140635498942237c
MD5 437143b819633a4f32f22cedbecfe9e0
BLAKE2b-256 1d899feb2d90dd410bd8fa488ac505d6216e91a87cbeb14bad5945c9af06eb60

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_dotpath-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ec9b2617cf3de77e2f739d3e40b85d1a4d1e43ecf99e5c8af40ca72ad79247cc
MD5 8afc5b6f7643b43c60197283d760b3e3
BLAKE2b-256 c23d036d8ecb4e38537acc14479ba85bce6bf8a8d25ef91ac188e8cdfce77f62

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