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

Development

pip install -e .
python -m pytest tests/ -v

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.5.tar.gz (4.1 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.5-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: philiprehberger_safeget-0.1.5.tar.gz
  • Upload date:
  • Size: 4.1 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.5.tar.gz
Algorithm Hash digest
SHA256 2f0af2d7df1e87805942c135139444686685d1a8e7be29ac08bc341f89674626
MD5 f6e851d4ea9dc09d2eabbfe907230ad5
BLAKE2b-256 9ff2b07d8ece210cd03e916b908cbb595ae79e15a0392b025117860420e26a3e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_safeget-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 7bf5eda331646e9c12458ebb8ac71c7991b8aca2a9924d3b16bb5ce2327cb4be
MD5 88b17c18619cdc9106b5d85e27c385db
BLAKE2b-256 84906629951a8d61f66fe2b94e8163a04bde2e2fc8c45b3dcf9c0a9a08f10625

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