IfRaise is a simple python decorator used to avoid a repetitive try/except pattern. (See examples)
Python
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
ifraise.py
readme

readme

IfRaise is a simple decorator used to avoid a repetitive try/except pattern.

Example:

def get_user(username):
    try:
        return DB.find_user(username=username)
    except: UserNotFound:
        return None

Can be replaced with:

@IfRaise(UserNotFound, None)
def get_user(username): 
    return DB.find_user(username=username)

------------------------

Syntax:

@IfRaise(Exception, Object)

where:

if Object is an exception, 
    it is raised

if Object is a callable, 
    it is called and the result is either returned or raised

otherwise, the object is returned.

-----------------------

See the tests for more examples.