提供存储键/值对的一层。
项目描述
简介
此包提供了一种存储键值对的层。可以通过提供在哪个上下文中使用字典存储的 IConfig 适配器来动态配置存储。
示例
适配器默认使用非持久性字典
>>> from ftw.dictstorage.interfaces import IDictStorage
>>> context = layer['example_context']
>>> print context
<ftw.dictstorage.testing.ExampleContext object at ...>
>>> storage = IDictStorage(context)
>>> storage['key'] = 'value'
>>> print storage['key']
value
>>> print storage.storage
{'key': 'value'}
>>> print IDictStorage(context).storage
{}
要配置自定义存储,实现自己的 IConfig,它使用自定义 IDictStorage
>>> from ftw.dictstorage.interfaces import IConfig
>>> from ftw.dictstorage.base import DictStorage
>>> from zope.component import provideAdapter, adapts
>>> from zope.interface import Interface, alsoProvides, implements
>>> context = layer['example_context']
>>> class IMyContext(Interface):
... pass
>>> alsoProvides(context, IMyContext)
>>> class ContextStorageConfig(object):
... implements(IConfig)
... adapts(IMyContext)
...
... def __init__(self, context):
... self.context = context
...
... def get_storage(self):
... if not hasattr(self.context, '_dictstorage'):
... self.context._dictstorage = {}
... return self.context._dictstorage
>>> provideAdapter(ContextStorageConfig)
>>> class ContextDictStorage(DictStorage):
... implements(IDictStorage)
... adapts(IMyContext, IConfig)
...
... def __init__(self, context, config):
... self.context = context
... self.config = config
... self._storage = config.get_storage()
...
... @property
... def storage(self):
... return self._storage
...
>>> provideAdapter(ContextDictStorage)
>>> storage = IDictStorage(context)
>>> storage['foo'] = 'bar'
>>> print storage['foo']
bar
>>> print context._dictstorage
{'foo': 'bar'}
>>> print IDictStorage(context)['foo']
bar
兼容性
与 Plone 4.2、4.3、5.1 兼容。
链接
版权
此包由 4teamwork 版权所有。
ftw.dictstorage 在 GNU 通用公共许可证下许可,版本 2。
变更日志
1.3.0 (2019-11-11)
移除对 Plone 4.0 和 4.1 的支持。[jone]
添加对 Plone 5.1 的支持。[Nachtalb]
1.2 (2012-06-05)
声明 zope 依赖。[jone]
更新 README。[jone]
1.1 (2012-03-28)
从 IDictStorage 接口定义中删除“self”。这使得可以使用 verifyClass 验证实现。[jone]
1.0 (2011-11-17)
添加了针对 plone 4.1 的测试构建。[eschmutz]
1.0a4 (2011-07-12)
修复了一些类方法的签名。[lgraf]
1.0a3
1.0a2
项目详情
关闭
ftw.dictstorage-1.3.0.tar.gz 的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | 68001b84383b5e71f64d3d1a4157a19b7d67bb40051cc411862d074d4e7149f3 |
|
MD5 | 316cae5e7ecc2efc59c5c5043ea9cee1 |
|
BLAKE2b-256 | aa11a800593ca0d15f3eda5ff915a1524314e271605f9a0ecbd6c18a18dd2556 |