I have functions, that return validator functions, simple example:
def check_len(n):
return lambda s: len(s) == n
Is it possible to add a decorator, that prints out a message, in case the check evaluates to false? Something like this:
@log_false_but_how
def check_len(n):
return lambda s: len(s) == n
check_one = check_len(1)
print(check_one('a')) # returns True
print(check_one('abc')) # return False
Expected output:
True
validator evaluated to False
False
I've tried creating an annotation, but can only access the function creation with it. One way would be to define the functions like this:
def log_false(fn):
def inner(*args):
res = fn(*args)
if not res:
print("validation failed for {}".format(fn.__name__))
return res
return inner
@log_false
def check_one(s):
return check_len(1)(s)
But this way we lose the dynamic creation of validation functions.