一个基于python配置分析器的简单配置读取器。
ini-config-reader的Python项目详细描述
这是一个从ini文件读取python配置的简单项目。 具有以下功能:
- 它使用singleton模式只配置读卡器一次,并且 在应用程序中使用它。
- 更改配置文件时重新加载配置
- 以字符串形式读取操作系统变量,例如“%(LANG)s”
- 配置会自动转换为python数据类型
- 将配置节收集为字典
可用的数据类型是:-字符串,由“”或“”包围- 整数-浮点数-列表,[1,2,3]-字典,{'test':{'is_dict': 真的}
安装
要使用配置读取器,请使用pip简单安装
$ pip install ini-config-reader
要求
python3测试了版本3.5、3.6和3.7
用法
conf_reader有三个类变量:INI_FILE、RELOAD和 dict_缓存。
Variable | Description |
---|---|
INI_FILE | Path to the file that contains the configurations |
RELOAD | Set if the INI file should be reloaded when changes are detected |
DICT_CACHE | When collecting the section as dictionary store it in a dict to avoid processing the reading |
经常使用
由于config_reader实现了singleton模式,因此用户只能 需要配置一次ini_文件。那么get方法和 get_section_dict可以用作静态方法。
fromconf_reader.readerimportConfReaderConfReader('/etc/configuration/my_config.ini')ConfReader.get('Section_1','configuration_1')# Can be used in different modules
装潢师
配置读取器使用decorator在 功能。要使用它,必须已经配置了ini_文件,并且 CONFIG变量必须存在于函数的签名中。
fromconf_reader.readerimportConfReader,read_conf@read_conf("SECTION_1",'config_2')defread_with_arguments(*args,config,**kwargs):passConfReader('/etc/configuration/my_config.ini')read_with_arguments()
许可证
这个项目是根据麻省理工学院的许可证授权的-参见 LICENSE.txt有关详细信息的文件