Package: exceptbool
exceptboolConverts caught exception into bool value.Free software:MIT licenseDocumentation:https://exceptbool.readthedocs.io.Issue reporting:https://github.com/konrad-kocik/exceptbool/issues/newFeaturesHow many of those have you written in your life?defis_something_possible():try:do_something()returnTrueexceptDoingSomethingError:returnFalseUgh! A perfect example of six-line boilerplate code. With exceptbool you can shorten that into only three lines!@except_to_bool(exc=DoingSomethingError)defis_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:withexcept_converter(exc=DoingSomethingError)asconverted_exception:do_something()InstallationStable releaseTo install exceptbool, run this command in your terminal:$pipinstallexceptboolThis is the preferred method to install exceptbool, as it will always install the most recent stable release.If you don’t havepipinstalled, thisPython installation guidecan guide
you through the process.From sourcesThe sources for exceptbool can be downloaded from theGithub repo.You can either clone the public repository:$gitclonehttps://github.com/konrad-kocik/exceptbool.gitOr download thetarball:$curl-OLhttps://github.com/konrad-kocik/exceptbool/tarball/masterOnce you have a copy of the source, you can install it with:$pythonsetup.pyinstallUsageAs decoratorFirst, importexcept_to_booldecorator into current namespace:fromexceptboolimportexcept_to_boolTo catch any exception and convert it into False:@except_to_booldefdecorated_function():error_raising_function()Nowdecorated_functionwill return False iferror_raising_functionraises Exception, True otherwise.To catch given exception and convert it into given bool value:@except_to_bool(exc=ValueError,to=True)defdecorated_function():error_raising_function()Nowdecorated_functionwill return True iferror_raising_functionraises ValueError, False otherwise.To catch any of multiple exceptions:@except_to_bool(exc=(TypeError,TimeoutError))defdecorated_function():error_raising_function()Nowdecorated_functionwill return False iferror_raising_functionraises TypeError or TimeoutError, True otherwise.Function decorated withexcept_to_boolis perfectly capable of accepting positional and keyword arguments:@except_to_booldefdecorated_function(*args,**kwargs):error_raising_function(*args,**kwargs)decorated_function("foo",bar="baz")# no errorAs context managerFirst, importexcept_convertercontext manager into current namespace:fromexceptboolimportexcept_converterTo catch any exception and convert it into False:withexcept_converter()asconverted_exception:error_raising_function()Nowconverted_exception.valuewill return False iferror_raising_functionraises Exception, True otherwise.To catch given exception and convert it into given bool value:withexcept_converter(exc=ValueError,to=True)asconverted_exception:error_raising_function()Nowconverted_exception.valuewill return True iferror_raising_functionraises ValueError, False otherwise.To catch any of multiple exceptions:withexcept_converter(exc=(OSError,KeyError))asconverted_exception:error_raising_function()Nowconverted_exception.valuewill return False iferror_raising_functionraises OSError or KeyError, True otherwise.History1.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.