跳转到主要内容

提供存储键/值对的一层。

项目描述

简介

此包提供了一种存储键值对的层。可以通过提供在哪个上下文中使用字典存储的 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.24.35.1 兼容。

变更日志

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 (12.5 kB 查看散列)

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面