Dolmen关系
项目描述
dolmen.relations 是在 zc.relation 之上的一层薄层,允许简单地实现对象之间的独立关系。
入门
为了展示软件包的功能,我们首先设置一个合理的环境
>>> from zope import component >>> from zope.container.btree import BTreeContainer >>> sm = component.getGlobalSiteManager() >>> herd = getRootFolder()['herd'] = BTreeContainer()
关系目录
dolmen.relations 提供了一个名为 RelationCatalog 的组件,负责注册关系和查找它们
>>> from dolmen.relations import RelationCatalog, ICatalog >>> sm.registerUtility(RelationCatalog(), ICatalog)
关系容器
为了存储关系并触发所需的事件,dolmen.relations 提供了一个 btree 容器
>>> from dolmen.relations import RelationsContainer >>> relations = herd['_relations'] = RelationsContainer()
内容
现在,我们需要一些内容来开始。测试模块定义了一个Mammoth持久对象,我们将在这里使用它
>>> from dolmen.relations.tests import Mammoth >>> manfred = herd['manfred'] = Mammoth() >>> gunther = herd['gunther'] = Mammoth()
为了确保我们的对象将被持久化并将被赋予一个整数id,我们提交
>>> import transaction >>> transaction.commit()
关系
由 dolmen.relations 提出的关系为“从A到B”类型。它们允许您将源对象与目标对象关联起来。为了测试目的,我们将创建两个作为源和目标的 Mammoth 对象。
>>> from dolmen.relations import values, any >>> from zope.intid.interfaces import IIntIds >>> ids = component.getUtility(IIntIds) >>> rcatalog = component.getUtility(ICatalog) >>> gunther_id = ids.getId(gunther) >>> manfred_id = ids.getId(manfred)
简单关系
最简单的关系类型是 RelationValue。这种关系使用源ID和目标ID创建。
>>> relations["simple"] = values.RelationValue(gunther_id, manfred_id)
您可以通过提供目标ID和/或源ID来查询关系。
>>> found = list(rcatalog.findRelations({'target_id': manfred_id})) >>> found [<dolmen.relations.values.RelationValue object at ...>]
关系具有用于解决源或目标的属性。
>>> relation = found.pop() >>> relation <dolmen.relations.values.RelationValue object at ...> >>> relation.source <Mammoth gunther> >>> relation.target <Mammoth manfred>
标记关系
第二种关系类型是 TaggedRelationValue。它允许我们在源-目标对中添加一个标签列表,该列表为Unicode字符串列表。
>>> relations["tagged"] = values.TaggedRelationValue( ... gunther_id, manfred_id, tags=[u'grok', u'dolmen'])
关系仍然可以通过基本查询检索。
>>> found = list(rcatalog.findRelations({'target_id': manfred_id})) >>> found [<dolmen.relations.values.RelationValue object at ...>, <dolmen.relations.values.TaggedRelationValue object at ...>]
现在,也可以使用标签值进行查询。
>>> found = list(rcatalog.findRelations({'tag': any('grok')})) >>> found [<dolmen.relations.values.TaggedRelationValue object at ...>] >>> found = list(rcatalog.findRelations({'tag': any('drupal')})) >>> found []
状态关系
第三种关系类型是 StatefulRelationValue。它向源-目标对添加状态信息,状态信息为Unicode字符串。
>>> relations["stateful"] = values.StatefulRelationValue( ... gunther_id, manfred_id, state=u"private")
关系仍然可以通过基本查询检索。
>>> found = list(rcatalog.findRelations({'target_id': manfred_id})) >>> found [<dolmen.relations.values.RelationValue object at ...>, <dolmen.relations.values.TaggedRelationValue object at ...>, <dolmen.relations.values.StatefulRelationValue object at ...>]
现在也可以使用状态字符串进行查询。
>>> found = list(rcatalog.findRelations({'state': any('private')})) >>> found [<dolmen.relations.values.StatefulRelationValue object at ...>] >>> found = list(rcatalog.findRelations({'state': any('public')})) >>> found []
事件
每当删除对象时,作为源或目标使用它的关系也会被删除。
>>> del herd['manfred'] >>> print list(herd['_relations'].keys()) [] >>> found = list(rcatalog.findRelations({'target_id': manfred_id})) >>> found []
变更日志
0.5 (2011-11-08)
在ID被删除时捕获intids的错误。在这种情况下返回None。
0.4 (2010-03-15)
当关系由于组件被删除而删除时添加事件。
修复在关系容器中删除不存在的关系的组件[reference_id]时的错误(您应该只得到一个KeyError)。
0.3 (2010-03-10)
修复包上的zip-safe标志。
通过使用register而不是getId IntIds方法来修复潜在的非Yet错误。
修复没有关系目录可用时的事件。
0.2 (2009-12-26)
ZTK兼容性导入更改。
0.1 (2009-10-20)
初始版本
项目详情
dolmen.relations-0.5.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 0db01c59cd9806e143c883b38701dcd4084154f5b353df9ea23a1092103c8d3c |
|
MD5 | 60ef4dd740b7d1bf2c74894eb3891eea |
|
BLAKE2b-256 | 0e0ff07b18917fe8dc1ae08a5ce0e832ad7177113dc4d9797053fe34a35c1c15 |