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 valuesafeset(data, path, value, separator=".")— Set nested valuehas_path(data, path, separator=".")— Check if path existspluck(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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2f0af2d7df1e87805942c135139444686685d1a8e7be29ac08bc341f89674626
|
|
| MD5 |
f6e851d4ea9dc09d2eabbfe907230ad5
|
|
| BLAKE2b-256 |
9ff2b07d8ece210cd03e916b908cbb595ae79e15a0392b025117860420e26a3e
|
File details
Details for the file philiprehberger_safeget-0.1.5-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_safeget-0.1.5-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7bf5eda331646e9c12458ebb8ac71c7991b8aca2a9924d3b16bb5ce2327cb4be
|
|
| MD5 |
88b17c18619cdc9106b5d85e27c385db
|
|
| BLAKE2b-256 |
84906629951a8d61f66fe2b94e8163a04bde2e2fc8c45b3dcf9c0a9a08f10625
|