configi是一个简单的配置数据包装器。它提供了一个到多个后端的一致接口,目前通过任意http json加载、redis k/v存储和s3json文件加载。

configi的Python项目详细描述


configi是一个简单的配置数据包装器。它提供了一个到多个后端的一致接口,目前通过任意http json加载、redis k/v存储和s3json文件加载。

使用configi

代码本身是有文档记录的,但这里有几个方法:

s3配置源

fromconfigiimportDynamicConfig,S3ConfigSourceimportboto# Boto not included, but if you want to use S3ConfigSource# you probably have it already. I hope.s3=boto.connect_s3()bucket=s3.get_bucket("my-config-repository")config_file_key=bucket.get_key("my-config-file-key")source=S3ConfigSource(config_file_key)config=DynamicConfig(source,expiry=(60*4),# Every four hours reloadquiet_mode=True,# Errors will `print` instead of throwing Exceptions.namespace_dicts=True,# Turn dictionaries into dot-accessible namespaces.defaults={'some_key':'a_value'}# Defaults dict is used to return values, if a key is unset remotely.)printconfig.foo>Noneconfig.foo='bar'printconfig.foo>barprintconfig.some_key>a_valueconfig.some_key='banana'printconfig.some_key>banana

redis配置源

fromconfigiimportDynamicConfig,RedisConfigSourceimportredis# Redis not included, but if you want to use# RedisConfigSource, then you'll need it.r=redis.StrictRedis()# Get a Redis connection. Yours will have more parameters.source=RedisConfigSource(r,prefix='MY_AWESOME_CONFIGI_CONFIG_SETUP:')# Default prefix is 'CONFIGI:'config=DynamicConfig(source,expiry=(60*4),# Every four hours reloadquiet_mode=True,# Errors will `print` instead of throwing Exceptions.namespace_dicts=True,# Turn dictionaries into dot-accessible namespaces.defaults={'some_key':'a_value'}# Defaults dict is used to return values, if a key is unset remotely.)printconfig.foo>Noneconfig.foo='bar'printconfig.foo>barprintconfig.some_key# Even though defaults were passed, any remotely-set value overrides.>orangutan

json文件配置源

fromconfigiimportDynamicConfig,JSONConfigSourcesource=JSONConfigSource("https://some-url.com/my-config-file.json")config=DynamicConfig(source,expiry=(60*4),# Every four hours reloadquiet_mode=True,# Errors will `print` instead of throwing Exceptions.namespace_dicts=True,# Turn dictionaries into dot-accessible namespaces.defaults={'some_key':'a_value'}# Defaults dict is used to return values, if a key is unset remotely.)printconfig.foo>Noneconfig.foo='bar'>DynamicConfigError:Couldnotsetkeyfoo

注意事项

一些配置源更适合于只读配置。也就是说,基于json文件的任意配置是严格只读的。基于s3的配置是读/写的,但对于高写场景来说不是很理想。redis,如果一个redis商店既安全又对你可用,可能是你最好的选择。

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

推荐PyPI第三方库


热门话题
JavaSpring事件发射器停止在新连接上向以前的客户端发送事件   javascript如何在Ionic 4中向选项卡添加模式?   java Hibernate hbm2ddl。自动更新不会删除mysql中的列   java如何使用instanceof根据子类类型对子类执行不同的操作?   java在JPanel中动态添加JLabel(重新验证无效)   java我的计算机上可以有两个版本的JDK吗?   spring在Java中管理每个表单提交的版本   java获取装饰器对象的所有类型:包装对象的类型和包装对象的类型   多线程Java区分可运行线程类型   javajavax。网ssl。SSLexException:填充长度无效   java JSP将单引号和双引号显示为符号   java当使用TestNG DataProvider时,有没有办法从同一个Excel工作表中读取和写入参数?   java不同的枚举哈希代码生成?   java ASM AdviceAdapter onMethodEnter打印所有参数   JavaStruts2(版本2.3.28)只接受注册的区域设置   excel如何使用Java中的Apache POI库对数据透视表数据进行排序   如果没有Kotlin库,是否可以将Kotlin翻译成Java?   安卓中用于JSON数据的java Junit