在一个地方解析文件、环境和其他地方的配置值。

easy-config的Python项目详细描述


下面是使用easy_config的完整工作示例。首先,编写配置类:

# config.pyfromeasy_configimportEasyConfigclassMyProgramConfig(EasyConfig):FILES=['myprogram.ini']NAME='MyProgram'# the name for the .ini file section and the namespace prefix for environment variables# define the options like you would a dataclassnumber:intname:strcheck_bounds:bool=True# options with defaults must all come after non-default options

示例配置文件:

# myprogram.ini[MyProgram]# section name matches `NAME` from the configuration classnumber=3

以及演示用法的示例程序:

# test_config.pyimportsysfromconfigimportMyProgramConfigprint(MyProgramConfig.load(name=sys.argv[1])

使用各种选项运行此程序:

$ python test_config.py Scott
MyProgramConfig(number=3, name='Scott', check_bounds=True)

$ env MYPROGRAM_CHECK_BOUNDS=False python test_config.py Scott
# environment variable names are the all-uppercase transformation of the NAME concatenated with the option name and an underscore
MyProgramConfig(number=3, name='Scott', check_bounds=False)

$ env MYPROGRAM_NUMBER=10MYPROGRAM_NAME=Charlie python test_config.py Scott
MyProgramConfig(number=10, name='Scott', check_bounds=True)

如您所见,值是优先的,参数传递给load覆盖环境中的值,而环境反过来覆盖配置文件中的值。

一旦拥有了MyProgramConfig实例,就可以像使用任何数据类一样使用它。

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

推荐PyPI第三方库


热门话题
java中最小长度的字数计算   java事务处理JavaEE 6   java如何进行5分钟刷新并检查txt是否被修改?   java可以同时拖动多个对象   变量长、双精度、字节、字符在Java中的用途是什么?   spring将XMLBean配置转换为java配置   java检测不可靠网络上的TCP丢失   Java:TCP加密、SSL和Netty   在java中,如何使用isAssignableFrom的映射避免多个if-else   在J2EE动态Web项目中找不到java CSS文件   java遍历领域查询到RealmList   安卓阅读网站内容Java   java如何修改/自定义/反编译Opera mini jar文件?   java死锁播放框架如何使用控制器中的参数检查@RestrictedResource?   java在MS Excel中导入xml文件我们如何使用Python或任何其他编程语言自动化此流程?   java如何暂停正在运行的线程并在需要时重新启动同一线程?