Prettier error handling for Python
Project description
Tri/bi
This package provides wrappers around functions that nicely handle errors. Reducing code clutter and allowing better responses to uses. This package is inspired by tri-fp
Tri
Tri still lets native exceptions (usually more severe errors) throw, but catches any other errors
>>> from tri import tri, bi
>>> def my_function():
... return 1 / 0 # Zero division error!
>>> # This function raises a "native" exception
>>> tri(my_function)()
Traceback (most recent call last):
...
ZeroDivisionError: division by zero
>>> def my_other_function():
... raise Exception
>>> # This only raises a standard exception, so doesn't fail
>>> tri(my_other_function)()
(Exception(), None)
Bi
Bi, on the contrary, handles all errors blindly. This should only be used if you know what you're doing.
>>> bi(my_function)()
(ZeroDivisionError('division by zero'), None)
Async
Both tri
and bi
support async.
Tri
>>> # Assuming running in async
>>> from tri import atri, abi
>>> async def divide(a, b):
... return a / b
>>> # Let's try to divide by zero
>>> await atri(divide(1, 0))
Traceback (most recent call last):
...
ZeroDivisionError: division by zero
>>> # And with abi?
>>> await abi(divide(1, 0))
(ZeroDivisionError('division by zero'), None)
>>> # If the function takes no params, we can just pass the function name
>>> async def func():
... return "hello"
>>> await atri(func)
(None, "hello")
Real life example
Some examples include...
Safer JSON loading
from tri import bi
safer_loads = bi(json.loads)
error, result = safer_loads(data)
if error:
# Error handling JSON
print("Invalid JSON")
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
tribi-1.0.2.tar.gz
(3.7 kB
view details)
Built Distribution
tribi-1.0.2-py3-none-any.whl
(5.1 kB
view details)
File details
Details for the file tribi-1.0.2.tar.gz
.
File metadata
- Download URL: tribi-1.0.2.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6444e508db772efce477c8fb50b492f470df4a2c82a55afbdbee7e48a401dbce |
|
MD5 | 151f1f3257b2708483dd29de24136fad |
|
BLAKE2b-256 | 46e5c8a8ff8859a50290bc51556500de562b367be7283335ff1d3333f24568b9 |
File details
Details for the file tribi-1.0.2-py3-none-any.whl
.
File metadata
- Download URL: tribi-1.0.2-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ccca41473df7831809ce58abce7bdd4af9e2008ce42decd283ebb2b349ab2a8 |
|
MD5 | c1732794e77b0e20b23e3955f25d17e4 |
|
BLAKE2b-256 | d30e47484e901a848d02d0704cda473af38e285c21b768933bf3ec8ce228f594 |