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

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for philiprehberger_safeget-0.1.2.tar.gz
Algorithm Hash digest
SHA256 658fd286ddb8b8bd76ce55517e960288de55bb564a16f55e04430fa441b2054d
MD5 f25ab62beb5b420f625e6ea460b77781
BLAKE2b-256 cdfb0874d7e45592c68a7247d7ed1275dcf3fffac9fae85e4b8b80a216ccb7fd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_safeget-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1e852b8601b49855cfc9b8e59e2ead06137f8aba857e85d0d71dec36ddfc9dc8
MD5 2e390e65db4dc6f577a74931b723fce1
BLAKE2b-256 ff02e047b21c4ca6728e3d8670ef3e5b4fed62ba579c749680b9e5722d2baa10

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