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



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

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"文件?

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

class dkconfig(configparser.ConfigParser):
    # ....

最新更新