轻松管理多个环境中的项目配置。
figa的Python项目详细描述
Figa可以读取多个配置源,包括多种文件格式、环境变量, 甚至是因特网,允许你根据你的需要来配置你的项目。在
Figa支持多种来源,包括:
- 环境变量
- Dict对象
- JSON(.JSON)
- HOCON(.HOCON,.conf)
- INI,CFG(.INI,.CFG)
- YAML(.YAML,.yml)
- TOML(.TOML)
- .properties(.properties)属性
- 互联网资源
$ pip install figa
使用
^{pr2}$环境检测
您可以实现自己的函数来检测从何处提取配置值。在
@figa.configclassConfig:development="~/config.yml"# use YAML file for config when developingproduction="env","cfg_"# use environment variables with cfg_ prefix in productiondefget_env(self):if"ON_HEROKU"infiga.env:# figa.env is shortcut for os.environreturn"production"else:return"development">>>config=Config()# if no environment is passed, get_env() will be called.
文件类型
默认情况下,配置文件类型将从文件扩展名猜测。 也可以显式设置:
@figa.configclassMyConfig:example="ini","./config.conf"# .conf would be detected as HOCON, but we set to INI
默认值
可以设置将包含在每个环境中的默认值。在
@figa.configclassMyConfig:default={"name":"My App"}dev={"host":"localhost"}prod={"host":"myapp.com"}>>>dev_cfg=MyConfig("dev")>>>prod_cfg=MyConfig("prod")>>>dev_cfg.name==prod_cfg.name# "name" config item is included in bothTrue
所需值和类型检查
在配置类中,可以设置所需的值和类型,当 配置已加载。这有助于确保代码在运行时不会丢失信息。在
@figa.configclassConfig:# two required config values: `string` & `sub.number`__required__={"string":str,"sub":{"number":int}}
如果缺少任何值,将引发错误:
@figa.configclassConfig:# two required config values: `string` & `sub.number`__required__={"string":str,"sub":{"number":int}}missing_vals={# this config is missing sub.number"string":"hello, world","sub":{}}>>>cfg=Config("missing_vals")ValueError:Missingrequireditem'sub.number'
如果可能,Figa会自动为您转换字符串和数字。在
@figa.configclassConfig:__required__={"stringval":str,"numberval":int}values={"stringval":100,"numberval":"42"}>>>cfg=Config("values")>>>cfg.stringval'100'>>>cfg.numberval42
这个项目是在麻省理工学院授权下发布的。请参见LICENSE.md。在
- 项目
标签: