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

Uploaded Python 3

File details

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

File metadata

  • Download URL: philiprehberger_safeget-0.1.1.tar.gz
  • Upload date:
  • Size: 3.9 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.1.tar.gz
Algorithm Hash digest
SHA256 723faa6718154096fc13647a25fd709a706326095c120827f756ab762fe72b6e
MD5 d6d4e89ecbc3dda99c2cf47217cdb25e
BLAKE2b-256 9a90afe4ed906854145f81ee2883a33f8f5a4eb41a91cfcd20e05c5d98f435af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_safeget-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0f201d249df0ca965c913057d313762729de0d15438ca12d182623468939466b
MD5 71d5886b6076810686a85342aa5ad3ab
BLAKE2b-256 08fd128931736b1c9fc63a07eef8e03d00a9239a43f8275bdfb485a712067973

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