多级统一配置

configuration的Python项目详细描述


python使用的多级统一配置

  • you have a (python) program that wants to read configuration from configuration files (I currently support JSON and YAML) and also from the command line [TODO: environment variables]
  • you want to be able to serialize and deserialize configuration

基本用法

configuration.Configuration类是抽象基 扩展optparse.OptionParser的类。的形式 配置是通过在 子类。options是一种形式的字典:

{'name': {<value>}}

name是配置选项的名称,valuedict,给出了选项的形式。

Configuration将这些选项转换为OptionParser选项。

value的选项包括:

  • help : what the option is about (translated to command line help)
  • default: default value for the option
  • required: if a true value, this option must be present in the configuration. If ^{tt11}$ is a string, it will be displayed if the option is not present. If the default is defined, you won’t need required as the default value will be used
  • type: type of the option. Used to control the parsing of the option
  • flags: a list that, if present, will be used for the command line flags. Othwise, the option name prepended by ^{tt12}$ will be used. To disable as a command line option, use an empty list ^{tt13}$

此外,您可以扩展Configuration,并拥有 optionsvaluedict中的有用项。

例如,请参见 http://k0s.org/mozilla/hg/configuration/file/c831eb58fb52/tests/example.py#l7

配置文件

配置文件对于(imho)复杂的设置很有用; b.再现性;c.能够共享运行时配置。 如果配置不包含任何内容,则后者非常有用 特定于计算机(例如,可执行文件的路径可能不同于 或者如果配置可以从 命令行。

configuration具有序列化(转储)和反序列化的功能 (加载)可插入格式集的配置。默认情况下, --dump <filename>将转储结果配置(即 从命令行选项和加载的配置文件中收集) 文件扩展名指定格式的文件(例如: --dump mydumpfile.json将使用json格式)。国旗 选项,例如--dump,可以通过dump参数设置为 Configuration的构造函数。

Configuration实例也可以反序列化数据。正常情况下 使用配置是指您希望能够从 配置文件。默认情况下,Configuration实例读取 要加载的配置文件的位置参数。如果你 为Configuration构造函数指定一个load参数,这个 将改为使用选项。同样,文件扩展名将是 用于确定格式。

configuration package 在旧的python上需要json``(``simplejsonPyYAMLso 如果安装包,则这些序列化程序/反序列化程序可用。

扩展配置

configuration设计为可插入。当你得到有用的 开箱即用的行为集,大多数处理程序 configuration可能被操纵来做您想做的事情。

Configuration的构造函数接受一个参数types,它是 键入类型的可调用字典,用于翻译 Configuration.options进入optparse选项。如果其中一个 Configuration.options类型未指定(或是None),则 使用默认值(configuration.base_cli,除非覆盖此值)。 如果未通过,则Configuration实例使用configuration.types

types中的可调用项应采用选项名和值 字典并应返回 实例化一个optparse.Option

Configuration的构造函数也接受一个选项, configuration_providers,这是 要使用的序列化程序/反序列化程序。这些应该是带有列表的对象 要使用的extensions的,将加载的read(filename)方法 配置,并使用write(config, filename)方法编写它。 read应该返回读取配置。 如果write不存在,则提供程序无法序列化。

待办事项

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

推荐PyPI第三方库


热门话题
用于批量操作的java RESTful API分块响应   java读取在线存储的文本文件   在Java ME中将双精度舍入到小数点后5位   java查找一个数字的最接近因子   java更改JMenuBar的字体   java Kmeans聚类算法运行时间和复杂性   java是否可以阻止try catch返回null   java内容解析器指向具有正确URI的错误表   java Android Kotlin插装测试未被识别为插装测试   java TestNG@Dataprovider   在forloop和print语句中声明变量时发生java错误   java在Android Studio 3中设置JNI