python argparse - pass values WITHOUT command line

巧了我就是萌 提交于 2019-12-23 08:03:09

问题


I think I'm not understanding something basic about python's argparse.

I am trying to use the Google YouTube API for python script, but I am not understanding how to pass values to the script without using the command line.

For example, here is the example for the API. The examples on github and elsewhere show this example as being called from the command line, from where the argparse values are passed when the script is called.

I don't want to use the command line. I am building an app that uses a decorator to obtain login credentials for the user, and when that user wants to upload to their YouTube account, they submit a form which will then call this script and have the argparse values passed to it.

How do I pass values to argparser (see below for portion of code in YouTube upload API script) from another python script?

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)

EDIT: Per request, here is the traceback for the 400 Error I am getting using either the standard method to initialize a dictionary or using argparse to create a dictionary. I thought I was getting this due to badly formed parameters, but perhaps not:

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">

回答1:


Whether it is the best approach or not is really for you to figure out. But using argparse without command line is easy. I do it all the time because I have batches that can be run from the command line. Or can also be called by other code - which is great for unit testing, as mentioned. argparse is especially good at defaulting parameters for example.

Starting with your sample.

import argparse

argparser = argparse.ArgumentParser()
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="")
VALID_PRIVACY_STATUSES = ("private","public")
argparser.add_argument("--privacyStatus", choices=VALID_PRIVACY_STATUSES,
    default=VALID_PRIVACY_STATUSES[0], 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)    

output:

{'category': '22', 'description': 'Test Description', 'title': 'Test Title', 'privacyStatus': 'private', 'file': 'myfile.avi', 'keywords': ''}
{'category': 42, 'description': 'Test Description', 'title': 'Test Title', 'privacyStatus': 'some status not in choices - already parsed', 'file': 'myfile.avi', 'keywords': ''}



回答2:


Calling parse_args with your own list of strings is a common argparse testing method. If you don't give parse_args this list, it uses sys.argv[1:] - i.e. the strings that the shell gives. sys.argv[0] is the strip name.

args = argparser.parse_args(['--foo','foovalue','barvalue'])

It is also easy to construct an args object.

args = argparse.Namespace(foo='foovalue', bar='barvalue')

In fact, if you print args from a parse_args call it should look something like that. As described in the documentation, a Namespace is a simple object, and the values are artributes. So it is easy to construct your own namespace class. All args needs to be is something that returns the appropriate value when used as:

x = args.foo
b = args.bar

Also as noted in the docs, vars(args) turns this namespace into a dictionary. Some code likes to use dictionary, but evidently these youtub functions want a Namespace (or equivalent).

get_authenticated_service(args)
initialize_upload(youtube, args)

https://docs.python.org/3/library/argparse.html#beyond-sys-argv

https://docs.python.org/3/library/argparse.html#the-namespace-object


https://developers.google.com/youtube/v3/guides/uploading_a_video?hl=id-ID

has get_authenticated_service and initialize_upload code

def initialize_upload(youtube, options):
  tags = None
  if options.keywords:
    tags = options.keywords.split(",")

  body=dict(
    snippet=dict(
      title=options.title,
      description=options.description,
      tags=tags,
      categoryId=options.category
    ),
    status=dict(
      privacyStatus=options.privacyStatus
    )
  )
 ....

The args from the parser is options, which it uses as options.category, options.title, etc. You could substitute any other object which has the same behavior and the necessary attributes.




回答3:


Why are you using argparse if you dont want to use command line? Argparse is module created to parse command line arguments there is no other use of it. You cannot pass value to argparse in other ways.

I think what you want is a web app displaying html form making POST request to some server handler that will pass value from form to api call connect to youtube and execute your python code. Argparse is not required for this at all. You can just take value from form and pass it to your api call.



来源:https://stackoverflow.com/questions/31090479/python-argparse-pass-values-without-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!