扩展configparser类并在新类中使用配置解析器

时间:2012-09-26 21:11:08

标签: python class python-3.x configparser

所以我正在尝试创建一个已经在文件中读取的类,并具有configparser的所有功能以及更多功能。 代码如下所示:

import configparser
class dkconfig(configparser):
    def __init__(self):
        self.clusterini = os.path.abspath("..\\cluster.ini")
        super(dkconfig,self).__init__(allow_no_value=True)
        if os.path.exists(self.clusterini):
            self.read(self.clusterini)


    def getHostnames(self):
        hostnames = {}
        for sec in self.config.sections():
            if sec.startswith("node"):
                hostnames[sec] = self.config.get(sec, "hostname")
        return hostnames

从另一个脚本中调用它是这样的:

config = dkconfig()
names = config.getHostnames()
opts = config.options("node1")

错误说明:TypeError: module.__init__() takes at most 2 arguments (3 given)我缺少什么,如何让“dkconfig”对象的所有实例在构造期间都已读入“cluster.ini”文件?

1 个答案:

答案 0 :(得分:3)

嗯,错误的直接原因是您尝试继承configparser 模块。您需要继承,而不是模块。

class dkconfig(configparser.ConfigParser):
    # ....
相关问题