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}
Deleting and walking
from philiprehberger_safeget import delete_path, walk
# Remove a nested key (mutates in place); returns True if something was removed
data = {"a": {"b": 1, "c": 2}}
delete_path(data, "a.b") # True -> data == {"a": {"c": 2}}
delete_path(data, "x.y") # False -> data unchanged
# Works on list indices too
data = {"users": [{"id": 1}, {"id": 2}]}
delete_path(data, "users.0") # True -> data == {"users": [{"id": 2}]}
# Iterate every (path, value) pair in a nested structure
for path, value in walk({"a": {"b": 1, "c": [10, 20]}}):
print(path, value)
# a {'b': 1, 'c': [10, 20]}
# a.b 1
# a.c [10, 20]
# a.c.0 10
# a.c.1 20
# Only the leaves
list(walk({"a": {"b": 1, "c": [10, 20]}}, leaves_only=True))
# [("a.b", 1), ("a.c.0", 10), ("a.c.1", 20)]
API
safeget(data, path, default=None, separator=".")— Get nested valuesafeset(data, path, value, separator=".")— Set nested valuehas_path(data, path, separator=".")— Check if path existspluck(data, *paths, default=None)— Extract multiple pathsdelete_path(data, path, separator=".")— Remove value at path; returns whether anything was removedwalk(data, leaves_only=False, separator=".")— Iterate(path, value)pairs for every node (or only leaves)
Development
pip install -e .
python -m pytest tests/ -v
Support
If you find this project useful:
License
Metadata
Release files for philiprehberger-safeget 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| philiprehberger_safeget-0.3.0.tar.gz | 185.6 kB | Without provenance | Details for philiprehberger_safeget-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| philiprehberger_safeget-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for philiprehberger_safeget-0.3.0-py3-none-any.whl |
Total release size: 191.0 kB
Release files / philiprehberger_safeget-0.3.0.tar.gz
| Download URL | philiprehberger_safeget-0.3.0.tar.gz |
|---|---|
| Size | 185.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7f6265c418b440f134c318d46aab9aec283cdc3f4e052dd51d01e4a39c521527
|
|
BLAKE2b-256 checksum How to use checksums |
e10eb72a271c2597af838455eaa2f8e92c66c165fe44f088262d1a40a096a23e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Release files / philiprehberger_safeget-0.3.0-py3-none-any.whl
| Download URL | philiprehberger_safeget-0.3.0-py3-none-any.whl |
|---|---|
| Size | 5.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9b8c2fe033c0a3a0589e0f6f8e730d8679d020b0b25ccc6510d75607b3f6ee0f
|
|
BLAKE2b-256 checksum How to use checksums |
a5f1ea3ce63db69de9f39bacc4554615ddf1ba1baafe7a717e2edd5688bfb07b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Security for philiprehberger-safeget 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.