【问题标题】:Python config file as JSON with inheritancePython 配置文件为 JSON 与继承
【发布时间】:2016-10-19 11:56:33
【问题描述】:

我正在寻找可以轻松从 JSON 文件返回配置对象的库,而不仅仅是读取 JSON 文件并返回它 - 将继承应用于部分。 类似于旧的 *.ini 或 Zend_Config_JSON(来自 PHP Zend Framework)

例子:

{
    "production":{
        "phpSettings":{
            "display_startup_errors": false,
            "display_errors": false
        },
        "includePaths":{
            "library": "APPLICATION_PATH/../library"
        },
        "bootstrap":{
            "path": "APPLICATION_PATH/Bootstrap.php",
            "class": "Bootstrap"
        },
        "appnamespace": "Application",,
        "resources":{
            "frontController":{
                "controllerDirectory": "APPLICATION_PATH/controllers",
                "moduleDirectory": "APPLICATION_PATH/modules",
                "params":{
                    "displayExceptions": false
                }
            },
            "modules":[],
            "db":{
                "adapter": "pdo_sqlite",
                "params":{
                    "dbname": "APPLICATION_PATH/../data/db/application.db"
                }
            },
            "layout":{
                "layoutPath": "APPLICATION_PATH/layouts/scripts/"
            }
        }
    },
    "staging":{
        "_extends": "production"
    },
    "testing":{
        "_extends": "production",
        "phpSettings":{
            "display_startup_errors": true,
            "display_errors": true
        },
    },
    "development":{
        "_extends": "production",
        "resources":{
            "frontController":{
                "params":{
                    "displayExceptions": true
                }
            }
        }
    }
}

作为回报,我将拥有其中一个部分,该部分具有“_extend”属性,它将是扩展部分顶部(合并/更新)的此部分。

是某个库已经这样做了,还是 python 社区更喜欢以不同的方式处理继承?

【问题讨论】:

    标签: python json django inheritance config


    【解决方案1】:

    请看pyconfigini,它是仿照 Zend_Config_Ini 的配置文件解析器

    【讨论】:

      【解决方案2】:

      检查这个

      cfg = JSONConfigParser()
      
      cfg.read_string("""
      [section]
      number = 3.141592654
      dictionary = {"key": "value"}
      list = [1,
              2,
              3]
      nested = {"list": [1,2,3]}
      true = true
      none = null
      
      [DEFAULT]
      # settings in the default section are inherited
      # by all other sections.
      default-setting = "default"
      """)
      
      # read a setting
      cfg.get("section", "number")
      
      # read a setting using index notation
      cfg["section"]["true"]
      
      # settings inherited from DEFAULT
      cfg.get("section", "default-setting")
      

      在这里继承

      # settings inherited from DEFAULT
      cfg.get("section", "default-setting")
      

      参考:https://pypi.python.org/pypi/json-config-parser/0.1.2

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-28
        • 1970-01-01
        • 1970-01-01
        • 2019-03-28
        • 1970-01-01
        相关资源
        最近更新 更多