python怎么设置参数_Python参数设置
1. 配置⽂件(ConfigParser模块)
1.1 ConfigParser简介
ConfigParser 是⽤来读取配置⽂件的包。配置⽂件的格式如下:中括号“[ ]”内包含的为section。section 下⾯为类似于key-value的options内容。例如
[db]
db_host = 127.0.0.1
db_port = 22
db_user = root
db_pass = rootroot
[concurrent]
thread = 10
processor = 20
1.2 ConfigParser 初始⼯作
使⽤ConfigParser ⾸选需要初始化实例,并读取配置⽂件:
import ConfigParser
cf = ConfigParser.ConfigParser()
1.3 ConfigParser函数白鹿原小说结局
1.3.1. 获取所有sections
>>> s = cf.sections()
>>> print s
['db', 'concurrent']
1.3.2 获得指定section的options
>>> cf.options('db')
['db_host', 'db_port', 'db_user', 'db_pass']
1.3.3 获得指定sections的配置信息
>>> cf.items('db')
[('db_host', '127.0.0.1'), ('db_port', '22'), ('db_user', 'root'), ('db_pass', 'rootroot')]
1.3.4 获得指定sections的option的信息
>>> cf.get("db", "db_host")
'127.0.0.1'
>>> cf.getint("db", "db_port")
22
同样有getfloat、getboolean
以下注意:凡是改变⽂件内容的,都要最后写⼊。
1.3.5 设置某个option的值
>>> cf.set("db", "db_host", "127.1.1.1")
>>> cf.write(open("config.ini", 'w'))
写⼊后的⽂件内容为
[db]
db_host = 127.1.1.1
db_port = 22
db_user = root
db_pass = rootroot
[concurrent]
thread = 10
processor = 20
1.3.6 添加⼀个section
>>> cf.add_section("jihite")
>>> cf.set("jihite", "int", "15")
>>> cf.set("jihite", "bool", "True")
>>> cf.set("jihite", "float", "3.14")
>>> cf.write(open("config.ini", 'w'))
写⼊后的⽂件内容为
[db]
db_host = 127.1.1.1
db_port = 22
db_user = root
db_pass = rootroot
[concurrent]
thread = 10
processor = 20
[jihite]
int = 15
bool = True
float = 3.14
1.3.7 移除⼀个section
>>> cf.remove_option("jihite", "int")
True
>>> cf.write(open("config.ini", 'w'))
改变后的⽂件为
[db]
db_host = 127.1.1.1
db_port = 22
手机发邮件db_user = root
db_pass = rootroot
[concurrent]
thread = 10
processor = 20
[jihite]
bool = True
float = 3.14
1.3.8 移除⼀个option
>>> cf.remove_section("jihite")
True
>>> cf.write(open("config.ini", 'w'))
改变后的⽂件为
[db]
db_host = 127.1.1.1
db_port = 22羽绒服哪些品牌好
db_user = root
db_pass = rootroot
[concurrent]
thread = 10
processor = 20
1.4 ConfigParser举例
⽅式⼆:解析参数(argparse模块)
2. 解析参数(argparse模块)
2.1 argparse简介
argparse是python的命令⾏解析⼯具,它是Python标准库中推荐使⽤的编写命令⾏程序的⼯具。
2.2 argparser 初始⼯作
import argparse
parser = argparse.ArgumentParser()
买车首付多少类ArgumentParser定义为:
class 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)
参数的含义为
2.2.1 prog:程序的名字,默认为sys.argv[0]
>>> parser = argparse.ArgumentParser(prog="myprogram")
>>> parser.print_help()
usage: myprogram [-h]
optional arguments:
-h, --help show this help message and exit
2.2.2 usage: 描述程序⽤途的字符串我的世界药水合成表
例⼦
del.py
importargparseif __name__=="__main__":
parser=argparse.ArgumentParser()
parser.add_argument('content', help='content')
parser.add_argument('title', help='title')
parser.add_argument('tolist', help='tolist')
args=parser.parse_args()
content, title, t, args.title, list
mail=SMTP_SSL()
mail.sendMail(content, title, tolist)
执⾏
python2.7 del.py
usage: del.py [-h] content title tolist
del.py: error: too few arguments
2.2.3
更多参数
parser.add_argument("-v", "--verbose", help="increase output verbosity", action="store_true")
parser.add_argument('numbers', type=int, help="numbers to calculate", nargs='+')
parser.add_argument('-s', '--sum', help="sum all numbers", action='store_true', default=True)
parser.add_argument('-f', '--format', choices=['int', 'float'], help='choose result format', default='idsf')
2.3 添加参数选项
3. 综合实例
要求
1. 配置⽂件和输⼊参数两种⽅式,其中输⼊参数优先级⾼
2. 参数包括配置⽂件、下载⽬录、进程个数、⽇志⽂件、⽇志类型
3. 如果进程个数不知,那么默认为机器cpu核数
4. 如果参数类型不知,那么默认为3
defsetArgs():
parse= argparse.ArgumentParser(prog = "down_v9.py", usage="%(prog)s [options]")
parse.add_argument("-c", "-config", type=str, help="配置⽂件")
parse.add_argument("-o", "-outdir", type=str, help="下载⽬录")
parse.add_argument("-p", "-processnum", type=int, help="进程个数")
parse.add_argument("-l", "-logfile", type=str, help="⽇志⽂件")
parse.add_argument("-lt", "-logtype", type=str, help="输出⽇志类型:1,只显⽰到屏幕;2,只输出到⽇志⽂件;3,即显⽰到屏幕⼜输出到⽇志⽂件")
args=parse.parse_args()returnargs
defget_args():
becauseofyou中文歌词args=setArgs()
config=args.c
outdir=args.o
process_num=args.p
logfile=args.l
logtype=args.ltif config andos.path.isfile(config):
cf=ConfigParser.ConfigParser()
s=cf.sections()if "con" ins:
cons= cf.options("con")if outdir == None and "outdir" incons:
outdir= cf.get("con", "outdir")if process_num == None and "process_num" incons:
process_num= cf.getint("con", "process_num")if logfile == None and "logfile" incons:
logfile= cf.get("con", "logfile")if logtype == None and "logtype" incons:
logtype= cf.get("con", "logtype")if process_num ==None:
process_num=multiprocessing.cpu_count()if logtype ==None:
logtype= 3
return [outdir, process_num, logfile, logtype]
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论