Skip to main content

Safely access nested dictionary keys without exceptions

Project description

philiprehberger-safeget

Tests PyPI version Last updated

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

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

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.2.0.tar.gz (5.7 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.2.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: philiprehberger_safeget-0.2.0.tar.gz
  • Upload date:
  • Size: 5.7 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.2.0.tar.gz
Algorithm Hash digest
SHA256 5c51d6a171dd1740809ba08cd924217fb50b65a9f5f283c33af05d2b194c9061
MD5 a8316af9c31c2f754cafde359cb2695f
BLAKE2b-256 0ab39b1686a0be1e8f69f4a134461b5767d87b29971f09e45d9e5781f8538c44

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_safeget-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f85783c4430c9eb3ee611627dfd211127e9ea05627f75c6c38a23e2037b2181b
MD5 53588e78dcc971085852249aac43e9c9
BLAKE2b-256 d313fc4a435e9c00a7ba88301a5c5d3295940a60e9ddfc9d9b793f9ecb04d7d6

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