A none-safe wrapper for containers
Project description
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
Project details
Release history Release notifications | RSS feed
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 nonesafe_ex-0.1.2.tar.gz.
File metadata
- Download URL: nonesafe_ex-0.1.2.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.3.2 CPython/3.11.9 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7454024f9996653dd976ead505742568054f223a5c70c2789ea3264171352f7a
|
|
| MD5 |
a77fc107735fee16dff28aa4efa3d219
|
|
| BLAKE2b-256 |
6f2d88ed1cef38d2e5c9c15f0c9c318fbbe070419cba3dffd60b5a2cc061ec31
|
File details
Details for the file nonesafe_ex-0.1.2-py3-none-any.whl.
File metadata
- Download URL: nonesafe_ex-0.1.2-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.3.2 CPython/3.11.9 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b1260ecbba6838493de18f1beff8bdc77cbb2b80604004bac323912d2c7a92f
|
|
| MD5 |
1e127161af8418786c4263c81c2ec8b1
|
|
| BLAKE2b-256 |
fd29582e2c58c56139e0f813529f0ded8ebebb4175f8e74236f8f29e90032f6a
|