Safely access nested dictionary keys without exceptions
pip install philiprehberger-safegetSafely access nested dictionary keys without exceptions.
pip install philiprehberger-safeget
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}
safeget(data, path, default=None, separator=".") — Get nested valuesafeset(data, path, value, separator=".") — Set nested valuehas_path(data, path, separator=".") — Check if path existspluck(data, *paths, default=None) — Extract multiple pathspip install -e .
python -m pytest tests/ -v
If you find this project useful: