第2单 2.2 准备一项任务
一、普通接收参数
import argparse def main(number, other_number): result = number * other_number print(f'The result is {result}') if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('-n1', type=int, help='A number', default=1) parser.add_argument('-n2', type=int, help='Another number', default=1) args = parser.parse_args() main(args.n1, args.n2)
(.venv) huangsiyangdeiMac:ch02 huangsiyang$ python prepare_task_step1.py
The result is 1
(.venv) huangsiyangdeiMac:ch02 huangsiyang$ python prepare_task_step1.py -n1 3 -n2 4
The result is 12
二、读取配置文件:
import argparse import configparser def main(number, other_number): result = number * other_number print(f'The result is {result}') if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('-n1', type=int, help='A number', default=1) parser.add_argument('-n2', type=int, help='Another numnber', default=1) parser.add_argument('--config', '-c', type=argparse.FileType('r'), help='config file') args = parser.parse_args() if args.config: config = configparser.ConfigParser() config.read_file(args.config) # Transforming values into integers args.n1 = int(config['ARGUMENTS']['n1']) args.n2 = int(config['ARGUMENTS']['n2']) main(args.n1, args.n2)
配置文件的内容config.ini:
[ARGUMENTS]
n1=5
n2=7
执行结果如下:
(.venv) huangsiyangdeiMac:ch02 huangsiyang$ python prepare_task_step2.py -c config.ini
The result is 35
(.venv) huangsiyangdeiMac:ch02 huangsiyang$ python prepare_task_step2.py -c config.ini -n1 2 -n2 3
The result is 35
#配置文件的值会覆盖传入参数的值。
三、将执行结果输出到文件:
import argparse import sys import configparser def main(number, other_number, output): result = number * other_number print(f'The result is {result}', file=output) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('-n1', type=int, help='A number', default=1) parser.add_argument('-n2', type=int, help='Another number', default=1) parser.add_argument('--config', '-c', type=argparse.FileType('r'), help = 'config file') parser.add_argument('-o', dest='output', type=argparse.FileType('w'), help = 'output file', default=sys.stdout) args = parser.parse_args() if args.config: config = configparser.ConfigParser() config.read_file(args.config) # Transforming values into integers args.n1 = int(config['ARGUMENTS']['n1']) args.n2 = int(config['ARGUMENTS']['n2']) main(args.n1, args.n2, args.output)
(.venv) huangsiyangdeiMac:ch02 huangsiyang$ python prepare_task_step5.py -n1 3 -n2 5
The result is 15
(.venv) huangsiyangdeiMac:ch02 huangsiyang$ python prepare_task_step5.py -n1 3 -n2 5 -o result.txt
(.venv) huangsiyangdeiMac:ch02 huangsiyang$ cat result.txt
The result is 15
(.venv) huangsiyangdeiMac:ch02 huangsiyang$ python prepare_task_step5.py -c config.ini -o result2.txt
(.venv) huangsiyangdeiMac:ch02 huangsiyang$ cat result2.txt
The result is 35
#将结果输出到文件
四、通过使用YAML文件作为配置:
首先安装支持YAML的库:
(.venv) huangsiyangdeiMac:ch02 huangsiyang$ echo 'PyYAML==3.12' > requirements.txt
(.venv) huangsiyangdeiMac:ch02 huangsiyang$ pip3 install -r requirements.txt
Collecting PyYAML==3.12
Downloading PyYAML-3.12.zip (375 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 375.8/375.8 KB 1.1 MB/s eta 0:00:00
Preparing metadata (setup.py) ... done
Using legacy 'setup.py install' for PyYAML, since package 'wheel' is not installed.
Installing collected packages: PyYAML
Running setup.py install for PyYAML ... done
Successfully installed PyYAML-3.12
import yaml import argparse import sys def main(number, other_number, output): result = number * other_number print(f'The result is {result}', file=output) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('-n1', type=int, help='A number', default=1) parser.add_argument('-n2', type=int, help='Another number', default=1) parser.add_argument('-c', dest='config', type=argparse.FileType('r'), help='config file in YAML format', default=None) parser.add_argument('-o', dest='output', type=argparse.FileType('w'), help='output file', default=sys.stdout) args = parser.parse_args() if args.config: config = yaml.load(args.config) # No need to transform values args.n1 = config['ARGUMENTS']['n1'] args.n2 = config['ARGUMENTS']['n2'] main(args.n1, args.n2, args.output)
(.venv) huangsiyangdeiMac:ch02 huangsiyang$ cat config.yaml
ARGUMENTS:
n1: 7
n2: 4
(.venv) huangsiyangdeiMac:ch02 huangsiyang$ python prepare_task_yaml.py -c config.yaml
The result is 28