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

Chris Withers chris at simplistix.co.uk
Thu Nov 16 04:14:17 EST 2006


Philipp von Weitershausen wrote:
>>  >>> 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. :)

Yeah, but the words were wrong, so I didn't grok it.
A simple example as above would probably have done it ;-)

>>  >>> 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)

Ahhhh... okay, I had to guess from this:

 >>> help(provideAdapter)
Help on function provideAdapter in module zope.component:

provideAdapter(factory, adapts=None, provides=None, name='')

 >>> provideAdapter(str,int,str)
Traceback...
...
TypeError: iteration over non-sequence

I guessed the wrong use of the sequence :-(

Shame provideAdapter doesn't have a docstring ;-)

Okay, so now I can do what I want:

 >>> getAdapter('2006/11/16',DateTime)
DateTime('2006/11/16')

...except:

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

I'm guessing this is 'cos my adapter is returning None and that's a 
"special value" meaning "couldn't adapt", right?

How would I _really_ return None from an adapter?

cheers,

Chris

-- 
Simplistix - Content Management, Zope & Python Consulting
            - http://www.simplistix.co.uk


More information about the Zope3-dev mailing list