Python OptsParse属性检查。。我想
我有一个opts解析。 例: 现在我想买一本字典。问题在于它是opts的一个属性。 所以我试着:Python OptsParse属性检查。。我想,python,parsing,dictionary,arguments,Python,Parsing,Dictionary,Arguments,我有一个opts解析。 例: 现在我想买一本字典。问题在于它是opts的一个属性。 所以我试着: for x in opts.__dict__.iterkeys(): if x = 'bblah': callfunction1(opts.bblah[0],opts.bblah[1]) if x = 'ablah': callfunction2(opts.ablah[0],opts.ablah[1]) if x = 'cblah':
for x in opts.__dict__.iterkeys():
if x = 'bblah':
callfunction1(opts.bblah[0],opts.bblah[1])
if x = 'ablah':
callfunction2(opts.ablah[0],opts.ablah[1])
if x = 'cblah':
callfunction3(opts.cblah[0],opts.cblah[1])
这里的问题有两个方面
谢谢。我不确定我是否正确理解了您的问题,但这可能会给您一个提示:
# map option name to function
func_map = {'ablah': ablah,
'bblah': bblah,
'cblah': cblah}
# filter out the options without argument
# this may be redundant, optparse can make proper validations
selected = filter(lambda x: x[1], opts.__dict__.items())
for k, v in selected:
func_map[k](*v)
# map option name to function
func_map = {'ablah': ablah,
'bblah': bblah,
'cblah': cblah}
# filter out the options without argument
# this may be redundant, optparse can make proper validations
selected = filter(lambda x: x[1], opts.__dict__.items())
for k, v in selected:
func_map[k](*v)