This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
Project Description

dolmen.relations is a thin layer above zc.relation, allowing a simple and straightforward implementation of standalone relationships between objects.

Getting started

In order to demonstrate the package’s features, we first set up a sane environment:

>>> from zope import component
>>> from zope.container.btree import BTreeContainer

>>> sm = component.getGlobalSiteManager()
>>> herd = getRootFolder()['herd'] = BTreeContainer()

Relations catalog

dolmen.relations provides a component called RelationCatalog that is in charge of registering the relations and finding them:

>>> from dolmen.relations import RelationCatalog, ICatalog
>>> sm.registerUtility(RelationCatalog(), ICatalog)

Relations container

To store the relations and trigger the needed events, dolmen.relations provides a btree container:

>>> from dolmen.relations import RelationsContainer
>>> relations = herd['_relations'] = RelationsContainer()

Content

Now, we need some content to get started. The tests module defines a Mammoth persistent object that we are going to use here:

>>> from dolmen.relations.tests import Mammoth

>>> manfred = herd['manfred'] = Mammoth()
>>> gunther = herd['gunther'] = Mammoth()

To be sure that our objects will be persisted and will be granted an int id, we commit:

>>> import transaction
>>> transaction.commit()

Relations

The relations proposed by dolmen.relations are of the “A to B” type. They allow you to link a source object with a target object. For tests purposes, we are going to create two Mammoth objects that are going to be used as source and target:

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

Simple relation

The first and simpliest relation type is the RelationValue. This relation is created with a source id and target id:

>>> relations["simple"] = values.RelationValue(gunther_id, manfred_id)

You can query the relations by giving the target and/or source id:

>>> found = list(rcatalog.findRelations({'target_id': manfred_id}))
>>> found
[<dolmen.relations.values.RelationValue object at ...>]

The relation has attributes dedicated to resolving the source or target:

>>> relation = found.pop()
>>> relation
<dolmen.relations.values.RelationValue object at ...>
>>> relation.source
<Mammoth gunther>
>>> relation.target
<Mammoth manfred>

Tagged relation

The second type of relation is the TaggedRelationValue. It allows us to add to the a source-target couple, a list of tags as a list of unicode strings:

>>> relations["tagged"] = values.TaggedRelationValue(
...           gunther_id, manfred_id, tags=[u'grok', u'dolmen'])

The relation can still be retrieved with a basic query:

>>> found = list(rcatalog.findRelations({'target_id': manfred_id}))
>>> found
[<dolmen.relations.values.RelationValue object at ...>, <dolmen.relations.values.TaggedRelationValue object at ...>]

It can also, now, be queried using a tag value:

>>> found = list(rcatalog.findRelations({'tag': any('grok')}))
>>> found
[<dolmen.relations.values.TaggedRelationValue object at ...>]

>>> found = list(rcatalog.findRelations({'tag': any('drupal')}))
>>> found
[]

Stateful relation

The third type of relation is the StatefulRelationValue. It adds, to the source-target couple, state information as a unicode string:

>>> relations["stateful"] = values.StatefulRelationValue(
...           gunther_id, manfred_id, state=u"private")

The relation can still be retrieved with a basic query:

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

It can also, now, be queried using the state string:

>>> found = list(rcatalog.findRelations({'state': any('private')}))
>>> found
[<dolmen.relations.values.StatefulRelationValue object at ...>]

>>> found = list(rcatalog.findRelations({'state': any('public')}))
>>> found
[]

Events

Whenever an object is deleted, the relations using it as source or target are deleted also:

>>> del herd['manfred']
>>> print list(herd['_relations'].keys())
[]
>>> found = list(rcatalog.findRelations({'target_id': manfred_id}))
>>> found
[]

Changelog

0.5 (2011-11-08)

  • Catch an error from the intids when the ID have been removed. Return None in this case.

0.4 (2010-03-15)

  • Add event when relation are deleted because a component of them has been deleted.
  • Fix an error when you del container[reference_id] in a relation container of an inexisting relation (you should only get a KeyError).

0.3 (2010-03-10)

  • Correct zip-safe flag on package.
  • Fix potential NotYet errors by using register instead of getId IntIds method.
  • Fix event when you don’t have a relation catalog available.

0.2 (2009-12-26)

  • ZTK compatibility imports change.

0.1 (2009-10-20)

  • Initial release
Release History

Release History

0.5

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.4

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.3

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.2

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.1

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

TODO: Brief introduction on what you do with files - including link to relevant help section.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
dolmen.relations-0.5.tar.gz (7.9 kB) Copy SHA256 Checksum SHA256 Source Nov 8, 2011

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting