金字塔设置类型转换

Pyramid settings type conversions

我正在从金字塔的 registry.settings 获取一些设置。不幸的是,我无法弄清楚为什么某些设置(主要是金字塔特定的设置)会自动转换,而其他设置(我的自定义设置)却不会。

development.ini包含:

pyramid.reload_templates = true
stripe.enabled = false

我最终得到的设置字典如下:

{
  "pyramid.reload_templates": True,
  "stripe.enabled": "false",
}

如何将我的条目也标记为 "convert to bool"?

在 main() 函数中,您将获得此 key/value 对作为设置,并且必须进行任何必要的类型转换,然后才能将字典作为设置传递给配置器。例如:

[app:main]
debug_frobnosticator = True

def main(global_config, **settings):
    from pyramid.settings import asbool
    debug_frobnosticator = asbool(settings.get(
        'debug_frobnosticator', 'false'))
    settings['debug_frobnosticator'] = debug_frobnosticator
    config = Configurator(settings=settings)

asbool(s) Return 如果字符串输入 s 的小写值是真实字符串,则布尔值 True。如果 s 已经是布尔值 True 或 False 之一,return它。