我是 python 的新手,我一直在研究如何在使用命令行参数时构建我的简单脚本。
该脚本的目的是自动执行我工作中与图像排序和操作相关的一些日常任务。
我可以指定参数并让它们调用相关函数,但我也想在没有提供参数时设置默认操作。
这是我当前的结构。
parser = argparse.ArgumentParser()
parser.add_argument("-l", "--list", help="Create CSV of images", action="store_true")
parser.add_argument("-d", "--dimensions", help="Copy images with incorrect dimensions to new directory", action="store_true")
parser.add_argument("-i", "--interactive", help="Run script in interactive mode", action="store_true")
args = parser.parse_args()
if args.list:
func1()
if args.interactive:
func2()
if args.dimensions:
func3()
但是当我不提供参数时,什么都不会被调用。
Namespace(dimensions=False, interactive=False, list=False)
如果没有提供参数,我想要的是一些默认行为
if args.list:
func1()
if args.interactive:
func2()
if args.dimensions:
func3()
if no args supplied:
func1()
func2()
func3()
这看起来应该很容易实现,但我迷失了如何检测所有参数是否为假而不循环遍历参数并测试是否全部为假的逻辑。
多个参数一起有效,这就是我没有走 elif 路线的原因。
这是我更新后的代码,考虑了@unutbu 的回答
它似乎并不理想,因为所有内容都包含在 if 语句中,但在短期内我找不到更好的解决方案。我很高兴接受@unutbu 的回答,如有任何其他改进,我们将不胜感激。
lists = analyseImages()
if lists:
statusTable(lists)
createCsvPartial = partial(createCsv, lists['file_list'])
controlInputParital = partial(controlInput, lists)
resizeImagePartial = partial(resizeImage, lists['resized'])
optimiseImagePartial = partial(optimiseImage, lists['size_issues'])
dimensionIssuesPartial = partial(dimensionIssues, lists['dim_issues'])
parser = argparse.ArgumentParser()
parser.add_argument(
"-l", "--list",
dest='funcs', action="append_const", const=createCsvPartial,
help="Create CSV of images",)
parser.add_argument(
"-c", "--convert",
dest='funcs', action="append_const", const=resizeImagePartial,
help="Convert images from 1500 x 2000px to 900 x 1200px ",)
parser.add_argument(
"-o", "--optimise",
dest='funcs', action="append_const", const=optimiseImagePartial,
help="Optimise filesize for 900 x 1200px images",)
parser.add_argument(
"-d", "--dimensions",
dest='funcs', action="append_const", const=dimensionIssuesPartial,
help="Copy images with incorrect dimensions to new directory",)
parser.add_argument(
"-i", "--interactive",
dest='funcs', action="append_const", const=controlInputParital,
help="Run script in interactive mode",)
args = parser.parse_args()
if not args.funcs:
args.funcs = [createCsvPartial, resizeImagePartial, optimiseImagePartial, dimensionIssuesPartial]
for func in args.funcs:
func()
else:
print 'No jpegs found'
最佳答案
您可以将函数append_const args.funcs 属性,然后使用一个 if 语句来提供未设置选项时的默认行为:
if not args.funcs:
args.funcs = [func1, func2, func3]
import argparse
def func1(): pass
def func2(): pass
def func3(): pass
parser = argparse.ArgumentParser()
parser.add_argument(
"-l", "--list",
dest='funcs', action="append_const", const=func1,
help="Create CSV of images", )
parser.add_argument(
"-i", "--interactive",
dest='funcs', action="append_const", const=func2,
help="Run script in interactive mode",)
parser.add_argument(
"-d", "--dimensions",
dest='funcs', action='append_const', const=func3,
help="Copy images with incorrect dimensions to new directory")
args = parser.parse_args()
if not args.funcs:
args.funcs = [func1, func2, func3]
for func in args.funcs:
print(func.func_name)
func()
% test.py
func1
func2
func3
% test.py -d
func3
% test.py -d -i
func3
func2
请注意,与您的原始代码不同,这允许用户控制调用函数的顺序:
% test.py -i -d
func2
func3
这可能是理想的,也可能不是。
响应更新 2:
您的代码可以正常工作。然而,这里有另一种组织方式:
不是将主程序嵌套在 if 子句中,您可以
使用
if not lists:
sys.exit('No jpegs found')
# put main program here, unnested
sys.exit将 No jpegs found 打印到 stderr 并以退出代码 1 终止。
虽然我最初建议使用 functools.partial,但现在想到了另一种——也许更简单——的方法:代替
for func in args.funcs:
func()
我们可以说
for func, args in args.funcs:
func(args)
我们需要做的就是在 args.func 中存储一个元组 (func, args)
而不是单独的函数。
例如:
import argparse
import sys
def parse_args(lists):
funcs = {
'createCsv': (createCsv, lists['file_list']),
'resizeImage': (resizeImage, lists['resized']),
'optimiseImage': (optimiseImage, lists['size_issues']),
'dimensionIssues': (dimensionIssues, lists['dim_issues']),
'controlInput': (controlInput, lists)
}
parser = argparse.ArgumentParser()
parser.add_argument(
"-l", "--list",
dest='funcs', action="append_const", const=funcs['createCsv'],
help="Create CSV of images",)
parser.add_argument(
"-c", "--convert",
dest='funcs', action="append_const", const=funcs['resizeImage'],
help="Convert images from 1500 x 2000px to 900 x 1200px ",)
parser.add_argument(
"-o", "--optimise",
dest='funcs', action="append_const", const=funcs['optimiseImage'],
help="Optimise filesize for 900 x 1200px images",)
parser.add_argument(
"-d", "--dimensions",
dest='funcs', action="append_const", const=funcs['dimensionIssues'],
help="Copy images with incorrect dimensions to new directory",)
parser.add_argument(
"-i", "--interactive",
dest='funcs', action="append_const", const=funcs['controlInput'],
help="Run script in interactive mode",)
args = parser.parse_args()
if not args.funcs:
args.funcs = [funcs[task] for task in
('createCsv', 'resizeImage', 'optimiseImage', 'dimensionIssues')]
return args
if __name__ == '__main__':
lists = analyseImages()
if not lists:
sys.exit('No jpegs found')
args = parse_args(lists)
statusTable(lists)
for func, args in args.funcs:
func(args)
关于没有提供参数时python argparse设置行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14358753/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)