Python 官方文档:入门教程 => 点击学习
一:配置文件 1. 什么是配置文件 配置文件是为程序配置参数和初始设置的文件。一般为文本文件,以ini,conf,cnf,cfg,yaml等作为后缀名。 例如Mysql的配置文件my.cnf内容如下: [mysqld]# Only a
配置文件是为程序配置参数和初始设置的文件。一般为文本文件,以ini
,conf
,cnf
,cfg
,yaml
等作为后缀名。
例如Mysql
的配置文件my.cnf
内容如下:
[mysqld]# Only allow connections from localhostbind-address = 0.0.0.0mysqlx-bind-address = 127.0.0.1default_authentication_plugin = mysql_native_passWord
通过配置文件可以使得代码中的参数根据配置文件进行动态配置,而不用直接修改代码的内部,减少风险提高代码复用。
经典应用场景
1 ini/conf/cnf
文件
这类配置文件由节(section),键(key),值(value)由一下格式组成。
[section1]key1=value1key2=value2[section2]key1=value1
2 yaml文件
2.1 简介
yaml文件本质上是一种标记语言,和普通的配置文件相比它能表示更为复杂的数据结构。
它的基本语法规则如下:
#
表示行注释
yaml支持三种数据结构:
2.2 对象
对象的一组键值对使用冒号结构表示
name: xinlanperson: {name: xinlan, age: 18}
2.3 数组
一组连字符开头的行,构成一个数组
- title- username- passwordargs: [title, username, password]
2.4 组合结构
对象数组可以结合使用,形成组合结构
name: xinlanage: 18hobby: [python, 游戏, sport]ouxiang: - name: 刘德华 age: 60 - name: 任达华 age: 65
2.5 标量
yaml可以表示如下数据类型如下:
ConfigParser
模块Python提供内置库ConfigParser
用来解析ini
格式的配置文件。
[log]filename=py45.logdebug=false[mysql]host=127.0.0.1database=lemonuser=rootpassword=123456port=3306
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2021/11/15 21:31# @Author : shisuiyi# @File : read_ini.py# @Software: win10 Tensorflow1.13.1 python3.9from configparser import ConfigParser config = ConfigParser() # 实例化config.read('config.ini') # 读取配置文件print(config.sections()) # 返回所有的section名称字符串,一列表返回print(config.options('mysql')) # 返回指定section下对应的配置项的所有的字符串名称,以列表返回print(config.items('log')) # 返回指定section下所有的配置项的键值对,二元元组print(config.get('mysql', 'port'))print(config.getint('mysql', 'port')) # 指定类型,帮我们转换类型print(config["mysql"]['host']) # 直接以字典取值的方式读取ini文件
输出
C:\Users\12446\AppData\Local\Programs\Python\Python39\python.exe D:/Lemon/py45/day19/read_ini.py['log', 'mysql']['host', 'database', 'user', 'password', 'port'][('filename', 'py45.log'), ('debug', 'false')]33063306127.0.0.1 Process finished with exit code 0
pyyaml
模块python解析yaml文件需要安装第三方库pyyaml
。
pip安装pip install pyyaml
pyyaml库的使用非常简单,它会将整个yaml配置文件内容解析成一个python字典返回。
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2021/11/16 20:22# @Author : shisuiyi# @File : read_yaml.py# @Software: win10 Tensorflow1.13.1 python3.9import yaml with open('config.yaml', 'r', encoding='utf-8') as f: config = yaml.load(f, Loader=yaml.FullLoader) print(config)
输出的是字典
{'log': {'filename': 'py45.log', 'debug': False}, 'mysql': {'host': '127.0.0.1', 'database': 'lemon', 'user': 'root', 'password': '123456', 'port': 3306}}
1 功能分析
封装前,我们先考虑一下,这个配置文件解析模块需要哪些功能?
2 封装成函数
封装思路:
ConfigParser
对象支持字典格式的取值代码封装如下
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2021/11/16 20:33# @Author : shisuiyi# @File : congig_handler.py# @Software: win10 Tensorflow1.13.1 python3.9from configparser import ConfigParserimport yaml def get_config(filename, encoding='utf-8'): """ 获取yaml/ini配置文件中的配置 @param filename: str 文件名 @param encoding: 文件字符编码 """ # 1. 获取配置文件后缀 suffix = filename.split('.')[-1] # 2.判断类型 # 3.分别处理 if suffix in ['yaml', 'yml']: with open(filename, 'r', encoding=encoding) as f: data = yaml.load(f, Loader=yaml.FullLoader) else: conf = ConfigParser() conf.read(filename) data = {} for section in conf.sections(): data[section] = dict(conf.items(section)) # 4. 返回 return data if __name__ == '__main__': res = get_config(r'D:\Lemon\py45\day18\config.yaml') print(res)
3 封装成类
封装思路:
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2021/11/16 20:33# @Author : shisuiyi# @File : congig_handler.py# @Software: win10 Tensorflow1.13.1 python3.9from configparser import ConfigParserimport yaml class Config: def __init__(self, filename, encoding='utf-8'): self.filename = filename self.encoding = encoding self.suffix = self.filename.split('.')[-1] if self.suffix not in ['yaml', 'yml', 'cnf', 'conf', 'ini']: raise ValueError('不能识别的配置文件后缀:{}'.fORMat(self.suffix)) def parse_ini(self): """ 解析ini :return: """ conf = ConfigParser() conf.read(self.filename) data = {} for section in conf.sections(): data[section] = dict(conf.items(section)) return data def parse_yaml(self): """ 解析yaml :return: """ with open(self.filename, 'r', encoding=self.encoding) as f: data = yaml.load(f, Loader=yaml.FullLoader) return data def parse(self): """ 解析配置文件 :return: """ if self.suffix in ['yaml', 'yml']: return self.parse_yaml() else: return self.parse_ini() if __name__ == '__main__': cm = Config(r'D:\Lemon\py45\day20\config.yaml') res = cm.parse() print(res)
一个框架封装的彻不彻底的标准是能否复用,也即是另外一个项目来用时,不需要修改框架的源码。
在我们目前封装的框架中,耦合高的点有:
配置文件config.yaml
log: name: py45 filename: 'D:\Lemon\py45\day18\logs\my.log' debug: truetest_cases_dir: 'D:\Lemon\py45\day18\testcases'test_data_file: 'D:\Lemon\py45\day18\testdata\testdata.xlsx'test_report: report_dir: 'D:\Lemon\py45\day18\reports' title: 'py45期第一份测试报告' desc: '木森老师的测试报告模板' tester: 'shisuiyi'
get_config
函数解析后:
{'log': {'name': 'py45', 'filename': 'D:\\Lemon\\py45\\day18\\logs\\my.log', 'debug': True}, 'test_cases_dir': 'D:\\Lemon\\py45\\day18\\testcases', 'test_data_file': 'D:\\Lemon\\py45\\day18\\testdata\\testdata.xlsx', 'test_report': {'report_dir': 'D:\\Lemon\\py45\\day18\\reports', 'title': 'py45期第一份测试报告', 'desc': '木森老师的测试报告模板', 'tester': 'shisuiyi'}}
我们可以将这些写到配置文件中,然后在框架代码中动态的获取配置文件的相对应设置,实现代码的解耦。
在common
文件夹下的 __init__.py
的文件中调用解析配置文件的函数
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2021/11/13 10:16# @Author : shisuiyi# @File : __init__.py.py# @Software: win10 Tensorflow1.13.1 python3.9from common.log_handler import get_loggerfrom common.read_excel_tool import get_data_from_excelfrom common.congig_handler import get_config conf = get_config(r'D:\Lemon\py45\day19\config.yaml') # 在这里将配置文件解析成字典格式返回logger = get_logger(**conf['log']) # 在这里创建日志器----日志器调用时的传参
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2021/11/8 21:08# @Author : shisuiyi# @File : test_login.py# @Software: win10 Tensorflow1.13.1 python3.9 cases = get_data_from_excel(conf['test_data_file'], 'login') # ---测试用例数据的路径
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2021/11/9 20:14# @Author : shisuiyi# @File : main.py# @Software: win10 Tensorflow1.13.1 python3.9import unittestimport unittestreportfrom common import conf if __name__ == '__main__': discover = unittest.defaultTestLoader.discover(conf['test_cases_dir']) # 表示收集当前目录下所有用例 runner = unittestreport.TestRunner(discover, **conf['test_report']) runner.run()
以上就是今天为各位小伙伴准备的内容,如果你想了解更多关于Python自动化测试的知识和技巧,欢迎关注!
感谢每一个认真阅读我文章的人,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:
来源地址:https://blog.csdn.net/qq_56271699/article/details/131289123
--结束END--
本文标题: python 操作配置文件。
本文链接: https://www.lsjlt.com/news/415635.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0