如何通过分解两个非常相似的 classes 来为 Argparse 在 class 中传递参数
How to pass parameter in a class for Argparse by factoring two very similar classes
我有以下代码:
def main(args):
"""
Description of main
"""
print args
if __name__ == '__main__':
class DefaultListAction(argparse.Action):
CHOICES = ['ann','netmhcpan','comblib_sidney2008','consensus','smm','smmpmbec','netmhccons']
def __call__(self, parser, namespace, values, option_string=None):
if values:
for value in values:
if value not in self.CHOICES:
message = ("invalid choice: {0!r} (choose from {1})"
.format(value,
', '.join([repr(action)
for action in self.CHOICES])))
raise argparse.ArgumentError(self, message)
setattr(namespace, self.dest, values)
class DefaultListAction_Frames(argparse.Action):
CHOICES = ['R','F','6']
def __call__(self, parser, namespace, values, option_string=None):
if values:
for value in values:
if value not in self.CHOICES:
message = ("invalid choice: {0!r} (choose from {1})"
.format(value,
', '.join([repr(action)
for action in self.CHOICES])))
raise argparse.ArgumentError(self, message)
setattr(namespace, self.dest, values)
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("-iedb",help="IEDB tools options: ann, comblib_sydney2008, consensus, netmhcpan, smm, smmpmbec, netmhccons", \
action=DefaultListAction, nargs='*', default=[''], \
metavar='iedb_tools')
parser.add_argument("-f",help="Frame to translate insert: (F)orward three frames, (R)everse three frames or (6) frames F + B. Default F.", \
action=DefaultListAction_Frames, nargs=1, default=['F'], \
metavar='frames')
args = parser.parse_args()
main(args)
基本上,有两个 argparse.ArgumentParser.add_argument()
,每个都将 class 之一作为 action
中的参数。
我的问题是如何分解 class DefaultListAction(argparse.Action)
和 class DefaultListAction_Frames(argparse.Action)
,因为两者之间的唯一区别是 CHOICES
参数。
以及如何将那些 CHOICES
作为参数传递给 argparse.ArgumentParser.add_argument()
add_argument
choices
参数可用 self.choices
。它没有在任何现有的 Action
subclasses 中使用,但没有理由不使用它。
解析器将使用它在将值传递给 Action.__call__
之前测试这些值。在这个测试中似乎与您自己的使用没有冲突,但我不能排除这种可能性。
action.choices
也用于帮助格式化,尽管 metavar
参数覆盖了它。
class ListAction(argparse.Action):
# change self.CHOICES to self.choices
def __call__(self, parser, namespace, values, option_string=None):
if values:
for value in values:
if value not in self.choices:
message = ("invalid choice: {0!r} (choose from {1})"
.format(value,
', '.join([repr(action)
for action in self.choices])))
raise argparse.ArgumentError(self, message)
setattr(namespace, self.dest, values)
# should behave just like the -f argument
parser.add_argument("-g",help="Frame to translate insert: (F)orward three frames, (R)everse three frames or (6) frames F + B. Default F.", \
action=ListAction, nargs=1, default=['F'], \
choices=['R','F','6'])
帮助允许 choices
作为元变量(用于说明目的)
2304:~/mypy$ python stack41562756.py -h
usage: stack41562756.py [-h] [-iedb [iedb_tools [iedb_tools ...]]] [-f frames]
[-g {R,F,6}]
optional arguments:
-h, --help show this help message and exit
-iedb [iedb_tools [iedb_tools ...]]
IEDB tools options: ann, comblib_sydney2008, consensus, netmhcpan, smm, smmpmbec, netmhccons
-f frames Frame to translate insert: (F)orward three frames, (R)everse three frames or (6) frames F + B. Default F.
-g {R,F,6} Frame to translate insert: (F)orward three frames, (R)everse three frames or (6) frames F + B. Default F.
解析时action.choices
只用在_check_value
中,被_get_values
调用。
def _check_value(self, action, value):
# converted value must be one of the choices (if specified)
if action.choices is not None and value not in action.choices:
args = {'value': value,
'choices': ', '.join(map(repr, action.choices))}
msg = _('invalid choice: %(value)r (choose from %(choices)s)')
raise ArgumentError(action, msg % args)
手头看起来你可以使用默认的 store
操作 choices
:
parser.add_argument("-e",help="Frame to translate insert: (F)orward three frames, (R)everse three frames or (6) frames F + B. Default F.", \
nargs=1, default=['F'], choices=['R','F','6'])
我在您的自定义操作中没有发现任何不同之处。但是我没有详细研究或测试过它们。
=================
另一种方法是仅子class您的一个新操作:
class DefaultListAction_Frames(DefaultListAction):
CHOICES = ['R','F','6','X']
如果__call__
方法一样,就不用重复了
另一种方法是使用工厂函数为每个 DefaultListAction
动作赋予其自己的 CHOICES
属性。 FileType
就是这样一个 class - 它创建了一个自定义 type
函数。
type
函数是您可以自定义值检查的另一个地方。 type
用于值转换和测试,而自定义操作 class 如果您想以某种特殊方式保存值,则最有用。
我有以下代码:
def main(args):
"""
Description of main
"""
print args
if __name__ == '__main__':
class DefaultListAction(argparse.Action):
CHOICES = ['ann','netmhcpan','comblib_sidney2008','consensus','smm','smmpmbec','netmhccons']
def __call__(self, parser, namespace, values, option_string=None):
if values:
for value in values:
if value not in self.CHOICES:
message = ("invalid choice: {0!r} (choose from {1})"
.format(value,
', '.join([repr(action)
for action in self.CHOICES])))
raise argparse.ArgumentError(self, message)
setattr(namespace, self.dest, values)
class DefaultListAction_Frames(argparse.Action):
CHOICES = ['R','F','6']
def __call__(self, parser, namespace, values, option_string=None):
if values:
for value in values:
if value not in self.CHOICES:
message = ("invalid choice: {0!r} (choose from {1})"
.format(value,
', '.join([repr(action)
for action in self.CHOICES])))
raise argparse.ArgumentError(self, message)
setattr(namespace, self.dest, values)
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("-iedb",help="IEDB tools options: ann, comblib_sydney2008, consensus, netmhcpan, smm, smmpmbec, netmhccons", \
action=DefaultListAction, nargs='*', default=[''], \
metavar='iedb_tools')
parser.add_argument("-f",help="Frame to translate insert: (F)orward three frames, (R)everse three frames or (6) frames F + B. Default F.", \
action=DefaultListAction_Frames, nargs=1, default=['F'], \
metavar='frames')
args = parser.parse_args()
main(args)
基本上,有两个 argparse.ArgumentParser.add_argument()
,每个都将 class 之一作为 action
中的参数。
我的问题是如何分解 class DefaultListAction(argparse.Action)
和 class DefaultListAction_Frames(argparse.Action)
,因为两者之间的唯一区别是 CHOICES
参数。
以及如何将那些 CHOICES
作为参数传递给 argparse.ArgumentParser.add_argument()
add_argument
choices
参数可用 self.choices
。它没有在任何现有的 Action
subclasses 中使用,但没有理由不使用它。
解析器将使用它在将值传递给 Action.__call__
之前测试这些值。在这个测试中似乎与您自己的使用没有冲突,但我不能排除这种可能性。
action.choices
也用于帮助格式化,尽管 metavar
参数覆盖了它。
class ListAction(argparse.Action):
# change self.CHOICES to self.choices
def __call__(self, parser, namespace, values, option_string=None):
if values:
for value in values:
if value not in self.choices:
message = ("invalid choice: {0!r} (choose from {1})"
.format(value,
', '.join([repr(action)
for action in self.choices])))
raise argparse.ArgumentError(self, message)
setattr(namespace, self.dest, values)
# should behave just like the -f argument
parser.add_argument("-g",help="Frame to translate insert: (F)orward three frames, (R)everse three frames or (6) frames F + B. Default F.", \
action=ListAction, nargs=1, default=['F'], \
choices=['R','F','6'])
帮助允许 choices
作为元变量(用于说明目的)
2304:~/mypy$ python stack41562756.py -h
usage: stack41562756.py [-h] [-iedb [iedb_tools [iedb_tools ...]]] [-f frames]
[-g {R,F,6}]
optional arguments:
-h, --help show this help message and exit
-iedb [iedb_tools [iedb_tools ...]]
IEDB tools options: ann, comblib_sydney2008, consensus, netmhcpan, smm, smmpmbec, netmhccons
-f frames Frame to translate insert: (F)orward three frames, (R)everse three frames or (6) frames F + B. Default F.
-g {R,F,6} Frame to translate insert: (F)orward three frames, (R)everse three frames or (6) frames F + B. Default F.
解析时action.choices
只用在_check_value
中,被_get_values
调用。
def _check_value(self, action, value):
# converted value must be one of the choices (if specified)
if action.choices is not None and value not in action.choices:
args = {'value': value,
'choices': ', '.join(map(repr, action.choices))}
msg = _('invalid choice: %(value)r (choose from %(choices)s)')
raise ArgumentError(action, msg % args)
手头看起来你可以使用默认的 store
操作 choices
:
parser.add_argument("-e",help="Frame to translate insert: (F)orward three frames, (R)everse three frames or (6) frames F + B. Default F.", \
nargs=1, default=['F'], choices=['R','F','6'])
我在您的自定义操作中没有发现任何不同之处。但是我没有详细研究或测试过它们。
=================
另一种方法是仅子class您的一个新操作:
class DefaultListAction_Frames(DefaultListAction):
CHOICES = ['R','F','6','X']
如果__call__
方法一样,就不用重复了
另一种方法是使用工厂函数为每个 DefaultListAction
动作赋予其自己的 CHOICES
属性。 FileType
就是这样一个 class - 它创建了一个自定义 type
函数。
type
函数是您可以自定义值检查的另一个地方。 type
用于值转换和测试,而自定义操作 class 如果您想以某种特殊方式保存值,则最有用。