共享配置accross模块的包
config-bat的Python项目详细描述
简介
config_bat是一个帮助开发人员使用config.json文件轻松配置应用程序的包。
入门
- 安装过程:
pip install config_bat
- 用法:
获取配置:如果config.json中没有配置,则返回默认值
fromconfig_batimportconfigyour_config=config.get("config_key",default_value)# Get nested config data# You can get nested dict data for cases that you have many value spread out for the same master key# Example dict: two mongodb master key config, but only username is different from dev and prod# The "common" key is readed by default.# If the config apears both in "common" and other stages, the stage config will be used.nestedConfig={"common":{"mongodb":{"host":"localhost"},"development":{"mongodb":{"username":"test"}}}}host=config.get("mongodb",default_value)["host"]# Return KeyErrorhost=config.get("mongodb.host",default_value)# Return "test"
默认值是可选的,如果未提供默认值,函数将不返回任何值。
- API引用:
环境变量:
导出这些环境变量可以覆盖包行为:
配置路径:配置文件的路径。默认值:“/config/config.json”。
app_stage:在config.json中定义。
config.json文件:
第一级的关键是应用程序的阶段。您可以定义嵌套在这些键中的配置。
默认情况下会读取“common”键。如果配置同时出现在“common”和其他阶段,则将使用阶段配置。
如果要使用环境变量,请在变量名称前放置“$”字符。
config.json示例:
{
"common": {
// Environmental variables
"JWT_SECRET": "$SEC_KEY",
"port": 8080
},
"development": {
// Nested config
"mongodb": {
"host": "localhost",
"user": "dev_user",
"pass": "dev_pass"
}
},
"production": {
"mongodb": {
"host": "your.domain",
"user": "prod_user",
"pass": "prod_pass"
}
}
}