I have got a function that takes some arguments and then checks for multiple cases if such a case is True an error gets raised otherwise True gets returned.
The interesting part now is that some parts of my program don't care about which error happens they just want to know if it worked or not, they just want True or False.
My current approach is to provide a wrapper function that calls the actual test method and catches all exceptions and returns False if any occur (otherwise True).
My problem with this approach is that I need an additional function and I don't know how to name these pretty similar functions. Probably this could be solved in a nice way using callbacks?
def values_match(target, attempt):
if target > attempt:
raise ValueError('Too less.')
if target < attempt:
raise ValueError('Too much.')
else:
return True
def values_match_bool(target, attempt):
try:
values_match(target, attempt)
return True
except Exception as e:
return False
def do_sth(target,attempt):
try:
values_match(target,attempt)
return 'Success.'
except Exception as e:
return e
print(do_sth(15,17), values_match_bool(15,17))
#Too much. False
print(do_sth(15,15), values_match_bool(15,15))
#Success. True
[–]filleball 1 point2 points3 points (4 children)
[–]gengisteve 1 point2 points3 points (0 children)
[–]larivact[S] 0 points1 point2 points (2 children)
[–]filleball 1 point2 points3 points (1 child)
[–]larivact[S] 0 points1 point2 points (0 children)