配置文件应用
在公司项目中,我们常常需要修改测试环境的一些配置,当我们有配置文件时,我们就只需要修改配置文件,常用的配置文件 Httpd.conf、cogfig.ini、config.yml,在python中我们使用*.properties
import configparser
conf = configparser.ConfigParser()
conf.read(cfgpath,encoding = ‘utf-8’)
conf.get(sec,option)
import os
import configparser
import xlrd
from case_info import CaseInfo
class ExcelDataInof:
def __init__(self,file_path):
self.file_path = file_path
def read_data_in_class(self):
all_case_info = []
try:
excel_file = xlrd.open_workbook(self.file_path)
sheet = excel_file.sheet_by_index(0)
for i in range(1,sheet.nrows):
case_info = CaseInfo(sheet.cell_value(i,0),
sheet.cell_value(i,1),
sheet.cell_value(i,2),
sheet.cell_value(i,3),
sheet.cell_value(i,4),
sheet.cell_value(i,5),
sheet.cell_value(i,6),
sheet.cell_value(i,7))
all_case_info.append(case_info)
except FileNotFoundError as e:
print('文件未找到')
except IndexError as e:
print('表格sheet下标越界')
except Exception as e:
print('系统异常:'+str(e))
return all_case_info
if __name__ == '__main__':
cerrunt_path = os.path.dirname(__file__)
conf = configparser.ConfigParser() #创建配置文件对象
cfgpath = cerrunt_path+'/../conf/config.ini' #查看配置文件路径
conf.read(cfgpath,encoding="utf-8") #查看配置文件
Excel_data = ExcelDataInof(conf.get('default','excel_path')) #获取某个配置
str1 = Excel_data.read_data_in_class()
print(str1[0].act_results)
封装配置文件读取:
import configparser
import os
cerrunt_path = os.path.dirname(__file__)
cfgpath = cerrunt_path + '/../conf/config.ini'
class ConfigUtils:
def __init__(self,config_path=cfgpath):
self.config_path = config_path
self.conf = configparser.ConfigParser()
self.conf.read(config_path,encoding='utf-8')
def read_ini(self,sec,option):
value = self.conf.get(sec,option)
return value
def get_excel_path(self):
value = self.read_ini('default','excel_path')
return value
if __name__=='__main__':
config_u = ConfigUtils()
print(config_u.get_excel_path())
顶层应用配置文件读取模块:
import os
import xlrd
from case_info import CaseInfo
from config_utils import ConfigUtils
class ExcelDataInof:
def __init__(self,file_path):
self.file_path = file_path
def read_data_in_class(self):
all_case_info = []
try:
excel_file = xlrd.open_workbook(self.file_path)
sheet = excel_file.sheet_by_index(0)
for i in range(1,sheet.nrows):
case_info = CaseInfo(sheet.cell_value(i,0),
sheet.cell_value(i,1),
sheet.cell_value(i,2),
sheet.cell_value(i,3),
sheet.cell_value(i,4),
sheet.cell_value(i,5),
sheet.cell_value(i,6),
sheet.cell_value(i,7))
all_case_info.append(case_info)
except FileNotFoundError as e:
print('文件未找到')
except IndexError as e:
print('表格sheet下标越界')
except Exception as e:
print('系统异常:'+str(e))
return all_case_info
if __name__ == '__main__':
cerrunt_path = os.path.dirname(__file__)
conf = ConfigUtils()
excel_path = conf.get_excel_path()
Excel_data = ExcelDataInof(excel_path) #获取某个配置
str1 = Excel_data.read_data_in_class()
print(str1[0].act_results)
改造
1、把获取某个属性的方法改为属性方法
@property
import configparser
import os
cerrunt_path = os.path.dirname(__file__)
cfgpath = cerrunt_path + '/../conf/config.ini'
class ConfigUtils:
def __init__(self,config_path=cfgpath):
self.config_path = config_path
self.conf = configparser.ConfigParser()
self.conf.read(config_path,encoding='utf-8')
def read_ini(self,sec,option):
value = self.conf.get(sec,option)
return value
@property
def get_excel_path(self):
value = self.read_ini('default','excel_path')
return value
@property
def get_email_path(self):
value = self.read_ini('email','excel_path')
return value
@property
def get_word_path(self):
value = self.read_ini('word','excel_path')
return value
if __name__=='__main__':
config_u = ConfigUtils()
print(config_u.get_excel_path)
2、改造二:在封装类中加入一个创建好对象
直接在类中创建对象,在其他类中使用时,用from导入改对象
from config_utils import config_u
import configparser
import os
cerrunt_path = os.path.dirname(__file__)
cfgpath = cerrunt_path + '/../conf/config.ini'
class ConfigUtils:
def __init__(self,config_path=cfgpath):
self.config_path = config_path
self.conf = configparser.ConfigParser()
self.conf.read(config_path,encoding='utf-8')
def read_ini(self,sec,option):
value = self.conf.get(sec,option)
return value
@property
def get_excel_path(self):
value = self.read_ini('default','excel_path')
return value
@property
def get_email_path(self):
value = self.read_ini('email','excel_path')
return value
@property
def get_word_path(self):
value = self.read_ini('word','excel_path')
return value
config_u = ConfigUtils()
if __name__=='__main__':
print(config_u.get_excel_path)