python封装configparser模块获取conf.ini值(优化版)

  昨天晚上封装了configparser模块,是根据keyname获取的value。python封装configparser模块获取conf.ini值

  我原本是想通过config.ini文件中的section和keyname获取value的,前两天怎么都调试不通过。今天百度了一下,有人通过字典的方式把我的和这个想法实现了,我把这个例子修改了一下,代码如下,并通过测试,以后可以用在自动化测试框架中:

 1 #coding:utf-8
 2 import os
 3 import ConfigParser
 4
 5 class Dictionary(dict):
 6     ‘‘‘
 7     把config.ini中的参数添加值dict
 8     ‘‘‘
 9     def __getattr__(self, keyname):
10         #如果key值不存在则返回默认值"not find config keyname"
11         return self.get(keyname, "config.ini中没有找到对应的keyname")
12
13 class Config(object):
14     ‘‘‘
15     ConfigParser二次封装,在字典中获取value
16     ‘‘‘
17     def __init__(self):
18         # 设置conf.ini路径
19         current_dir = os.path.dirname(__file__)
20         top_one_dir = os.path.dirname(current_dir)
21         file_name = top_one_dir + "\\conf\\conf.ini"
22         # 实例化ConfigParser对象
23         self.config = ConfigParser.ConfigParser()
24         self.config.read(file_name)
25         #根据section把key、value写入字典
26         for section in self.config.sections():
27             setattr(self, section, Dictionary())
28             for keyname, value in self.config.items(section):
29                 setattr(getattr(self, section), keyname, value)
30
31     def getconf(self, section):
32         ‘‘‘
33         用法:
34         conf = Config()
35         info = conf.getconf("main").url
36         ‘‘‘
37         if section in self.config.sections():
38             pass
39         else:
40             print("config.ini 找不到该 section")
41         return getattr(self, section)
42
43 if __name__ == "__main__":
44     conf = Config()
45     info = conf.getconf("main").url
46     print info

运行结果:

conf.ini文件

原文地址:https://www.cnblogs.com/zhuque/p/8365217.html

时间: 2024-08-10 06:16:03

python封装configparser模块获取conf.ini值(优化版)的相关文章

Python中ConfigParser模块应用

Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser,ConfigParser和SafeConfigParser.其中RawCnfigParser是最基础的INI文件读取类,ConfigParser.SafeConfigParser支持对%(value)s变量的解析. 下面看看怎样通过ConfigParser类来解析一个ini文件. 配置文件settings.cfg [DEFAULT] myk

python之ConfigParser模块处理ini文件

ini文件 [global]      #######global为session  IPADDR为option    115.182.1.157为值 IPADDR = 115.182.1.157        ######为键值对 IPADDR2 = 10.0.3.157 [7192] mysql = 3306 port = 80 mem = 90% load = 2 inode = 90% disk = 90% 详细介绍 >>> import ConfigParser     ###

python的ConfigParser模块

简介 ConfigParser模块在python3中修改为configparser.这个模块定义了一个ConfigParser类,该类的作用是使用配置文件生效,配置文件的格式和windows的INI文件的格式相同 该模块的作用 就是使用模块中的RawConfigParser().ConfigParser(). SafeConfigParser()这三个方法(三者择其一),创建一个对象使用对象的方法对指定的配置文件做增删改查 操作. 配置文件有不同的片段组成和Linux中repo文件中的格式类似:

python 之ConfigParser模块学习

1.1 读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该section的所有option -items(section) 得到该section的所有键值对 -get(section,option) 得到section中option的值,返回为string类型 -getint(section,option) 得到section中option的值,返回为int类型 1

Python基础-ConfigParser模块

此模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 来看一个好多软件的常见配置文件格式如下 [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [bitbucket.org] User = hg [topsecret.server.com] Port = 50022 ForwardX11 = no 解析配

python:使用netifaces模块获取本机IP网关等信息

python获取本机IP有很多种方法,可每种方法都有局限性. 使用netifaces模块获取本机IP网关等信息,需要安装netifaces模块,不管windows还是linux都可以通用. 一.程序: #!/usr/bin/env python2 # -*- coding: utf-8 -*- #实现本地网卡IP #需要安装netifaces模块 def GetNetworkIP():     #获取本地网卡IP地址     import netifaces     #routingGatewa

python之configParser模块读写配置文件

借鉴:http://www.cnblogs.com/TankXiao/p/3038350.html configParser是python自带的模块,用来读写配置文件 配置文件的格式:[]包含的叫section,section下有option=value这样的键值 配置文件  test.conf [section1] name = tank age = 28 [section2] ip = 127.0.0.1 port = 8080 python代码 #-*- coding:UTF-8 -*-

python之configparser模块

1. 简介 configparser用于配置文件解析,可以解析特定格式的配置文件,多数此类配置文件名格式为XXX.ini,例如mysql的配置文件.在python3.X中 模块名为configparser ,在python2.X中使用的模块名为ConfigParser. ##### ini 文件示例 ######## [section1] name = wang age = 18 [section2] name:python age = 19 #### 文件格式说明 ######### [XXX

python 之 configparser 模块

[[email protected] python]# vim config.py import configparser config = configparser.ConfigParser() config["DEFAULT"] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9'} config['bitbucket.org'] = {} config['bitbucket.or