Python面向对象编程10 配置文件

文章介绍了如何在Python中使用configparser模块读取配置文件,如Httpd.conf、config.ini等,以及如何处理Excel数据。通过创建ConfigUtils类封装配置文件的读取,使用@property装饰器将获取属性的方法转换为属性访问。此外,展示了如何从配置文件中获取Excel路径并读取Excel数据。
摘要由CSDN通过智能技术生成

配置文件应用

在公司项目中,我们常常需要修改测试环境的一些配置,当我们有配置文件时,我们就只需要修改配置文件,常用的配置文件 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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值