The LightConfig library enables you to get a easy way to create or read config file
LightConfig is conveniently available via pip:
pip install lightconfig
or installable via git clone
and setup.py
git clone https://github.com/daassh/LightConfig.git python setup.py install
from lightconfig import LightConfig
cfg = LightConfig("config.ini")
(if specific path not exists, the path file will be create)
Usually, you can read/write config by both attribute and item
>>> cfg.section.option = 'value'
>>> cfg.section.option
'value'
>>> cfg['section']['option'] = 'value'
>>> cfg['section']['option']
'value'
>>> cfg.section1 = {'option1': 'value1'}
>>> cfg['section2'] = {'option2': 'value2'}
>>> cfg
{'section1': {'option1': 'value1'}, 'section2': {'option2': 'value2'}}
But in some situation, you can only use read/write by item:
wrong
>>> cfg.section-2.2option = 'value2'
...
SyntaxError: invalid syntax
right
>>> cfg['section-2']['2option'] = 'value2'
keys and __dict__ are inner method of LightConfig (keys used to make LightConfig object dictable, __dict__ used to visit other inner method of LightConfig), so when using keys or __dict__ as section/option name, you can only access it by item:
wrong
>>> cfg.keys.option3 = 'value3'
...
AttributeError: 'method' object has no attribute 'option3
>>> cfg.keys
<bound method LightConfig.keys of ...>
>>> cfg.__dict__.option4 = 'value4'
...
AttributeError: 'dict' object has no attribute 'option4'
>>> cfg.__dict__
{'_config_path': '', '_try_encoding': {}, ...}
right
>>> cfg['keys'].option3 = 'value3'
>>> cfg['keys']
{'option3': 'value3'}
>>> cfg['__dict__'].option4 = 'value4'
>>> cfg['__dict__']
{'option4': 'value4'}
use del to delete section/option:
>>> del cfg.section.option
>>> 'option' in cfg.section
False
>>> del cfg['section']
>>> 'section' in cfg
False
use dict to convert LightConfig or LightConfig.Section object to dict:
>>> type(dict(cfg))
<class 'dict'>
>>> type(dict(cfg.section))
<class 'dict'>
>>> for section_name, section_info in cfg:
... print(section_name)
... for option, value in section_info:
... print(' {}={}'.format(option, value))
section1
option1=value1
section2
option2=value2