我在linux中使用python运行一个命令,我有几种不同的基于系统类型的验证方法。我的问题是我可以使验证功能获取功能列表而不是创建所有组合。
目前我有:
def verify_A(*args):
checks the commands was successful using method A
def verify_B(*args):
checks the commands was successful using method B
def run_command(*args)
runs the commands on linux terminal, no checking
def run_and_verify_A(*args):
run_command(*args)
verify_A(*args)
def run_and_verify_B(*args):
run_command(*args)
verify_B(*args)
def run_and_verify_All(*args):
run_command(*args)
if not verify_A(*args):
verify_B(*args)
我想要的是:
def run_command(*args)
runs the commands on linux terminal, no checking
verify_list=['verify_A','verify_B']
def run_and_verify(verify_list):
run_command(*args)
for func in verify_list:
if eval(func):
print "verification passed"
return True
else:
print "verification is failed... running next verify method"
但是我的run_and verify函数没有按预期工作..
答案 0 :(得分:1)
您可以将功能放在列表中:
def foo():
return something
def bar():
return stuff
lst = [foo, bar]
for func in lst:
if func()
print('passed...')
break # or return, however you decide to set it up
else:
print('failed...')
答案 1 :(得分:0)
考虑到所有函数具有相同的签名,请创建函数列表并使用any
按指定的顺序进行评估。 any
的短路属性基本上可以帮助您忽略过去成功的所有功能
示例强>
verify = [verify_A, verify_B, run_and_verify_A,
run_and_verify_B, run_and_verify_Allrun_and_verify_All]
def run_and_verify(verify_list, *args):
run_command(*args)
if any(fn(*args) for fn in verify_list):
print "verification passed"
else:
print "verification is failed... running next verify method"