Python配置解析器包装器
我有一个配置文件,我想通过输入节和选项名来加载选项dynamicali 例如,这是配置文件内容Python配置解析器包装器,python,configparser,Python,Configparser,我有一个配置文件,我想通过输入节和选项名来加载选项dynamicali 例如,这是配置文件内容 [templates] path = /full/path/to/template 以及config.py文件 class Config(object): def __init__(self): self.config = ConfigParser.ConfigParser() self.config.read('config.ini') f
[templates]
path = /full/path/to/template
以及config.py文件
class Config(object):
def __init__(self):
self.config = ConfigParser.ConfigParser()
self.config.read('config.ini')
for section in self.config.sections():
self.__dict__[section] = dict()
for k, v in self.config.items(section):
self.__dict__[section][k] = v
def from_options(self, section, option):
if section in self.__dict__:
if option in self.__dict__[section]:
return self.__dict__[section][option]
if __name__ == '__main__':
c = Config()
print c.from_options('templates', 'path')
编辑:
问题是这是否是一种pythonic==正确的方法。config.py
;# if not applicable leave it blank
[templates]
path = /full/path/to/template
configparse.py
class Config(object):
def __init__(self):
self.config = ConfigParser.ConfigParser()
self.config.read('config.py')
def get_path(self):
return self.config.get('templates', 'path')
if __name__ == '__main__':
c = Config()
print c.get_path()
这应该可以做到…config.py
;# if not applicable leave it blank
[templates]
path = /full/path/to/template
configparse.py
class Config(object):
def __init__(self):
self.config = ConfigParser.ConfigParser()
self.config.read('config.py')
def get_path(self):
return self.config.get('templates', 'path')
if __name__ == '__main__':
c = Config()
print c.get_path()
这应该可以解决问题……你的问题是什么?你的问题是什么?