提供用于存储密钥/值对的层。

ftw.dictstorage的Python项目详细描述


简介

这个包提供了一个存储密钥/值对的层储藏室 可以通过提供 使用dict存储的上下文。

示例

适配器默认使用非持久dict:

>>> 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

如果使用sqlalchemy,

变更日志

1.2(2012-06-05)

  • 声明zope依赖项。 [乔恩]
  • 更新自述文件。 [乔恩]

1.1(2012-03-28)

  • 从idictstorage接口定义中删除“self”。 这使得使用verifyClass验证实现成为可能。 [乔恩]

1.0(2011-11-17)

  • 为plone 4.1添加了测试构建 [eschmutz]

1.0A4(2011-07-12)

  • 某些类方法的固定签名 [lgraf]

1.0a3

1.0a2

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

推荐PyPI第三方库


热门话题
java在返回类类型时取消选中转换   java使用JSR303和spring的验证器的组合为spring引导端点实现自定义验证逻辑   java如何使用struts2预填充复选框列表   参数数目可变的java重写方法   java这种情况下正确的日期格式是什么?   java使用带有@RequestParam的springboot内部@Controller来提供搜索方法   室内导航和路径发现中的java图遍历和过滤   java在素数面中的另一个bean中获取bean值   java在发送电子邮件时更改文本(字符串)的颜色   在Java中使用Android文本视图时出错   java Resteasy javax如何将contenttype*/*映射到服务器端的application/json?   java如何使用ApacheShiro在会话过期时重新登录?   java是否有正当理由隐藏静态方法?   java如何在javax中覆盖/插入自定义文本。打印文档(pdf、doc、docx…)?