[Zope3-dev] adapter registration - sort of works :-S
Chris Withers
chris at simplistix.co.uk
Thu Nov 16 03:57:20 EST 2006
Hi All,
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. Although it does suggest that 'adapt'
should be provided as an acronym for 'getAdapter'.
Anyway, now all excited, I tried this:
>>> def to_date(value):
... try:
... return DateTime(value)
... except:
... return None
...
>>> provideAdapter(to_date,(str,int),DateTime)
>>> getAdapter('2006/11/16',DateTime)
Traceback ...
...
zope.component.interfaces.ComponentLookupError:
('2006/11/16', <class DateTime.DateTime.DateTime at 0x00E73600>, u'')
wtf?
Chris
--
Simplistix - Content Management, Zope & Python Consulting
- http://www.simplistix.co.uk
More information about the Zope3-dev
mailing list