1⃣️ 说明
此模块用来读取配置文件,封装了其他格式配置文件的访问与加载,并可以通过以属性的方式来访问配置项。
现已支持ini, yaml,conf格式的配置文件
1⃣️1⃣️ 属性访问的方式如下:
当按照如下结构注册配置文件时
[info]
sex: female
name: xiaoming
age: 18代码访问方式为:
cfg.CONF.INFO.nameini配置文件的section名已转换为大写,本例中info转换为INFO
2⃣️ 示例
from oocfg.cfg import cfg
from oocfg.config import options
info_opts = [
options.StrOpt('name', default='Joe', helper='name info'),
options.IntOpt('age', default=18, helper='age info'),
options.StrOpt('gender', default='female', helper='gender info', choices=['female', 'male']),
options.ListOpt('books', default=[], helper='books info'),
options.BoolOpt('humour', default=True, helper='is humour'),
options.FloatOpt('height', default=1.78, helper="height(m)")
]
class_opts = [
options.IntOpt('grade', default='6', helper='the class grade'),
options.StrOpt('school', default='No1. School', helper='school name')
]
cfg.register_group("education", class_opts)
cfg.register_group("info", info_opts)
file = '/The/path/to/your/config/file/config.ini'
cfg.startup(config_file=file)
print(cfg.CONF.INFO.name)详见测试脚本test.py