Converts caught exception into bool value
Project description
exceptbool
Converts caught exception into bool value.
Free software: MIT license
Documentation: https://exceptbool.readthedocs.io.
Features
How many of those have you written in your life?
def is_something_possible():
try:
do_something()
return True
except DoingSomethingError:
return False
Ugh! A perfect example of six-line boilerplate code. With exceptbool you can shorten that into only three lines!
@except_to_bool(exc=DoingSomethingError)
def is_something_possible():
do_something()
Exceptbool makes decorated function return bool instead of raising an exception by converting given exception(s) into given bool value. If no exception will be raised, then negation of given bool will be returned. If exception different than given one will be raised, then it will not be caught.
Installation
Stable release
To install exceptbool, run this command in your terminal:
$ pip install exceptbool
This is the preferred method to install exceptbool, as it will always install the most recent stable release.
If you don’t have pip installed, this Python installation guide can guide you through the process.
From sources
The sources for exceptbool can be downloaded from the Github repo.
You can either clone the public repository:
$ git clone git://github.com/konrad-kocik/exceptbool
Or download the tarball:
$ curl -OL https://github.com/konrad-kocik/exceptbool/tarball/master
Once you have a copy of the source, you can install it with:
$ python setup.py install
Usage
First, import except_to_bool decorator into current namespace:
from exceptbool import except_to_bool
To catch any exception and convert it into False:
@except_to_bool
def decorated_function():
error_raising_function()
Now decorated_function will return False if error_raising_function raises Exception, True otherwise.
To catch given exception and convert it into given bool value:
@except_to_bool(exc=ValueError, to=True)
def decorated_function():
error_raising_function()
Now decorated_function will return True if error_raising_function raises ValueError, False otherwise.
To catch any of multiple exceptions:
@except_to_bool(exc=(TypeError, TimeoutError))
def decorated_function():
error_raising_function()
Now decorated_function will return False if error_raising_function raises TypeError or TimeoutError, True otherwise.
Function decorated with except_to_bool is perfectly capable of accepting positional and keyword arguments:
@except_to_bool
def decorated_function(*args, **kwargs):
error_raising_function(*args, **kwargs)
decorated_function("foo", bar="baz") # no error
History
1.0.1 (2018-12-12)
Updated and improved documentation.
1.0.0 (2018-12-10)
First release on PyPI with basic functionality.
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
Built Distribution
File details
Details for the file exceptbool-1.0.1.tar.gz
.
File metadata
- Download URL: exceptbool-1.0.1.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8133248083f66d2d3399da9d0529a16f349f69a99eebfd8c9c76daa71e9d3d69 |
|
MD5 | 0d21a898e3287d432b7651397fb36a52 |
|
BLAKE2b-256 | 2b687bbec7b562144d270c0b8974a56e739d6a3f84fb0a9b0ea2c02e6ca9b734 |
File details
Details for the file exceptbool-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: exceptbool-1.0.1-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8962ade4bdeca188af61cd4ec6dd2ba140dc7d3ae98d6c0e83079a1c2243549 |
|
MD5 | e08d1e7a9c6843c3dcd1a7ffea507e2b |
|
BLAKE2b-256 | 76d30808bd1744a5ee0c28761a3e1235a1e9e5b1053c95e0af56984ec3bc8b04 |