python argparse – 传递值WITHOUT命令行

前端之家收集整理的这篇文章主要介绍了python argparse – 传递值WITHOUT命令行前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想我不理解 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,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">

解决方法

无论它是否是最好的方法,你真的要弄清楚.但是使用没有命令行的argparse很容易.我一直这样做,因为我有可以从命令行运行的批处理.或者也可以通过其他代码调用 – 这对于单元测试非常有用,如上所述.例如,argparse特别擅长默认参数.

从您的样本开始.

import argparse

argparser = argparse.ArgumentParser()
argparser.add_argument("--file",help="Video file to upload")
argparser.add_argument("--title",default="Test Title")
argparser.add_argument("--description",default="Test Description")
argparser.add_argument("--category",help="Numeric video category. " +
        "See https://developers.google.com/youtube/v3/docs/videoCategories/list")
argparser.add_argument("--keywords",default="")
VALID_PRIVACY_STATUSES = ("private","public")
argparser.add_argument("--privacyStatus",help="Video privacy status.")

#pass in any positional or required variables.. as strings in a list
#which corresponds to sys.argv[1:].  Not a string => arcane errors.
args = argparser.parse_args(["--file","myfile.avi"])

#you can populate other optional parameters,not just positionals/required
#args = argparser.parse_args(["--file","myfile.avi","--title","my title"])


print vars(args)

#modify them as you see fit,but no more validation is taking place
#so best to use parse_args.
args.privacyStatus = "some status not in choices - already parsed"
args.category = 42

print vars(args)

#proceed as before,the system doesn't care if it came from the command line or not
# youtube = get_authenticated_service(args)

输出

{'category': '22','description': 'Test Description','title': 'Test Title','privacyStatus': 'private','file': 'myfile.avi','keywords': ''}
{'category': 42,'privacyStatus': 'some status not in choices - already parsed','keywords': ''}
原文链接:https://www.f2er.com/python/186271.html

猜你在找的Python相关文章