No project description provided
Project description
SafeDict
Python does not natively support a chaining operator (like ?. in JavaScript) for safely accessing nested dictionary keys. This often leads to verbose code to check if a key exists or if its value is None before accessing deeper levels.
SafeDict is here to solve this problem.
SafeDict is a Python library that provides a safe way to access nested dictionary keys. When a key does not exist or its value is None, it returns None by default (or a user-specified default value). Unlike standard dictionaries, it prevents errors when accessing deeper levels of nested keys, making your code cleaner, easier to read, and less error-prone.
Key Features
- Safely access nested dictionary keys without manual checks.
- Fully compatible with regular Python dictionaries.
Installation
Install the library via pip:
pip install safe-dict
Usage
from safedict import SafeDict
# Example with a nested dictionary
data = SafeDict({"a": {"b": {"c": 42}}})
# Safe key access using get
print(data.get("a", {}).get("b", {}).get("c")) # Output: 42
print(data.get("a", {}).get("x", {}).get("y")) # Output: None
# Safe key access with a default value
print(data.get("a").get("x", "default")) # Output: default
Comparison
Traditional Way:
Using a standard dictionary, you would need to manually check each key:
data = {
"a": {
"b": {
"c": 42
},
"d": None
}
}
# Safe access
if "a" in data and data["a"] is not None:
if "b" in data["a"] and data["a"]["b"] is not None:
if "c" in data["a"]["b"]:
print(data["a"]["b"]["c"])
# You can also use the "get" method, but if you access a key whose value is None, an error will occur:
print(data.get("a", {}).get("d", {}).get("c")) # AttributeError: 'NoneType' object has no attribute 'get', because "d" is not a dictionary.
With SafeDict:
You can write the same logic much more cleanly:
data = SafeDict({"a": {"b": {"c": 42}}})
print(data.get("a", {}).get("b", {}).get("c")) # Output: 42
print(data.get("a", {}).get("d", {}).get("c")) # Output: None
License
SafeDict is licensed under the MIT License, allowing you to use and modify it freely.
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 safe_dict-1.0.7.tar.gz.
File metadata
- Download URL: safe_dict-1.0.7.tar.gz
- Upload date:
- Size: 2.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8441ce2337ebc4ca5c5561897a870e21e07dc459105b325311fbd766c0ec3982
|
|
| MD5 |
77bd52539d998f9c726198fce72cde19
|
|
| BLAKE2b-256 |
1ff68adae4e42f54de30a655a3fbe81e27732ba3cfd2018da29886e00204eef0
|
File details
Details for the file safe_dict-1.0.7-py3-none-any.whl.
File metadata
- Download URL: safe_dict-1.0.7-py3-none-any.whl
- Upload date:
- Size: 3.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89cb1287bd5220bc0d2141532a1ac12c8cd90aca821c748924539dfb840158f7
|
|
| MD5 |
d140784c12ecdd3d2f20c315bb5aa1f6
|
|
| BLAKE2b-256 |
65a04c965cb6cc9631660ff3e8a629e2bf55a98157c3aae8887ec140c162a483
|