![](/img/trans.png)
[英]What is the pythonic way of creating a pandas variable from multiple other variables
[英]What is the pythonic way to execute multiple conditions, one after the other, while showing to the user which is the first to fail
假設我有這個有效的代碼:
def check_some_conds(args):
assert (condA(args) or
condB(args))
assert condC(args)
assert condD(args)
return True
(是的,我知道assert
很糟糕,但請耐心等一下)
現在,當我調用這個函數時,我可以(或多或少)通過使用except AssertionError:
來回溯第一個失敗的斷言,並使用traceback模塊做一些魔術。
但是,這並不理想,因為:
assert
在python中進行了優化 我正在尋找一種方法:
condD
可能需要幾秒鍾 a == b
) condD failed
或nor condA or condB succeded
如果這不可能,請隨時建議我的問題的部分答案
循環你的條件並在最后提出錯誤:
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 ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.