module2
1.argparse模块
一个可执行文件或者脚本都可以接收参数。
$ ls -l /etc
/etc 是位置参数
-l 是短选项
1.参数分类
参数分为:
- 位置参数,参数放在那里,就要对应一个参数位置。例如/etc就是对应一个参数位置。
- 选项参数,必须通过前面是 - 的短选项或者 -- 的长选项,然后后面的才算该选项的参数,当然选项后面也可 以没有参数。 上例中,/etc对应的是位置参数,-l是选项参数。``
ls -alh src
2.基本解析
import argparse
parser = argparse.ArgumentParser() # 获得一个参数解析器
args = parser.parse_args() # 分析参数
parser.print_help() # 打印帮助
运行结果:
$ python test.py -h
usage: test1.py [-h]
optional arguments:
-h, --help show this help message and exit
argparse不仅仅做了参数的定义和解析,还自动帮助生成了帮助信息。尤其是usage,可以看到现在定义的参数是 否是自己想要的。
3.解析器的参数
参数名称 | 说明 |
---|---|
prog | 程序的名字,缺省使用 sys.argv[0] 的 basename |
add_help | 自动为解析器增加 -h 和 --help 选项,默认为True |
description | 为程序功能添加描述 |
parser = argparse.ArgumentParser(prog='ls', add_help=True, description='list directory contents')
$ python test.py --help
usage: ls [-h]
list directory contents
optional arguments:
-h, --help show this help message and exit
4.位置参数解析
import argparse
# 获得一个参数解析器
parser = argparse.ArgumentParser(prog='ls', add_help=True, description='list directory
contents')
parser.add_argument('path')
args = parser.parse_args() # 分析参数
parser.print_help() # 打印帮助
# 运行结果,出现了错误,提示需要输入path对应的位置参数
usage: ls [-h] path
ls: error: the following arguments are required: path
程序定义为: ls [-h] path -h为帮助选项,可有可无 path为位置参数,必须提供
5.传参
import argparse
# 获得一个参数解析器
parser = argparse.ArgumentParser(prog='ls', add_help=True, description='list directory
contents')
parser.add_argument('path') # 位置参数
args = parser.parse_args(('/etc',)) # 分析参数,同时传入可迭代的参数
print(args, args.path) # 打印名词空间中收集的参数
parser.print_help() # 打印帮助
运行结果:
Namespace(path='/etc') /etc
usage: ls [-h] path
list directory contents
positional arguments:
path
optional arguments:
-h, --help show this help message and exit
6.非必须位置参数
上面的代码必须输入位置参数,否则会报错。
usage: ls [-h] path
ls: error: the following arguments are required: path
有时候,ls命令不输入任何路径的话就表示列出当前目录的文件列表。
import argparse
# 获得一个参数解析器
parser = argparse.ArgumentParser(prog='ls', add_help=True, description='list directory
contents')
parser.add_argument('path', nargs='?', default='.', help="path help") # 位置参数,可有可无,缺省
值,帮助
args = parser.parse_args() # 分析参数,同时传入可迭代的参数
print(args) # 打印名词空间中收集的参数
parser.print_help() # 打印帮助
# 运行结果
Namespace(path='.')
usage: ls [-h] [path]
list directory contents
positional arguments:
path path help
optional arguments:
-h, --help show this help message and exit
可以看出path也变成可选的位置参数,没有提供就使用默认值 .点号 表示当前路径。
- help 表示帮助文档中这个参数的描述
- nargs 表示这个参数接收结果参数
- ? 表示可有可无
- 表示至少一个
- 可以任意个
- 数字表示必须是指定数目个
- default 表示如果不提供该参数,就使用这个值。一般和?、*配合,因为它们都可以不提供位置参数,不提 供就是用缺省值
7.选项参数
parser.add_argument('-l')
就增加了选项参数,参数定义为 ls [-h][-l L] [path]
和我们要的形式有一点出入,我们期望的是[-h],怎么解决?nargs能够解决吗?
parser.add_argument('-l', nargs='?')
ls [-h][-l [L]] [path]
L变成了可选,而不是-l。那么,直接把nargs=0,意思就是让这个选项接收0个参数,如下
parser.add_argument('-l', nargs=0)
结果,抛出异常
raise ValueError('nargs for store actions must be > 0; if you ' ValueError: nargs for store actions must be > 0; if you have nothing to store, actions such as store true or store const may be more appropriate
看来nargs是控制位置参数和选项参数的。
为了这个问题,使用action参数
parser.add_argument('-l', action='store_true')
看到命令定义变成了 ls [-h] [-l] [path]
提供-l选项,例如
ls -l得到Namespace(l=True, path='.'),提供-l值是True
ls 得到Namespace(l=False, path='.'),提供-l值是False
这样同True、False来判断用户是否提供了该选项
parser.add_argument('-l', action='store_const', const = 20)
表示,如果提供-l选项,则对应的值是20,如 果不提供,对应值是None
2.queue模块
queue提供了一个先进先出的队列Queue
queue.Queue(maxsize=0)
创建队列,返回Queue对象
Queue.get(block=True, timeout=None)
从队列中移除并返回这个元素,如果block为True,是阻塞,timeout为None就是一直阻塞。如果block为True但是timeout有值,阻塞到一定的秒数就抛出Empty异常;block为False,是非阻塞,timeout将被忽略,要么成功返回一个元素,要么抛出Empty异常。
Queue.get_nowait()
等价于get(False),要么成功返回一个元素,要么抛出Empty异常
Queue.put(item, block=True, timeout=None)
把一个元素加入到队列里面去,block=True,timeout=None,一致阻塞直至有空位放元素,block=True,timeout=5,阻塞5秒就抛出Full异常,block=False,timeout失效,立即返回,能塞进去就塞不能就抛出Full异常。
queue.put_nowait(item)
等价于put(item, False),也就是能塞进去就塞不能就抛出Full异常。
from queue import Queue
import random
q = Queue()
q.put(random.randint(1, 10))
q.put(random.randint(1, 10))
print(q.get())
print(q.get())
# print(q.get()) # 阻塞
# print(q.get(timeout=3)) # 阻塞,超时抛异常
print(q.get_nowait()) # 不阻塞,,没数据立即抛异常