简单的模块以一种干净的“pythonic”方式加载和使用配置。
confidence的Python项目详细描述
置信度.py:+1:
信心使加载一个或多个 配置值并将其公开为一个易于使用的python对象。 给定以下yaml文件:
foo:bar:42foo.baz:'21isonlyhalftheanswer'foobar:the answer is ${foo.bar}…
自信地使用它:
# load configuration from a YAML fileconfiguration=confidence.loadf('path/to/configuration.yaml')# a Configuration object is like a dict, but bettervalue=configuration.get('foo.bar')value=configuration.get('foo.bar',default=42)# or even kwargs, should you want to# (passing bar=42 and foo='21 is only half the answer')function(**configuration.foo)# namespaces are one honking great idea -- let's do more of those!value=configuration.foo.bar# they're even safe when values might be missingvalue=configuration.foo.whoopsieifvalueisNotConfigured:value=42# or, similarvalue=configuration.foo.whoopsieor42# even references to other configured values will workvalue=configuration.foobar# 'the answer is 42…'
通常,当 定义默认值或读取多个文件:
configuration=confidence.loadf('/etc/system-wide-defaults.yaml','./local-overrides.yaml')# confidence provides a convenient way of using this kind of precedence,# letting 'more local' files take precedence over system-wide sources# load_name will attempt to load multiple files, skipping ones that# don't exist (using typical *nix paths, XDG-specified locations, some# Windows environment variables and typical OSX paths):# - /etc/xdg/app.yaml# - /etc/app.yaml# - /Library/Preferences/app.yaml# - C:/ProgramData/app.yaml# - ~/.config/app.yaml# - ~/Library/Preferences/app.yaml# - ~/AppData/Roaming/app.yaml# - ~/.app.yaml# - ./app.yamlconfiguration=confidence.load_name('app')# if set, load_name will take a look at environment variables like# APP_FOO_BAR and APP_FOO_BAZ, mixing those in as foo.bar and foo.baz# the default load order can be overridden if necessary:configuration=confidence.load_name('app',load_order=confidence.loaders(# loading system after user makes system locations take precedenceconfidence.Locality.user,confidence.Locality.system))
虽然功能强大,但任何一套便利功能都无法满足 每个人的用例。能够像 可能,信心并没有隐藏其灵活的内部API。
# let's say application defaults are available as a dict in sourceapp_defaults={'foo':{'bar':42},'foo.baz':'21 is only half the answer'}# and we've already created a way to read a dict from somewheredefread_from_source(name):...returnread_values# all of this can be combined to turn it into a single glorious Configuration instance# precedence rules apply here, values from read_from_source will overwrite both# app_defaults and values read from fileconfiguration=confidence.Configuration(app_defaults,# yeah, this would be a Configuration instance# remember it's just like a dict?confidence.loadf('path/to/app.yaml'),read_from_source('app'))# make it so, no. 1run_app(configuration)
更改
0.6.1(2019-04-12)
- 修正了当传递到配置的源本身是配置实例时加载期间解析引用的问题。
0.6(2019-04-05)
- 添加缺少的策略以控制在属性访问时如何处理未配置的键
- 将单个文件模块拆分为多模块包(可从confidence包导入的面向用户的名称)
- 合并/拆分非-str类型键时引发错误,避免出现混乱和损坏的访问模式问题
0.5(2019-02-01)
- 从值启用引用键
- 为加载名到加载程序和位置启用自定义加载顺序(默认行为保持不变)
0.4.1(2018-11-26)
- 对与配置成员冲突的配置键的属性访问发出警告。
0.4(2018-07-09)
- 在环境变量中启用转义下划线(NAME_FOO__BARresults inconfig.foo_bar)。
- 使用yaml.safe_load可避免yaml.load的安全问题。
- 尝试在配置的实例上设置非受保护属性时引发AttributeError。
0.3(2018-05-24)
- 启用忽略loadf中丢失的文件。
- 修复读取空文件或仅注释yaml文件时的崩溃。
0.2(2018-03-06)
- 从XDG-specified目录读取文件。
- 从环境变量PROGRAMDATA、APPDATA和LOCALAPPDATA中指定的系统范围和用户本地目录读取文件(按顺序)。
- 从/Library/Preferences和~/Library/Preferences读取文件。
0.1.1(2018-01-12)
- 将参数的用户目录展开到loadf,包括EXAMPLE_CONFIG_FILE环境变量的值。
0.1(2017-12-18)
- 初次发布。