Reputation: 1750
Let's say I have this working code:
def check_some_conds(args):
assert (condA(args) or
condB(args))
assert condC(args)
assert condD(args)
return True
(Yeah, I know that assert
s are bad, but please bear with me for a moment)
Now, when I'm calling this function, I can (more or less), retrive the first failed assertion by using a except AssertionError:
, and by doing some magic with the traceback module.
However, this is not ideal because:
assert
is optimised-out in pythonI'm looking for a way to:
condD
may take secondsa == b
)condD failed
or nor condA or condB succeded
If that's not really possible, please feel free to suggest partial answers to my problem
Upvotes: 1
Views: 92
Reputation: 13498
Loop through your conditions and raise errors at the end:
fail = False
for condition in [cond1, cond2, cond3 ...]:
if not condition(args):
print(condition.__name__, "failed")
fail = True
else:
print(condition.__name__, "succeeded")
if fail: raise ...
Upvotes: 3