Skip to main content

Converts caught exception into bool value

Project description

exceptbool

https://img.shields.io/pypi/v/exceptbool.svg Documentation Status

Converts caught exception into bool value.

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, to=False)
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

To install exceptbool into your Python environment run following pip command:

$ pip install exceptbool

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

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

History

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

exceptbool-1.0.0.tar.gz (9.2 kB view hashes)

Uploaded Source

Built Distribution

exceptbool-1.0.0-py3-none-any.whl (4.2 kB view hashes)

Uploaded Python 3

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