[Zope3-dev] Re: adapter registration question

Christian Theune ct at gocept.com
Fri Nov 10 09:03:29 EST 2006


Morning,

Rocky Burt wrote:
> On Fri, 2006-10-11 at 08:00 +0000, Chris Withers wrote:
>> This is a toy example, but I need to do something similar and can't seem 
>> to get the registration right...
>>
>> How can I register the str builtin as an adapter from python int objects 
>> to python str objects?
> 
> I've personally found when I run into a situation where it seems like I
> need to adapt on the builtin str class, I should probably be using named
> adapters or named utilities instead.
> 
> I use this technique for example to lookup mime type stuff in one of my
> apps.
> 
> <adapter
>     for="*"
>     name="text/html"
>     factory=".somemodule.SomeClass"
>     provides=".interfaces.ISomeMimeTypeHandlerThingie"
>     />
> 
> from zope import component
> component.getAdapter(randomobj, ISomeMimeTypeHandlerThingie,
> name='text/html')

Humm. Doesn't that totally work around the fact that the CA knows how to
lookup what you need? What does you code do if you adapt from something
else?

The problem you have is to provide a specification for the 'str' interface.

Let's create a marker interface that promises everything that 'str' does:

class IString(zope.interface.Interface):
    """A marker for the 'str' interface."""

Now, you can setup your adapter like this:

<adapter
	for="int"
	provides=".interfaces.IString"
	factory="str"/>

Cheers,
Christian

-- 
gocept gmbh & co. kg - forsterstraße 29 - 06112 halle/saale - germany
www.gocept.com - ct at gocept.com - phone +49 345 122 9889 7 -
fax +49 345 122 9889 1 - zope and plone consulting and development


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 252 bytes
Desc: OpenPGP digital signature
Url : http://mail.zope.org/pipermail/zope3-dev/attachments/20061110/578f3e4d/signature.bin


More information about the Zope3-dev mailing list