学习Python的对象继承

来源:互联网 发布:淘宝外卖麻辣烫分口袋 编辑:程序博客网 时间:2024/05/22 11:50

Python是面向对象的语言,以下我对ConfigParaser.ConfigParaser对象的扩展。添加了 get_client 方法,对自定义参数的分解过程。

#coding=utf-8import ConfigParserimport re# 对象继承自对象 ConfigParser.ConfigParserclass Config( ConfigParser.ConfigParser ):        '''    提取从服务器列表数据    配置信息实例    [hosts]    client = root:passwo@^rd@101.101.100.90:3306,root:password@100.10.100.110: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


1 0