A tiny collection of simple error handling decorators.
Ignoring exception
import nah
@nah.meh
def check_age(age):
if age < 18:
raise UnderAgeException
check_age(17) # returns None
Returning a value
import nah
@nah.gimme(0)
def divide_by(n):
return 100 / n
divide_by(0) # returns 0 instead of raising ZeroDivisionError
Retry
@nah.again(3)
def connect_db(config):
return DbConnection.connect(config)
Alternative function
def alternative_func(a, b):
return safe_divide(a, b)
@nah.maybe(alternative_func)
def my_func(a, b):
return a / b
Other options
@nah.meh(when=ValueError) # filter ignored exception
@nah.meh(when=(ValueError, KeyError))
@nah.gimme(when=ValueError)
@nah.gimme(when=(ValueError, KeyError))
@nah.again(3, when=ValueError)
@nah.again(3, when=(ValueError, KeyError))
@nah.again(3, backoff=2.0) # back-off for 2 seconds
@nah.maybe(alternative_fn) # delegate the call to function `alternative_fn`
@nah.maybe(alternative_fn, when=ValueError)
@nah.maybe(alternative_fn, when=(ValueError, KeyError))
def my_func():
...
nah
is an open source project by me
. See the license file
for more information.