[Zope3-dev] Re: adapter registration - sort of works :-S

Philipp von Weitershausen philipp at weitershausen.de
Thu Nov 16 04:05:55 EST 2006


Chris Withers wrote:
> Chris Withers wrote:
>> Jean-Marc Orliaguet wrote:
>>> once you have that utility / adapter you should be able to call it like:
>>>
>>>   converter = getAdapterFor(123, type=IToStringConverter)
>>>   strResult = converter.convert(123)
>>
>> Not quite, what I'm looking to do is more along the lines of:
>>
>> mystr = getAdapter(123,str)
> 
> OK, less talk, more do... and when I stop worrying about it, it all gets 
> very easy:
> 
>  >>> from zope.component import provideAdapter
>  >>> provideAdapter(str,(int,),str)
>  >>> from zope.component import getAdapter
>  >>> getAdapter(123,str)
> '123'
> 
> Yay! That's _exactly_ what I want.

And that's exactly what I meant -- and wrote about half way up the 
thread. :)

> Anyway, now all excited, I tried this:
> 
>  >>> def to_date(value):
> ...   try:
> ...     return DateTime(value)
> ...   except:
> ...     return None
> ...
>  >>> provideAdapter(to_date,(str,int),DateTime)

This registers a multi adapter for (a_string, an_integer), like views 
are multi-adapters for (context, request). You want to say:

   >>> provideAdapter(to_date, (str,), DateTime)
   >>> provideAdapter(to_date, (int,), DateTime)

>  >>> getAdapter('2006/11/16',DateTime)
> Traceback ...
> ...
> zope.component.interfaces.ComponentLookupError:
> ('2006/11/16', <class DateTime.DateTime.DateTime at 0x00E73600>, u'')
> 


-- 
http://worldcookery.com -- Professional Zope documentation and training


More information about the Zope3-dev mailing list