The ConfigParser standard usually requires access through config['section_name']['key'] , which is easy. A small modification may provide access to attributes:
class AttrDict(dict): def __init__(self, *args, **kwargs): super(AttrDict, self).__init__(*args, **kwargs) self.__dict__ = self
AttrDict is a class derived from dict that provides access through dictionary keys and attribute access: this means ax is a['x']
We can use this class in ConfigParser :
config = configparser.ConfigParser(dict_type=AttrDict) config.read('application.ini')
and now get application.ini with:
[general] key = value
but
>>> config._sections.general.key 'value'
Robert Siemer Apr 28 '15 at 16:56 2015-04-28 16:56
source share