gpt4 book ai didi

Python Argparse 重用属性类型错误 : hasattr(): attribute name must be string

转载 作者:行者123 更新时间:2023-12-01 05:26:23 25 4
gpt4 key购买 nike

在 Python 2.7 中使用 argparse 我试图通过不重复代码并使用代码进行重用来减少我的代码。在本例中,我尝试重用给予不同子解析器的参数。但是,我收到一个错误,我认为这可能是我尝试调用/分配属性的方式造成的:

import argparse

def create_args():
parser = argparse.ArgumentParser(description='Transfer file between accounts.'
'Add/remove tabs and Schemas')
subparser = parser.add_subparsers(help='Transfer file between acconts')
transfer = subparser.add_parser('transfer',
help="Transfer file from 2 accounts")

add_arguments(transfer, "producer", "consumer", "uuid")

return parser


def add_arguments(group, *args):
options = group.add_argument_group("Switches")

choices = {
"producer" : ('-p',
'--producer',
'required=True',
'help="Producer\'s(source account)"'),
"consumer" : ('-c',
'--consumer',
'required=True',
'help="Consumer\'s(destination account)"'),
"uuid" : ('-u',
'--uuid',
'required=True',
'help="file ID number"')}
for arg in args:
options.add_argument(choices[arg])
<小时/>
   one@dash ~/image_transfer $ python transfer.py transfer
Traceback (most recent call last):
File "transfer.py", line 19, in <module>
start()
File "transfer.py", line 12, in start
args = vars(parser.parse_args())
File "/usr/lib/python2.7/argparse.py", line 1688, in parse_args
args, argv = self.parse_known_args(args, namespace)
File "/usr/lib/python2.7/argparse.py", line 1720, in parse_known_args
namespace, args = self._parse_known_args(args, namespace)
File "/usr/lib/python2.7/argparse.py", line 1929, in _parse_known_args
stop_index = consume_positionals(start_index)
File "/usr/lib/python2.7/argparse.py", line 1885, in consume_positionals
take_action(action, args)
File "/usr/lib/python2.7/argparse.py", line 1794, in take_action
action(self, namespace, argument_values, option_string)
File "/usr/lib/python2.7/argparse.py", line 1090, in __call__
namespace, arg_strings = parser.parse_known_args(arg_strings, namespace)
File "/usr/lib/python2.7/argparse.py", line 1706, in parse_known_args
if not hasattr(namespace, action.dest):
TypeError: hasattr(): attribute name must be string
<小时/>
def start():

parser = create_args()
args = vars(parser.parse_args())



if __name__ == "__main__":
start()

最佳答案

您可能想看看这个:

choices = {..
"producer" : (
('-p', '--producer'),
{'required':True, 'help': "Producer\'s(source account)"}
),
"consumer" : (
('-c', '--consumer'),
{'required':True, 'help': "Consumer\'s(destination account)"}
),
"uuid" : (
('-u', '--uuid'),
{'required':True, 'help': "file ID number"}
)
}
for arg in args:
options.add_argument(*choices[arg][0], **choices[arg][1])

add_argument 的方法签名如下所示:

add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])

名称或标志部分作为 *args 接收,因此其他所有内容都必须命名。您可以通过使用 **kwargs 扩展字典来传递命名参数。在您的情况下,您传递了一个元组,并且它试图将它(因为它是单个参数)用作名称或标志并出现错误。

关于Python Argparse 重用属性类型错误 : hasattr(): attribute name must be string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21267126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com