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

Philipp von Weitershausen philipp at weitershausen.de
Thu Nov 16 04:21:09 EST 2006


Chris Withers wrote:
>>>  >>> 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:

Don't guess, just look at the docs that are there.

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

It does -- in the interfaces. Like it or not, Zope 3's API *is* well 
documented, if you look in the right places: the interfaces. That's what 
they're there for.

> 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

Adapter factories can return None to indicate that adaption has failed.

> and that's a "special value" meaning "couldn't adapt", right?

Yes.

> How would I _really_ return None from an adapter?

You don't. You call the adaption with a default value (e.g. None) to 
cover for the "coudln't adapt" case:

   >>> getAdapter('moo', DateTime, None)

Philipp


More information about the Zope3-dev mailing list