Re: [Zope3-Users] Checking if an interface is provided by an object or could be adapted to

2006-04-30 Thread Bernd Dorn

i would use

return IFoo(context, False) is not False or False






On 01.05.2006, at 00:22, Martin Aspeli wrote:


Hi,

I'm probably doing something very wrong, but ...

I have a general interface that can either by directly provided by  
an object or class (typically with implements() on the class), but  
could also be provided via an adapter. I have a few places where I  
just need to check for this (it's not really a marker interface,  
but I need to know whether other code later on would be able to use  
the interface or not).


In my tests, I tried to use queryAdapter(), but that didn't return  
anything when the interface was implemented directly by the class.  
Similarly, Interface.providedBy() returns (expectedly) False if the  
interface could only be obtained via an adapter. Currently I do:


  try:
adapted = IFoo(context)
return True
except (ComponentLookupError, TypeError):
return False

but that's just silly... Is there a pattern for this?

Martin

--
"You can just adapt yourself out of it..." // Archipelago sprint  
26/04/2006


___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


Re: [Zope3-Users] Checking if an interface is provided by an object or could be adapted to

2006-04-30 Thread Tom Dossis

Martin Aspeli wrote:

Hi,

I'm probably doing something very wrong, but ...

I have a general interface that can either by directly provided by an  
object or class (typically with implements() on the class), but could 
also  be provided via an adapter. I have a few places where I just need 
to check  for this (it's not really a marker interface, but I need to 
know whether  other code later on would be able to use the interface or 
not).


In my tests, I tried to use queryAdapter(), but that didn't return  
anything when the interface was implemented directly by the class.  
Similarly, Interface.providedBy() returns (expectedly) False if the  
interface could only be obtained via an adapter. Currently I do:


  try:
adapted = IFoo(context)
return True
except (ComponentLookupError, TypeError):
return False

but that's just silly... Is there a pattern for this?


adapted = IFoo(conext, None):
if adapted is not None:
  ...

___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users