Skip to main content

Converts caught exception into bool value.

Project description

exceptbool

https://img.shields.io/pypi/v/exceptbool.svg https://pepy.tech/badge/exceptbool https://travis-ci.org/konrad-kocik/exceptbool.svg?branch=master https://readthedocs.org/projects/exceptbool/badge/?version=latest

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)
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.

Don’t want to decorate whole function? Fine, you can convert exception raised from chosen block of code by using context manager:

with except_converter(exc=DoingSomethingError) as converted_exception:
    do_something()

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 https://github.com/konrad-kocik/exceptbool.git

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

As decorator

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

As context manager

First, import except_converter context manager into current namespace:

from exceptbool import except_converter

To catch any exception and convert it into False:

with except_converter() as converted_exception:
    error_raising_function()

Now converted_exception.value will return False if error_raising_function raises Exception, True otherwise.

To catch given exception and convert it into given bool value:

with except_converter(exc=ValueError, to=True) as converted_exception:
   error_raising_function()

Now converted_exception.value will return True if error_raising_function raises ValueError, False otherwise.

To catch any of multiple exceptions:

with except_converter(exc=(OSError, KeyError)) as converted_exception:
   error_raising_function()

Now converted_exception.value will return False if error_raising_function raises OSError or KeyError, True otherwise.

History

1.2.1 (2019-01-19)

  • Improved documentation.

1.2.0 (2019-01-12)

  • Added possibility to convert exceptions by using context manager.

1.1.0 (2018-12-18)

  • Added support for Python 3.5.

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

exceptbool-1.2.1.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

exceptbool-1.2.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file exceptbool-1.2.1.tar.gz.

File metadata

  • Download URL: exceptbool-1.2.1.tar.gz
  • Upload date:
  • Size: 12.3 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

Hashes for exceptbool-1.2.1.tar.gz
Algorithm Hash digest
SHA256 5a431cb0c533555be5a51f81bf45fb2881da3876090a188e8247d1c04c4f81a3
MD5 5aaeab1692b28076fc711bd126d2746f
BLAKE2b-256 1904098ec5975a2b911279238f9ccb7f7279b417d2623bd1e3e1493a35b06fa6

See more details on using hashes here.

File details

Details for the file exceptbool-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: exceptbool-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 5.9 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

Hashes for exceptbool-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 16906ff3b72c924f1825d231121488efe94cfd02c159fd122d9bc83b44d65058
MD5 aebf324158b5ac1f40c9279560581064
BLAKE2b-256 2bd519100c6bf6967c9fe191968d46c712624bc7382e1163d7bb166dc8ac60eb

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