[Interface-dev] Chain adaption?
Alexander Smith
alex at zope.com
Fri Jul 11 14:06:54 EDT 2008
I do agree with you that it might be interesting if the component
architecture could infer how to compose adapters, but I imagine the
graph of possible adapter combinations could get quite large and
complex. I think for the sake of both efficiency and
understandability, it's probably for the best to do adapter "chaining"
manually.
To answer your second question, I think your call to register an
adapter is a little off. It should be more like this:
>>> zope.component.provideAdapter(BucketToCircle, ICircle, name='')
or even:
>>> zope.component.provideAdapter(BucketToCircle, ICircle)
Then to adapt a bucket instance:
>>> bucket = Bucket()
>>> circle_adapter = ICircle(bucket)
And to your third question, I think you're off to a solid start and
asking some good questions.
Alexander J Smith
Software Engineer
Zope Corporation
On Jul 11, 2008, at 7:21 AM, Nitro wrote:
> 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
> .<adaptertest.py>_______________________________________________
> Interface-dev mailing list
> Interface-dev at zope.org
> http://mail.zope.org/mailman/listinfo/interface-dev
More information about the Interface-dev
mailing list