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

2006-11-16 Thread Philipp von Weitershausen

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



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

2006-11-16 Thread Chris Withers

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)


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



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

2006-11-16 Thread Philipp von Weitershausen

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)


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



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

2006-11-16 Thread Jean-Marc Orliaguet

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


___
Zope3-dev mailing list
Zope3-dev@zope.org
Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com



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

2006-11-16 Thread Paul Winkler
On Thu, Nov 16, 2006 at 10:21:09AM +0100, Philipp von Weitershausen wrote:
 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)

That doesn't work, getAdapter() doesn't have a default arg.

I think what you meant is:
 queryAdapter('moo', DateTime, default='xyz')
'xyz'

-- 

Paul Winkler
http://www.slinkp.com
___
Zope3-dev mailing list
Zope3-dev@zope.org
Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com