一种模块,用于在一致性方法中从各种源检索程序设置。
setoptconf的Python项目详细描述
setoptconf是一个可用于检索程序设置的python库 来自多种常见来源:
- 命令行
- 环境变量
- ini文件
- json文件
- yaml文件
- python对象/模块
这个项目的目标是在一个简单的 一致的方法,然后将setoptconf指向尽可能多的源 喜欢使用,让它梳理它们,寻找你的设置。
无可否认,这个自述文件在细节上非常轻巧。完整的文档将提供 及时。现在,这里有一个使用示例:
导入库:
import setoptconf as soc
实例化管理器:
manager = soc.ConfigurationManager('myprogram')
定义要收集的设置:
manager.add(soc.StringSetting('foo')) manager.add(soc.IntegerSetting('bar', required=True)) manager.add(soc.BooleanSetting('baz', default=True))
从我们想要的资源中检索设置,将设置和 用我们传递的源的顺序所隐含的优先级覆盖:
config = manager.retrieve( # This source pulls from the command line using argparse. soc.CommandLineSource, # This source pulls from environment variables that are prefixed # with MYPROGRAM_* soc.EnvironmentVariableSource, # This source pulls from the named INI files. It stops at the first # file it finds. soc.ConfigFileSource(('.myprogramrc', '/etc/myprogram.conf')), )
我们现在有一个名为config的配置对象,它有三个属性; foo、bar和baz。