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第三方库


热门话题
java如何通过编程将图像插入Word文档?   java Maven在Quickstart原型中更改jUnit版本   OS X上的eclipse java版本和路径   java在hibernate中将具有依赖对象的类映射到单独的表   JavaJSF2如何在复合组件子组件完成操作后执行操作?   java无法从列表<节点>强制转换为列表<元素>   java如何将数组链接到方法   检查数组中是否存在一个范围内的所有值的最佳方法是什么?(爪哇)   redis Java:我们应该尽快退出trywithresource块来释放资源吗?   对象不会出现在java swing中的按键上   SQLServerJava。sql。SQLException:客户端尝试签出连接已超时   java如何使用JPA以线程安全的方式保存或更新对象?   java如何在不显示滚动条的情况下消除SWT ScrolledComposite中浪费的空间   如何在Eclipse中从Java编辑器显示scala文档?