使用配置文件、命令行选项等以简单的方式配置组件/类
configsimple的Python项目详细描述
配置简单
通过命令行选项、配置文件和环境变量配置命令/tall及其组件。
这是在ConfigArgParse包的基础上构建的,但不是替换包
对于ArgumentParser
类,提供它自己的类ConfigSimple
它可以用来定义可能的设置
add_argument
并在分析设置之后,检索
设定值。
每个ConfigSimple
实例都表示“顶级”设置
(类似于通常用于工具和程序的ArgumentParser
)或组件
属于顶级设置实例的设置。
configsimple
包提供了默认的顶级设置singleton,
configsimple.config
。
下面是一个如何定义设置的示例 对于顶层和两个组件,其中 顶层选择要使用的组件:
示例/example1.py:
fromconfigsimpleimporttopconfig,ConfigSimple,flagclassComponent1:def__init__(self):myconf=ConfigSimple(component="comp1")topconfig.add_config(myconf)myconf.add_argument("--foo",default="22",type=int,help="The FOO setting!")myconf.add_argument("--bar",type=flag)myconf.parse_args()print("Component1 foo is {}".format(myconf.get("foo")))classComponent2:def__init__(self):myconf=ConfigSimple(component="comp2")topconfig.add_config(myconf)myconf.add_argument("--foo",default="xyz",type=str,help="The FOO setting, but a different one!")myconf.parse_args()print("Component2 foo is {}".format(myconf.get("foo")))if__name__=="__main__":topconfig.add_argument("--bar",help="The BAR setting")topconfig.add_argument("--foo",help="The toplevel FOO setting")topconfig.add_argument("--comp",type=int,choices=[1,2],required=True,help="Component number")topconfig.add_argument("pos1")topconfig.parse_args()print("Toplevel foo is {}".format(topconfig.get("foo")))compclass=[Component1,Component2][topconfig.get("comp")-1]comp=compclass()print("Get the global comp1.foo: {}".format(topconfig.get("comp1.foo")))print("Get the global comp2.foo: {}".format(topconfig.get("comp2.foo")))print("Get the global comp1.bar: {}".format(topconfig.get("comp1.bar")))print("Top positional parameter pos1: {}".format(topconfig.get("pos1")))
运行此命令的一种方法:
$ python examples/example1.py --comp 1 1 --comp1.foo 2
Toplevel foo is None
Component1 foo is 2
Get the global comp1.foo: 2
Get the global comp2.foo: None
Get the global comp1.bar: None
Top positional parameter pos1: 1
这将选择组件comp1进行初始化,然后将 设置comp1.foo参数。注意位置参数 “pos1”必须在任何组件参数之前指定!
为了获取组件comp1设置的使用信息, 我们可以运行:
$ python examples/example1.py --comp 1 x --comp1.help
Toplevel foo is None
usage: example1.py [--comp1.help] [--comp1.config_file COMP1.CONFIG_FILE]
[--comp1.save_config_file CONFIG_OUTPUT_PATH]
[--comp1.foo COMP1.FOO] [--comp1.bar COMP1.BAR]
optional arguments:
--comp1.help Show help for the 'comp1' component
--comp1.config_file COMP1.CONFIG_FILE
Specify a file from which to load settings for
component 'comp1'
--comp1.save_config_file CONFIG_OUTPUT_PATH
Specify a file to which to save specified settings.
--comp1.foo COMP1.FOO
The FOO setting!
--comp1.bar COMP1.BAR
这将在获取参数后立即显示帮助信息 在组件comp1中分析。为了让它起作用, 必须提供顶级参数。
另一种或许更好的方法,尤其是在可能的情况下 预先知道的组件与此类似:
fromconfigsimpleimporttopconfig,flagclassComponent1:@staticmethoddefconfigsimple(config=None,component="comp1"):myconf=configortopconfig.get_config(component=component)myconf.add_argument("--sub1.sub2.foo",default="22",type=int,help="The FOO setting!")myconf.add_argument("--sub1.sub3.sub4.bar",type=flag)returnmyconfdef__init__(self):cfg=Component1.configsimple()topconfig.add_config(cfg)cfg.parse_args()print("Component1 sub1.sub2.foo is {}".format(cfg.get("sub1.sub2.foo")))classComponent2:defconfigsimple(config=None,component="comp2"):myconf=configortopconfig.get_config(component=component)myconf.add_argument("--foo",default="xyz",type=str,help="The FOO setting, but a different one!")returnmyconfdef__init__(self):myconf=Component2.configsimple()topconfig.add_config(myconf)myconf.parse_args()print("Component2 foo is {}".format(myconf.get("foo")))if__name__=="__main__":topconfig.add_argument("--bar",help="The BAR setting")topconfig.add_argument("--foo",help="The toplevel FOO setting")topconfig.add_argument("--comp",type=int,choices=[1,2],required=True,help="Component number")topconfig.add_argument("pos1")topconfig.add_config(Component1.configsimple())topconfig.add_config(Component2.configsimple())topconfig.parse_args()print("Toplevel foo is {}".format(topconfig.get("foo")))compclass=[Component1,Component2][topconfig.get("comp")-1]comp=compclass()print("Get the global comp1.foo: {}".format(topconfig.get("comp1.foo")))print("Get the global comp2.foo: {}".format(topconfig.get("comp2.foo")))print("Get the global comp1.bar: {}".format(topconfig.get("comp1.bar")))print("Get the global comp1.sub1.sub2.foo: {}".format(topconfig.get("comp1.sub1.sub2.foo")))print("Top positional parameter pos1: {}".format(topconfig.get("pos1")))
这里每个组件都有一个静态函数,它返回一个组件 添加了所有参数的配置。这些配置可以添加到 顶层代码和“-help”将显示顶层的帮助 以及所有添加的配置。
注
这个包是建立在ConfigArgParse包之上并依赖于它的, 但由于代码中的一个问题,对 当前直接包含configargparse.py。