【发布时间】:2015-09-14 10:17:31
【问题描述】:
我想我对 python 的 argparse 的一些基本知识不了解。
我正在尝试将 Google YouTube API 用于 python 脚本,但我不明白如何在不使用命令行的情况下将值传递给脚本。
例如,here 是 API 的示例。 github 和其他地方的示例显示此示例是从命令行调用的,调用脚本时从命令行传递 argparse 值。
我不想使用命令行。我正在构建一个应用程序,它使用装饰器获取用户的登录凭据,当该用户想要上传到他们的 YouTube 帐户时,他们提交一个表单,然后调用这个脚本并将 argparse 值传递给它。
如何将值从另一个 python 脚本传递给 argparser(请参阅下面的 YouTube 上传 API 脚本中的部分代码)?
if __name__ == '__main__':
argparser.add_argument("--file", required=True, help="Video file to upload")
argparser.add_argument("--title", help="Video title", default="Test Title")
argparser.add_argument("--description", help="Video description",
default="Test Description")
argparser.add_argument("--category", default="22",
help="Numeric video category. " +
"See https://developers.google.com/youtube/v3/docs/videoCategories/list")
argparser.add_argument("--keywords", help="Video keywords, comma separated",
default="")
argparser.add_argument("--privacyStatus", choices=VALID_PRIVACY_STATUSES,
default=VALID_PRIVACY_STATUSES[0], help="Video privacy status.")
args = argparser.parse_args()
if not os.path.exists(args.file):
exit("Please specify a valid file using the --file= parameter.")
youtube = get_authenticated_service(args)
try:
initialize_upload(youtube, args)
except HttpError, e:
print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
编辑:根据请求,这是我使用标准方法初始化字典或使用 argparse 创建字典时遇到的 400 错误的回溯。我以为我得到这个是因为参数格式错误,但也许不是:
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1102, in __call__
return handler.dispatch()
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "C:\Users\...\testapp\oauth2client\appengine.py", line 796, in setup_oauth
resp = method(request_handler, *args, **kwargs)
File "C:\Users\...\testapp\testapp.py", line 116, in get
resumable_upload(insert_request)
File "C:\Users\...\testapp\testapp.py", line 183, in resumable_upload
status, response = insert_request.next_chunk()
File "C:\Users\...\testapp\oauth2client\util.py", line 129, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Users\...\testapp\apiclient\http.py", line 874, in next_chunk
return self._process_response(resp, content)
File "C:\Users\...\testapp\apiclient\http.py", line 901, in _process_response
raise HttpError(resp, content, uri=self.uri)
HttpError: <HttpError 400 when requesting https://www.googleapis.com/upload/youtube/v3/videos?alt=json&part=status%2Csnippet&uploadType=resumable returned "Bad Request">
【问题讨论】:
-
最简单的方法是重构代码,这样
sys.argv被显式传递给使用argparse的函数来处理生成的list我>;这也使测试变得更加简单。参见例如github.com/textbook/py_wlc/blob/develop/py_wlc/data/… -
这里只有一个大问题,如果你不会使用命令行,为什么需要解析命令行参数?您只是从谷歌网站复制粘贴示例而不了解代码本身。
-
正如 JL Peyret 所展示的,argparse 可以在不使用命令行的情况下发挥作用。虽然我是 python 新手,但我试图理解代码,这就是为什么我尝试了不同的字典创建方法。我想两者都有效。我感谢人们尝试帮助我更好地理解 argparse 方法。因为我使用任何一种方法都会收到 400 错误,所以我显然需要了解更多关于该 API 的知识,所以你说得对,我没有完全理解代码。我希望我最终会做更多的工作。感谢您花时间光临,Reishin。
-
主要问题是
args是一个具有属性的对象,而不是字典。我找到了你需要调用的API代码。 -
是的,我的示例中的 vars(args) 只是一种快速显示 args 内容的方法。不打算在其他地方使用。我
标签: python google-api youtube-api argparse