Prettier error handling for Python
Reason this release was yanked:
Commit pushed did not pass tests
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.3.tar.gz
(3.7 kB
view details)
Built Distribution
tribi-1.0.3-py3-none-any.whl
(5.1 kB
view details)
File details
Details for the file tribi-1.0.3.tar.gz
.
File metadata
- Download URL: tribi-1.0.3.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 | 741be425d00acf01f734e4a482201d83150d03f32c5d3fed33d2bdee581ee4f1 |
|
MD5 | 9d4839a6bbf7f40ce843ea887054521e |
|
BLAKE2b-256 | ab3b674e35a99c9c4daf91b1ef0dd85a8d02b7fc0a4bc750f6ff6530abd3ccc8 |
File details
Details for the file tribi-1.0.3-py3-none-any.whl
.
File metadata
- Download URL: tribi-1.0.3-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 | 60a39011c86fffbd86c1ace04db8a55b30a41a0487d7c32cb1d70e81957f83f5 |
|
MD5 | de75ea03ac43b64b5f153628d4ee4b81 |
|
BLAKE2b-256 | bab07eddf9cddcce19fcfb8f48f66f3551841529e79405ba9ede7bc1ccf1964b |