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

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.3.tar.gz (4.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_safeget-0.1.3-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: philiprehberger_safeget-0.1.3.tar.gz
  • Upload date:
  • Size: 4.0 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.3.tar.gz
Algorithm Hash digest
SHA256 8c054ca42f9b666bfff29dd76b582b273baf8fbab7bd5d22922ef3be283bb9e0
MD5 a98fac56afe1141e91d2256fe3c61997
BLAKE2b-256 864de2390cd582c819ef3a94252e5f45d6947b0774cd769573c5c202ba4e06e2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_safeget-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 bd7d501f522af5fba3eda0b9e4e4dc4efeffa846e775a3122af601306315865f
MD5 6da503b015600454b254b6b092c0ab2e
BLAKE2b-256 54a62c0fe8b6feceef7560620194df00c4c7769a5dbfd133e3dbbcd21c67aa6b

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