Skip to main content

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


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.4.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

tribi-1.0.4-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file tribi-1.0.4.tar.gz.

File metadata

  • Download URL: tribi-1.0.4.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

Hashes for tribi-1.0.4.tar.gz
Algorithm Hash digest
SHA256 9aa03e960173121ca3f98217be767befbe080465db97bd0d348b7c7e418bb632
MD5 abd1e433d2bc3ef3c5ed116cf6174dbf
BLAKE2b-256 863b0062252c6a74d1dadff56e61536018124e4d5bbe16cb07604c92a4c2975c

See more details on using hashes here.

File details

Details for the file tribi-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: tribi-1.0.4-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

Hashes for tribi-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 61dba3216693f9ce4b9bde0b97a0e15ea0452f44069098b5225dc91576a36e26
MD5 7fe5532138e10bc4c9f7da15e8921c2e
BLAKE2b-256 6dd081fbe976cc19c0a4c40f703b26928f61d3dd109863814654c221ce9dd4a4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page