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
_______________________________________________
Zope3-dev mailing list
Zope3-dev@zope.org
Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com

Reply via email to