Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Decorator for adding obvious assertions to functions
def obviously(v, msg=''):
def outer(f):
def inner(*args, **kwargs):
r = f(*args, **kwargs)
if not v(r):
if msg:
errmsg = f.__name__ + ': ' + msg
else:
errmsg = f.__name__ + ': obviously not true'
raise Exception(errmsg)
return r
return inner
return outer
@obviously(lambda x: 0 <= x <= 1, "Probabilities must be between 0 and 1")
def my_probability_thingy(x):
return x
my_probability_thingy(0)
my_probability_thingy(0.5)
my_probability_thingy(1)
my_probability_thingy(2)
my_probability_thingy(-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.