Python是面向对象的语言,以下我对ConfigParaser.ConfigParaser对象的扩展。添加了 get_client 方法,对自定义参数的分解过程。
#coding=utf-8 import ConfigParser import re # 对象继承自对象 ConfigParser.ConfigParser class Config( ConfigParser.ConfigParser ): ''' 提取从服务器列表数据 配置信息实例 [hosts] client = root:[email protected]^[email protected]:3306,root:[email protected]:3306 多个服务器以英文豆号进行分隔 Example import libs.configure as config conf = config.Config() conf.read( "default.conf" ) clinets = conf.get_client( "hosts", "client" ) ''' def get_client( self, section, option ): item = self.get( section, option ) values = item.split(",") confs = {} for conf in values: match = re.match( r"(\w+?):(.+)@([^:]+):(\d+)", conf ) if match: user,password,host,port = match.groups() confs[host] = {"host":host,"user":user,"password":password,"port":int(port)} return confs
时间: 2024-10-09 23:58:23