码迷,mamicode.com
首页 > 编程语言 > 详细

学习Python的对象继承

时间:2014-11-27 14:29:41      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:python   继承   

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

#coding=utf-8
import ConfigParser
import re

# 对象继承自对象 ConfigParser.ConfigParser
class 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


学习Python的对象继承

标签:python   继承   

原文地址:http://blog.csdn.net/yagas/article/details/41545503

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!