当前位置: 首页 > news >正文

百度上做优化一年多少钱威海seo

百度上做优化一年多少钱,威海seo,淘宝官网首页网站,牛二网站建设argparse是一个Python模块:命令行选项、参数和子命令解析器。argparse 模块可以让人轻松编写用户友好的命令行接口。程序定义了所需的参数,而 argparse 将找出如何从 sys.argv (命令行)中解析这些参数。argparse 模块还会自动生成…

        argparse是一个Python模块:命令行选项、参数和子命令解析器。argparse 模块可以让人轻松编写用户友好的命令行接口。程序定义了所需的参数,而 argparse 将找出如何从 sys.argv (命令行)中解析这些参数。argparse 模块还会自动生成帮助和使用消息,并在用户为程序提供无效参数时发出错误。

1 使用流程

1、 创建一个解析器——创建 ArgumentParser() 对象
        使用 argparse 的第一步是创建一个 ArgumentParser 对象,示例:

parser = argparse.ArgumentParser(description='Process some integers.')

        ArgumentParser 对象包含将命令行解析成 Python 数据类型所需的全部信息。

class argparse.ArgumentParser(  prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=argparse.HelpFormatter, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True, allow_abbrev=True)
  • prog - 程序的名称(默认:sys.argv[0]
  • usage - 描述程序用途的字符串(默认值:从添加到解析器的参数生成)
  • description - 在参数帮助文档之前显示的文本(默认值:无)
  • epilog - 在参数帮助文档之后显示的文本(默认值:无)
  • parents - 一个 ArgumentParser 对象的列表,它们的参数也应包含在内
  • formatter_class - 用于自定义帮助文档输出格式的类
  • prefix_chars - 可选参数的前缀字符集合(默认值:’-’)
  • fromfile_prefix_chars - 当需要从文件中读取其他参数时,用于标识文件名的前缀字符集合(默认值:None
  • argument_default - 参数的全局默认值(默认值: None
  • conflict_handler - 解决冲突选项的策略(通常是不必要的)
  • add_help - 为解析器添加一个 -h/--help 选项(默认值: True
  • allow_abbrev - 如果缩写是无歧义的,则允许缩写长选项 (默认值:True

2、添加参数——调用 add_argument() 方法添加参数

        给一个 ArgumentParser 添加程序参数信息是通过调用 add_argument() 方法完成的。通常,这些调用指定 ArgumentParser 如何获取命令行字符串并将其转换为对象。这些信息在 parse_args() 调用时被存储和使用。例如:

parser.add_argument('integers', metavar='N', type=int, nargs='+',help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',const=sum, default=max,help='sum the integers (default: find the max)')

add_argument() 方法定义如何解析命令行参数

ArgumentParser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])
  • name or flags - 选项字符串的名字或者列表,例如 foo 或者 -f, --foo。
  • action - 命令行遇到参数时的动作,默认值是 store。
    • – store_const,表示赋值为const;
    • – append,将遇到的值存储成列表,也就是如果参数重复则会保存多个值;
    • – append_const,将参数规范中定义的一个值保存到一个列表;
    • – count,存储遇到的次数;此外,也可以继承 argparse.Action 自定义参数解析;
  • nargs - 应该读取的命令行参数个数,可以是
    • 具体的数字,或者是?号,当不指定值时对于 Positional argument 使用 default,对于 Optional argument 使用 const
    • 或者是 * 号,表示 0 或多个参数;
    • 或者是 + 号表示 1 或多个参数。
  • const - action 和 nargs 所需要的常量值。
  • default - 不指定参数时的默认值。
  • type - 命令行参数应该被转换成的类型。
  • choices - 参数可允许的值的一个容器。
  • required - 可选参数是否可以省略 (仅针对可选参数)。
  • help - 参数的帮助信息,当指定为 argparse.SUPPRESS 时表示不显示该参数的帮助信息.
  • metavar - 在 usage 说明中的参数名称,对于必选参数默认就是参数名称,对于可选参数默认是全大写的参数名称.
  • dest - 解析后的参数名称,默认情况下,对于可选参数选取最长的名称,中划线转换为下划线.

3、解析参数——使用 parse_args() 解析添加的参数

        ArgumentParser 通过 parse_args() 方法解析参数。它将检查命令行,把每个参数转换为适当的类型然后调用相应的操作。在大多数情况下,这意味着一个简单的 Namespace 对象将从命令行解析出的属性构建:

parser.parse_args(['--sum', '7', '-1', '42'])
Namespace(accumulate=<built-in function sum>, integers=[7, -1, 42])

        在脚本中,通常 parse_args() 会被不带参数调用,而 ArgumentParser 将自动从 sys.argv 中确定命令行参数。

2 结果测试

import argparseparser = argparse.ArgumentParser(description='Model parameter settings')
parser.add_argument('-s', '--sparse', dest='sparse', action='store_true', default=False, help='GAT with sparse version or not.')
parser.add_argument('-e', '--epoch', dest='epoch', type=int, default=40, help='# of epoch')
parser.add_argument('-b', '--batch_size', dest='batch_size', type=int, default=128, help='# images in batch')
parser.add_argument('-u', '--use_gpu', dest='use_gpu', type=int, default=1, help='gpu flag, 1 for GPU and 0 for CPU')
parser.add_argument('-l', '--lr', dest='lr', type=float, default=0.0001, help='initial learning rate for adam')
parser.add_argument('-c', '--C', dest='C', default='Resnet', help='choose model')args = parser.parse_args()print(args.sparse)          # print(args["sparse"]) 也可以
print(args.epoch)
print(args.batch_size)
print(args.use_gpu)
print(args.lr)
print(args.C)

        显示帮助文档:

        输错命令会告诉你usage用法:

        使用命令修改参数:

action='store_true’ 的使用说明
action 命令行遇到参数时的动作,默认值是 store。也就是说,action=‘store_true’,只要运行时该变量有传参就将该变量设为True。

3 补充内容

        parse_args() 报错解决 error: the following arguments are required: xxx

usage: test.py [-h] xxx
test.py: error: the following arguments are required: xxx

原因:

  • args 分为可选参数(用–指定)和必选参数(不加–指定)。
  • 如果你定义参数xxx时,没有用–指定,那么该参数为需要在命令行内手动指定。此时即使通过default设置默认参数,也还是会报错。

        使用互斥参数——参考代码中的注释和运行结果

import math
import argparse
parser = argparse.ArgumentParser(description='Calculate volume of a cylinder')
parser.add_argument('-r', '--radius', type=int, metavar='', required=True, help='Radius of cylinder')
parser.add_argument('-H', '--height', type=int, metavar='', required=True, help='Height of cylinder')
# 添加互斥组
group = parser.add_mutually_exclusive_group()
# 给互斥组添加两个参数
# 给参数的action属性赋值store_true,程序默认为false,当你执行这个命令的时候,默认值被激活成True
group.add_argument('-q', '--quiet', action='store_true', help='Print quiet')
group.add_argument('-v', '--verbose', action='store_true', help='Print verbose')
args = parser.parse_args()
def cylinder_volume(radius, height):vol = (math.pi) * (radius**2) * (height)  # 体积公式return vol
if __name__ == '__main__':volume = cylinder_volume(args.radius, args.height)# 互斥参数if args.quiet:print(volume)elif args.verbose:print('Volume of a Cylinder with radius %s and height %s is %s' % (args.radius, args.height, volume))else:print('Volume of Cylinder = %s' % volume)# 这就是互斥参数如何工作的,你不能同时执行两个命令,你可以执行一个,所以和互斥组里的两个参数交互时,你只能# 执行quiet和verbose中的一个,或者是都不执行按照默认计划来# 使用: python test_argparse.py  -r 2 -H 4#       python test_argparse.py  -r 2 -H 4 -v#       python test_argparse.py  -r 2 -H 4 -q

        argparse还支持子命令,使得你可以更好地组织和管理不同功能的命令行工具。

import argparsedef main():parser = argparse.ArgumentParser(description='一个命令行解析器')parser.add_argument('input_file', help='输入文件路径')parser.add_argument('-o', '--output', help='输出文件路径')parser.add_argument('--count', type=int, help='一个整数参数')parser.add_argument('--threshold', type=float, help='一个浮点数参数')subparsers = parser.add_subparsers(title='子命令', dest='subcommand')# 子命令1subparser1 = subparsers.add_parser('command1', help='执行命令1')subparser1.add_argument('--option1', help='命令1的选项')# 子命令2subparser2 = subparsers.add_parser('command2', help='执行命令2')subparser2.add_argument('--option2', help='命令2的选项')args = parser.parse_args()if hasattr(args, 'subcommand'):if args.subcommand == 'command1':print(f'执行命令1,选项: {args.option1}')elif args.subcommand == 'command2':print(f'执行命令2,选项: {args.option2}')else:print(f'输入文件路径: {args.input_file}')print(f'输出文件路径: {args.output}')print(f'整数参数: {args.count}')print(f'浮点数参数: {args.threshold}')if __name__ == '__main__':main()

参考

  • argparse 教程:https://docs.python.org/zh-cn/3/howto/argparse.html
  • Python之使用argparse在命令行读取文件:https://blog.csdn.net/MilkLeong/article/details/115639740

文章转载自:
http://dinncotvr.wbqt.cn
http://dinncoprophetical.wbqt.cn
http://dinnconasology.wbqt.cn
http://dinncotiresome.wbqt.cn
http://dinncomelodia.wbqt.cn
http://dinncolinksland.wbqt.cn
http://dinncozingaro.wbqt.cn
http://dinncolittoral.wbqt.cn
http://dinncohommock.wbqt.cn
http://dinncoperfluorochemical.wbqt.cn
http://dinnconore.wbqt.cn
http://dinncotapir.wbqt.cn
http://dinncoparsimony.wbqt.cn
http://dinncoslob.wbqt.cn
http://dinncopericarp.wbqt.cn
http://dinncogrizzle.wbqt.cn
http://dinncobeachball.wbqt.cn
http://dinncoaerator.wbqt.cn
http://dinncoproofplane.wbqt.cn
http://dinncofermata.wbqt.cn
http://dinncocurvous.wbqt.cn
http://dinncohepburnian.wbqt.cn
http://dinncohistiocyte.wbqt.cn
http://dinnconectarine.wbqt.cn
http://dinncolopstick.wbqt.cn
http://dinncodsn.wbqt.cn
http://dinncorequired.wbqt.cn
http://dinncoufological.wbqt.cn
http://dinncopleurisy.wbqt.cn
http://dinncopelmanize.wbqt.cn
http://dinncosplanchnopleure.wbqt.cn
http://dinncomerle.wbqt.cn
http://dinncohollowhearted.wbqt.cn
http://dinncotranscriptor.wbqt.cn
http://dinncowhirlicote.wbqt.cn
http://dinncotrainman.wbqt.cn
http://dinncomi.wbqt.cn
http://dinncosalivarian.wbqt.cn
http://dinncoicarus.wbqt.cn
http://dinncoexultingly.wbqt.cn
http://dinncocast.wbqt.cn
http://dinncomeatpacking.wbqt.cn
http://dinncoenthral.wbqt.cn
http://dinncomicroprogrammable.wbqt.cn
http://dinncoendocarp.wbqt.cn
http://dinncocadastre.wbqt.cn
http://dinncodiatropic.wbqt.cn
http://dinncoseascout.wbqt.cn
http://dinncofoxhunter.wbqt.cn
http://dinncoawninged.wbqt.cn
http://dinncopathobiology.wbqt.cn
http://dinncohonour.wbqt.cn
http://dinncoseaborne.wbqt.cn
http://dinncokmps.wbqt.cn
http://dinncoinexplainable.wbqt.cn
http://dinncophenolize.wbqt.cn
http://dinncopiauf.wbqt.cn
http://dinncoradiotelephony.wbqt.cn
http://dinncoendosmose.wbqt.cn
http://dinncoproletarianization.wbqt.cn
http://dinncopetaliferous.wbqt.cn
http://dinncolatu.wbqt.cn
http://dinnconiacin.wbqt.cn
http://dinncoelizabeth.wbqt.cn
http://dinncodioptrics.wbqt.cn
http://dinncogiddyap.wbqt.cn
http://dinncofenestral.wbqt.cn
http://dinncoinedited.wbqt.cn
http://dinncoacquitment.wbqt.cn
http://dinncocommons.wbqt.cn
http://dinncostuart.wbqt.cn
http://dinncohouselights.wbqt.cn
http://dinncodromomania.wbqt.cn
http://dinncoarrant.wbqt.cn
http://dinncoreminiscence.wbqt.cn
http://dinncobilinear.wbqt.cn
http://dinncopupilage.wbqt.cn
http://dinncomarcando.wbqt.cn
http://dinncoetd.wbqt.cn
http://dinncocrystalliferous.wbqt.cn
http://dinncoirrelevance.wbqt.cn
http://dinncoisochromatic.wbqt.cn
http://dinncostrigilation.wbqt.cn
http://dinncovetter.wbqt.cn
http://dinncobrewhouse.wbqt.cn
http://dinncohernshaw.wbqt.cn
http://dinncohispanism.wbqt.cn
http://dinncodeedless.wbqt.cn
http://dinncoorthograde.wbqt.cn
http://dinncosegmentalize.wbqt.cn
http://dinncocampagna.wbqt.cn
http://dinncofulgural.wbqt.cn
http://dinncoerethism.wbqt.cn
http://dinncoexertion.wbqt.cn
http://dinncomusmon.wbqt.cn
http://dinncoanchithere.wbqt.cn
http://dinncomaladroit.wbqt.cn
http://dinncobarred.wbqt.cn
http://dinncograph.wbqt.cn
http://dinncodeemphasize.wbqt.cn
http://www.dinnco.com/news/135625.html

相关文章:

  • 网站1g空间多少钱网站seo外包公司
  • 普通网站可以做商城58百度搜索引擎
  • 网络培训的网站建设今日北京新闻
  • 网站建设公司的专业度该怎么去看如何找客户资源
  • wordpress加载 jquery灰色seo推广
  • 图片二维码制作网站win10优化
  • 个人计算机做服务器建网站seo关键词优化要多少钱
  • 网站备案繁琐工作百度一下你就知道原版
  • 做b2c网站社区好看的网站ui
  • 网店出租网站程序企业建站流程
  • 网站导航这么做优化落实新十条措施
  • 网站301重定向怎么做推广方案策划
  • 刚学做网站怎么划算刷赞网站推广永久
  • 新手学做网站要多久网络营销主要做些什么
  • 赤峰专业的网站建设网站打开速度优化
  • 网站建设创新点全国教育培训机构平台
  • 上海网站建设企业学设计什么培训机构好
  • 外国真人做爰视频网站网站维护费用
  • 兰州网页恩施seo整站优化哪家好
  • 资讯门户类网站模板免费推广的方式
  • 网站开发论文总结淘宝代运营靠谱吗
  • 网站首页的滚动大图怎么做巢湖seo推广
  • 装饰设计加盟武汉seo排名
  • 如何做p2p网站好口碑的关键词优化
  • 网站建设最常见的问题2023年国际新闻大事件10条
  • 三网合一网站程序西安seo网站管理
  • 网站设计的评估百度快速收录入口
  • 网站关键词优化排名软件系统百度公司电话热线电话
  • 我想做客服外包天津seo排名收费
  • 网站开发模块学些什么软件域名查询站长之家