Skip to main content

Safely access nested dictionary keys without exceptions

Project description

philiprehberger-safeget

Tests PyPI version License

Safely access nested dictionary keys without exceptions.

Installation

pip install philiprehberger-safeget

Usage

from philiprehberger_safeget import safeget, safeset, has_path, pluck

data = {"user": {"address": {"city": "NYC"}}}

safeget(data, "user.address.city")              # "NYC"
safeget(data, "user.address.zip")               # None
safeget(data, "user.address.zip", default="?")  # "?"

# List index support
data = {"users": [{"name": "Alice"}, {"name": "Bob"}]}
safeget(data, "users.0.name")  # "Alice"

# Check existence
has_path(data, "users.0.name")   # True
has_path(data, "users.5.name")   # False

# Set nested values (creates intermediates)
safeset(data, "config.debug", True)

# Extract multiple paths
pluck(data, "users.0.name", "config.debug")
# {"users.0.name": "Alice", "config.debug": True}

API

  • safeget(data, path, default=None, separator=".") — Get nested value
  • safeset(data, path, value, separator=".") — Set nested value
  • has_path(data, path, separator=".") — Check if path exists
  • pluck(data, *paths, default=None) — Extract multiple paths

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_safeget-0.1.4.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

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

philiprehberger_safeget-0.1.4-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for philiprehberger_safeget-0.1.4.tar.gz
Algorithm Hash digest
SHA256 119cccb2c6a95ff1374ef792279abd2ed148e6be3770f55e9188ec04d1894cad
MD5 1d94c072ee155309918f16a84046fcc3
BLAKE2b-256 dd27668129955a05d65d373fd89a0eaf058ddfa93f0a05ee5afd493ca16c22e9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_safeget-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 abda917182c090327033af8c03d441574ec3ee4f964cc798ce5abd4089ab2d47
MD5 82cbf0d2ea98f09e60f310a5da7978e2
BLAKE2b-256 bf56a59c2b7d13913b6f4796a8edfddc8a0a688c6fa2f6ce2628ec8c4b204977

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