Skip to main content

Safely access nested dictionary keys without exceptions

Project description

philiprehberger-safeget

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.0.tar.gz (3.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_safeget-0.1.0-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: philiprehberger_safeget-0.1.0.tar.gz
  • Upload date:
  • Size: 3.9 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.0.tar.gz
Algorithm Hash digest
SHA256 45a62510ed218811f9953a08164d6801a269ab32d3d79a72f88eeb4366808f08
MD5 b7627a353a66f35efd4e9929df9ee615
BLAKE2b-256 7d7dcdcaf36b654d67a4bd084faf5957c298c38aee6830b7eeb00dac53c1ba1c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_safeget-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 95dcdc82a7a597895b3a2e6de6342ad1f5e12ef236ec09773032eeada1cc69c3
MD5 ffadbc6265c282c63ee91fa70d385f17
BLAKE2b-256 14825827340bc2e572cd6c9ebb20f1ef951f1892f5b8609da357664b3d6492f9

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