argparse

Getting Youtube data using Python

旧时模样 提交于 2019-12-10 15:16:12
问题 I'm trying to learn how to analyze social media data available on the web and I'm starting with Youtube. from apiclient.errors import HttpError from outh2client.tools import argparser from apiclient.discovery import build import pandas as pd DEVELOPER_KEY = "AIzaSyB_F1mCrDydEbGUosnZES-NW-mg1CaOyjI" YOUTUBE_API_SERVICE_NAME = "youtube" YOUTUBE_API_VERSION = "v3" argparser.add_argument("--q", help="Search term", default="apple product") argparser.add_argument("--max-results", help="Max results"

Python argparse AssertionError when number of arguments exceeds threshold

笑着哭i 提交于 2019-12-10 14:24:01
问题 I'm trying to write a Python script with a lot of arguments that I'd like to break up into clear groups using the argparse module as follows: import argparse parser = argparse.ArgumentParser(description='Placeholder text', add_help=False) req = parser.add_argument_group('required arguments') req.add_argument('-m','--mode', action='store', dest='mode', help='Operation mode', choices=['single', 'multi'], required=True, metavar='') req.add_argument('-s','--snps', action='store', dest='snps',

Python argparse choices from an infinite set

别来无恙 提交于 2019-12-10 14:20:06
问题 I have the following code to create a container which pretends to behave like the set of all prime numbers (actually hides a memoised brute-force prime test) import math def is_prime(n): if n == 2 or n == 3: return True if n == 1 or n % 2 == 0: return False else: return all(n % i for i in xrange(3, int(1 + math.sqrt(n)), 2)) class Primes(object): def __init__(self): self.memo = {} def __contains__(self, n): if n not in self.memo: self.memo[n] = is_prime(n) return self.memo[n] That seems to be

Argparse argument generated help, 'metavar' with choices

烂漫一生 提交于 2019-12-10 13:29:38
问题 When using an argument (optional and positional both have this problem ) with the keyword choices , the generated help output shows those choices. If that same argument also includes a metavar keyword, the list of choices is omitted from the generated output. What I had in mind, was to show the metavar in the usage line, but actually show the available choices when the 'autohelp' lists positional/optional argument details. Any simple fixes/workarounds? I have already started an argparse

argparse命令行传参

牧云@^-^@ 提交于 2019-12-10 12:33:27
import argparse parser = argparse.ArgumentParser(description='manual to this script') # 创建解析器,及其描述 parser.add_argument('-env', type=str, default='dev') # 添加参数,env为参数的名称,type为参数类型,default为参数默认值 parser.add_argument('-host', type=str, default='0.0.0.0') parser.add_argument('-port', type=str, default='8000') args = parser.parse_args() print(args.env) # 获取参数 来源: https://www.cnblogs.com/pyweb/p/12015898.html

Calling functions with argparse

南笙酒味 提交于 2019-12-10 10:54:44
问题 Hey guys, I'm having issues calling functions from argpars. This is a simplified version of my script and this works, printing whatever value I give -s or -p import argparse def main(): parser = argparse.ArgumentParser(description="Do you wish to scan for live hosts or conduct a port scan?") parser.add_argument("-s", dest='ip3octets', action='store', help='Enter the first three octets of the class C network to scan for live hosts') parser.add_argument("-p", dest='ip', action='store',help=

Can Python's argparse permute argument order like gnu getopt?

不打扰是莪最后的温柔 提交于 2019-12-10 03:28:58
问题 GNU getopt, and command line tools that use it, allow options and arguments to be interleaved, known as permuting options (see http://www.gnu.org/software/libc/manual/html_node/Using-Getopt.html#Using-Getopt). Perl's Getopt::Long module also supports this (with qw(:config gnu_getopt)). argparse seems to not support (or even mention) permuting options. There are many SO questions related to arg/opt order, but none seem answer this question: Can argparse be made to permute argument order like

Python argparse: Combine optional parameters with nargs=argparse.REMAINDER

你离开我真会死。 提交于 2019-12-10 03:09:14
问题 I must be missing something obvious. The goal is to use argparse with the first parameter required, a second optional and any other remaining parameters optional. To show the issue I made two test parsers; the only difference between them is using nargs=argparse.REMAINDER in one and nargs='*' in the other. def doParser1(argsin): parser = argparse.ArgumentParser(description='Parser demo.') parser.add_argument('req1', help='first required parameter') parser.add_argument('--opt1', help='first

Python argparse: How to insert newline the help text in subparser?

断了今生、忘了曾经 提交于 2019-12-10 02:47:05
问题 This question is related to a question asked earlier, but might be unrelated. Question is: How to use newlines in the help text in the given (working) example below, when using subparsers? import argparse parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter) subparsers = parser.add_subparsers() parser_start = subparsers.add_parser('stop') parser_start.add_argument("file", help = "firstline\nnext line\nlast line") print parser.parse_args() My output is as follows:

Set the default to false if another mutually exclusive argument is true

元气小坏坏 提交于 2019-12-10 02:34:00
问题 I realise this is a lot like Setting default option in Python of two mutually exclusive options using the argparse module although from a different perspective (and the answers given there don't seem to help). Code block (parser is an instance of argparse.ArgumentParser): mutex_group = parser.add_mutually_exclusive_group() mutex_group.add_argument("--show", action="store_true", dest="show", default=True) mutex_group.add_argument("--insert", action="store_true", dest="insert") opts = parser