共享配置accross模块的包

config-bat的Python项目详细描述


简介

config_bat是一个帮助开发人员使用config.json文件轻松配置应用程序的包。

入门

  1. 安装过程:
pip install config_bat
  1. 用法:

获取配置:如果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"

默认值是可选的,如果未提供默认值,函数将不返回任何值。

  1. 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"
    }
  }
}

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
相对于框架java窗口的鼠标位置错误   Java 8流peek api   java将数据附加到文件中   java使用ExoPlayer 2.8播放播放列表中的特定文件   JavaSpring国际化:如何动态设置语言环境值   java如何在mysql中实现两个表之间的两个关联   java在gradle可执行jar文件中包含运行时参数   surefire插件中的java maven多套测试套件   java试图理解堆分析以确定内存泄漏或所需的大量内存   java识别字符串有数字   数组如何解决错误“java.lang.ArrayIndexOutOfBoundsException:5”   java Swt文件对话框选择的文件太多?   java此登录代码易受SQL注入攻击吗?   Java[3]中的文件<identifier>预期编译错误   java如何在spring webflux中发送列表   jar中未找到java文件异常   如何在java中合并2D数组?   java如何评测本机JNI库