Stephan Richter wrote:

Note that I would strongly suggest using a relationship framework for this sort of use case. A good API is provided by ``schooltool.relationship`` and is heavily used in the SchoolTool code, so you have many examples.

I've spent some time recently studying schooltool.relationship and it is simple and sufficient if you have a small number of relationships. I like the Pythonic nature of its interface and it strongly leverages Zope 3 adaptation. However, if you need to search for either the referer, the reference, or the referent, it goes through relationship.LinkSet.find(), which is implemented as:

def find(self, my_role, target, role, rel_type):
        for link in self:
            if (link.rel_type == rel_type and is target
                and link.my_role == my_role and link.role == role):
                return link
        raise ValueError(my_role, target, role, rel_type)

This is, worst case, linear search time as your relationship set grows. Note also that relationship.relate() is guaranteed linear time (it must check for duplicates) and unrelate() is worst case linear time.

One could, of course, catalog the relationships and re-implement find(), relate() and unrelate() to be much more efficient. One of the beauties that components gives us!

For a similar relationship library that uses a faster indexing method, and supports multiple relationship store backends (ZODB, sleepycat, SQLObject, redland, cwm, 4Thought), supports W3C standard relationship interchange formats (RDF/XML, N3), and comes with a standard SQL -like query language (Sparql), see There is a Zope 3 product called Zemantic at codespeak which defines the necessary ZCML to register rdflib classes with Zope 3.

rdflib could be way too much for you, and schooltool.relationship just right. rdflib development was started before Zope 3 existed, so it is not as component oriented, but this is not really a disadvantage, the vast majority of existing Python libraries are in this same boat. It depends on your use cases. If I never had more than a thousand relations (in your case a few hundred book, a few dozen authors), I'd seriously consider using schooltool.relationship. We've tested rdflib with over 2 million relations on modest hardware.

-Michel (

Zope3-users mailing list

Reply via email to