NoneSafe-EX
A None-safe wrapper for Python containers, preventing KeyError, IndexError, and AttributeError when accessing missing or None values.
** Warn: NoneLike is not None. Please do not use NoneLike is None to check if a value is None. == None or is NoneLike is recommended.**
Features
- NoneSafeDict: None-safe dictionary - returns
NoneLikeinstead of raisingKeyError - NoneSafeList: None-safe list - returns
NoneLikefor out-of-bounds access - NoneLike: Sentinel object that supports arbitrary chained access without raising exceptions
Installation
pip install nonesafe_ex
Usage
NoneSafeDict
from nonesafe_ex import NoneSafeDict
data = NoneSafeDict({"name": "Alice", "info": {"age": 25}})
# Access existing key
print(data["name"]) # "Alice"
# Access non-existing key - returns NoneLike instead of KeyError
print(data["non_existing"]) # None
print(bool(data["non_existing"])) # False
# Nested access with automatic wrapping
print(data["info"]["age"]) # 25
print(data["info"]["non_existing"]) # None (NoneLike)
# Safe chained access
result = data["info"]["address"]["city"]
print(result) # None
NoneSafeList
from nonesafe import NoneSafeList
items = NoneSafeList([1, 2, {"value": 3}])
# Access existing index
print(items[0]) # 1
# Out-of-bounds access - returns NoneLike instead of IndexError
print(items[100]) # None
print(items[-100]) # None
# Nested access with automatic wrapping
print(items[2]["value"]) # 3
print(items[2]["non_existing"]) # None
# Iteration with automatic wrapping
for item in items:
print(item)
NoneLike
from nonesafe import NoneLike
# Supports arbitrary chained access
result = NoneLike.foo.bar.baz()["key"]
print(result) # None
print(bool(result)) # False
# Safe for any operation
print(NoneLike + 1) # None
print(NoneLike * 2) # None
print(len(NoneLike)) # 0
API
NoneSafeDict
__getitem__(key)- Returns wrapped value orNoneLikeif key not foundget(key, default=None)- Returns wrapped value or wrapped defaultvalues()- Returns wrapped values viewitems()- Returns items view with wrapped valuespop(key, default=None)- Removes and returns wrapped valuecopy()- Returns new NoneSafeDict
NoneSafeList
__getitem__(index)- Returns wrapped value orNoneLikeif out of boundspop(index=-1)- Removes and returns wrapped valuecopy()- Returns new NoneSafeListfirst(default=None)- Returns first item or wrapped defaultlast(default=None)- Returns last item or wrapped default
NoneLike
- Supports chained attribute access:
NoneLike.attr.subattr - Supports chained item access:
NoneLike["key"]["subkey"] - Supports function calls:
NoneLike.method() - Boolean value is
False - String representation is
"None"
License
MIT
Release files for nonesafe_ex 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| nonesafe_ex-0.1.2.tar.gz | 4.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| nonesafe_ex-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.5 kB
Release files / nonesafe_ex-0.1.2.tar.gz
| Download URL | nonesafe_ex-0.1.2.tar.gz |
|---|---|
| Size | 4.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7454024f9996653dd976ead505742568054f223a5c70c2789ea3264171352f7a
|
|
BLAKE2b-256 checksum How to use checksums |
6f2d88ed1cef38d2e5c9c15f0c9c318fbbe070419cba3dffd60b5a2cc061ec31
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.3.2 CPython/3.11.9 Windows/10
|
Release files / nonesafe_ex-0.1.2-py3-none-any.whl
| Download URL | nonesafe_ex-0.1.2-py3-none-any.whl |
|---|---|
| Size | 6.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2b1260ecbba6838493de18f1beff8bdc77cbb2b80604004bac323912d2c7a92f
|
|
BLAKE2b-256 checksum How to use checksums |
fd29582e2c58c56139e0f813529f0ded8ebebb4175f8e74236f8f29e90032f6a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.3.2 CPython/3.11.9 Windows/10
|