我有兴趣使用argparse的ArgumentDefaultsHelpFormatter类格式化程序(我的程序有几个子命令).默认情况下,输入和输出参数分别设置为sys.stdin和sys.stdout.但是,这两个参数的格式对用户来说可能有点混乱(例如(默认:',模式'r'在0x10028e0c0>).有没有办法专门轻松地改变这两个参数的输出格式来获取像'default:STDIN'或'default:STDOUT'之类的东西?
谢谢
import sys
import argparse
parser = argparse.ArgumentParser(prog='PROG',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--infile',
'-i',
metavar='File',
help='The input file/stream.',
default=sys.stdin,
type=argparse.FileType('r'),
required=False)
parser.add_argument('--outfile',
'-o',
metavar='File',
help='The output file/stream.',
default=sys.stdout,
type=argparse.FileType('r'),
required=False)
parser.add_argument('--whatever-arg',
'-w',
type=str,
default='any',
help='Change something',
required=False)
args = parser.parse_args()
parser.print_help()
Run Code Online (Sandbox Code Playgroud)
这使:
usage: PROG [-h] [--infile File] [--outfile File]
[--whatever-arg WHATEVER_ARG]
optional arguments:
-h, --help show this help message and exit
--infile File, -i File
The input file/stream. (default: <open file '<stdin>',
mode 'r' at 0x10028e0c0>)
--outfile File, -o File
The output …Run Code Online (Sandbox Code Playgroud) 该argparse命令行参数打交道时,包做了伟大的工作。但是,我想知道是否有任何方法可以要求argparse检查文件扩展名(例如“ .txt”)。想法是派生一个与argparse.FileType相关的类。我会对任何建议感兴趣。
请记住,我的程序中有50多个子命令,所有子命令都有自己的CLI。因此,我希望派生一个可以在每个类中导入的类,而不是在所有命令中添加一些严格的测试。
非常感谢。
# As an example one would be interested in turning this...
parser_grp.add_argument('-o', '--outputfile',
help="Output file.",
default=sys.stdout,
metavar="TXT",
type=argparse.FileType('w'))
# Into that...
from somewhere import FileTypeWithExtensionCheck
parser_grp.add_argument('-o', '--outputfile',
help="Output file.",
default=sys.stdout,
metavar="TXT",
type=FileTypeWithExtensionCheck('w', '.[Tt][Xx][Tt]$'))
Run Code Online (Sandbox Code Playgroud) 为什么pylint在函数外部时接受大写变量,而在函数内部拒绝它们?相反,为什么pylint拒绝camelCase ouside函数并在函数内部接受它?
我刚刚安装了pylint(版本2.2.2)来检查我的Python3。一定错过了某些东西。我相关的Python /软件包版本是:
pylint 2.2.2
astroid 2.1.0
Python 3.6.7 | packaged by conda-forge | (default, Nov 20 2018, 18:20:05)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)]
Run Code Online (Sandbox Code Playgroud)
考虑下面的代码(test_1),其中我使用camelCase和大写的变量命名。大写的变量被接受(为什么?)而骆驼的情况被拒绝(我想是因为代码没有包装到函数中)。
'''
Nothing important
'''
fileHandler = open("afile.txt")
for line in fileHandler:
Token = line.split("\t")
Part_1 = Token[0]
print(Part_1)
Run Code Online (Sandbox Code Playgroud)
哪个调用pylint:
$ pylint --py3k --enable=all test_1.py
************* Module test_1
test_1.py:5:0: C0103: Constant name "fileHandler" doesn't conform to UPPER_CASE naming style (invalid-name)
------------------------------------------------------------------
Your code has been rated at 8.00/10 (previous run: 8.00/10, +0.00)
Run Code Online (Sandbox Code Playgroud)
现在,如果我将所有内容都放入一个函数中(test_2)。 …
我正在开发一个包含多个 python 脚本的工具箱。对于其中的一些参数,某些参数可能是数值。根据脚本的不同,有些可能要求值 v 介于 -1 和 1 之间,或者 0 和 1 之间,或者 1 和 10 之间,或者...一个例子是输出图表中的页面宽度,它应该始终为正值。
我可以随时检查 v 是否在要求的范围内。我还可以使用 argparse 为每个范围定义一个操作或类型。给出了使用新类型的示例:
def positive_num(a_value):
"""Check a numeric positive."""
if not a_value > 0:
raise argparse.ArgumentTypeError("Should be positive.")
return a_value
Run Code Online (Sandbox Code Playgroud)
稍后将其添加到解析器中:
parser_grp.add_argument('-pw', '--page-width',
help='Output pdf file width (e.g. 7 inches).',
type=positive_num,
default=None,
required=False)
Run Code Online (Sandbox Code Playgroud)
现在,如果该值是相关系数(或范围内的任何值),则可以使用操作或类型来编写更通用的内容:
def ranged_num(a_value, lowest=-1, highest=1):
"""Check a numeric is in expected range."""
if not (a_value >= lowest and a_value <= highest):
raise argparse.ArgumentTypeError("Not in range.")
return a_value
Run Code Online (Sandbox Code Playgroud)
稍后可以添加如下内容: …