[Zope3-dev] Re: adapter registration - sort of works :-S
Jean-Marc Orliaguet
jmo at ita.chalmers.se
Thu Nov 16 04:20:59 EST 2006
Philipp von Weitershausen wrote:
> 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)
>
OK, but adaptation doesn't provide anything special here since "int" and
"str" are not sufficiently typed, or formatted, so the adapter has to do
all the guessing logic anyway, and there is no fundamental difference
between 123456898374 as date representation and '2006-11-16', or 061116
you might as well write a method that does that:
def to_date(value):
if isinstance(value, int):
...
else if isinstance(value, basestring):
...
this is why usually you don't adapt str or int, unless you're interested
in tracing nasty bugs?
/JM
More information about the Zope3-dev
mailing list