[Interface-dev] Chain adaption?

Nitro nitro at dr-code.org
Fri Jul 11 07:21:45 EDT 2008


Hello,

I am trying to evaluate zope.interface for a project. What I want to do is
this: Let's assume I have something like

class ICircle(zi.Interface):
      radius = zi.Attribute("radius")

class IEllipse(zi.Interface):
      size = zi.Attribute("size")

class Bucket(object):
      litres = 10

Now I define an adapter CircleToEllipse which can adapt ICircle to
IEllipse objects (by doing size = (radius, radius)). Next I define another
adapter BucketToCircle which adapts a Bucket to an ICircle, by doing
something like radius = litres. Finally, I want to be able to do something
like

IEllipse( Bucket() )

This doesn't work. The effect I'd like is zope.interface to chain the
adaptions like Bucket -> BucketToCircle -> CircleToEllipse . I can achieve
this manually by doing

myBucketAsEllipse = registry.queryAdapter( ICircle( Bucket() ), IEllipse )

But this is not really automatic. Related to this is that I am unable to
do:

registry.register( [Bucket], ICircle, '', BucketToCircle )

Instead I need to use adapter_hooks. This again seems to make chaining
more of a trial-and-error process, because I can't tell zope.interface to
map Bucket to ICircle in a "rigid" way (the adapter hook can return
different adapters depending on the object state for example).

Attached is my working test file. It shows how everything is setup.

1) Does zope.interface support chaining?
2) Why does the register( [Bucket] ... ) call not work. I know Bucket is
not an interface, but it can be adopted to one.
3) Am I terribly misusing zope.interface?

-Matthias

P.S.: The wiki seems a bit outdated.
http://wiki.zope.org/Interfaces/FrontPage and
http://www.zope.org/Products/ZopeInterface/ should probably point to
http://pypi.python.org/pypi/zope.interface .
-------------- next part --------------
A non-text attachment was scrubbed...
Name: adaptertest.py
Type: application/octet-stream
Size: 1515 bytes
Desc: not available
Url : http://mail.zope.org/pipermail/interface-dev/attachments/20080711/c9a0049b/adaptertest.obj


More information about the Interface-dev mailing list