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

Uploaded Python 3

File details

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

File metadata

  • Download URL: philiprehberger_dotpath-0.1.4.tar.gz
  • Upload date:
  • Size: 4.9 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.4.tar.gz
Algorithm Hash digest
SHA256 06babf7a126bfc98b63485877b54271251099e05e401597008884005540f7106
MD5 4c120a7a0d44e8f89a0dd1b1b616c4a6
BLAKE2b-256 e700f0e2fbb4571327953b1b45d676b606d8abcb05e75772ff23493c1471a961

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_dotpath-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 7ad7371c5cb23e25a62bd55163c167219a1e9ff37789ca8d7cb8cec08be1e0cf
MD5 88f2fa937908dd72bd164bc0e251cd6d
BLAKE2b-256 f89f68b6ab365d3c01fe12b1ddf73571c5a61408a8c774553e051e1d06a404d2

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