Re: [Zope3-dev] Re: calling interface returns object, calling queryAdapter returns None?
On Wed, Jan 24, 2007 at 08:44:31PM -0500, Tres Seaver wrote: Note that for regularity, I assert that the calling the IFoo interface should allow the follwing use cases, based on the asserition that utilities are zero-order adapters, just as views are binary order adapters. There is a very important difference. When you do adaptation, an adapter factory gets called and you get a new object. (There are exceptions -- the adapter factory may do caching, or the object you're adapting may already implement the interface.) When you do utility lookups, you always get the same object. No factory ever gets invoked. Marius Gedminas -- Special bonus feature: absolutely nowhere in RetchMail's code is there an arbitrary 3-second sleep(). Wow! What other mail retriever can say that? (Hint: not fetchmail.) -- http://open.nit.ca/wiki/index.php?page=RetchMail signature.asc Description: Digital signature ___ Zope3-dev mailing list Zope3-dev@zope.org Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com
[Zope3-dev] Re: calling interface returns object, calling queryAdapter returns None?
All the following are great, especially the (SF) ones ;-) Tres Seaver wrote: from zope.interface import Interface, Attribute from zope.interface import implements, directlyProvides from zope.component import provideUtility, provideAdapter, adapts class IFoo(Interface): ... name = Attribute(u'Name') class FooUtil: ... implements(IFoo) ... def __init__(self, name): ... self.name = name fooUtil = FooUtil('default') provideUtility(fooUtil) fooUtilNamed = FooUtil('named') provideUtility(fooUtilNamed, name='named') class FooAdapter: ... implements(IFoo) ... adapts(None) ... name = 'default' ... def __init__(self, context,*args): ... self.context = context class FooAdapterNamed(FooAdapter): ... name = 'named' provideAdapter(FooAdapter) provideAdapter(FooAdapterNamed, name='named') Now the usage. Things which *don't* work today are marked '(SF)'. Interfaces called with *no* context argument return utilities, not adapters (SF):: u_default = IFoo() # return the default utility u_default.__class__.__name__, u_default.name ('FooUtil', 'default') Named utilities can also be looked up (SF):: u_named = IFoo(name='named') u_named.__class__.__name__, u_named.name ('FooUtil', 'named') Default adapter lookup works as expected:: context = object() a_default = IFoo(context) a_default.__class__.__name__, a_default.name ('FooAdapter', 'default') But we can also do named adapter lookup (SF):: a_named = IFoo(context, name='named') a_named.__class__.__name__, a_named.name ('FooAdapterNamed', 'named') I'll note that none of these appear to do contextual lookup. Would we want a spelling like this which worked with contextual lookup? One thing I think I mentioned before would be the ability to use calling the interface to get a multi-adapter: provideAdapter(FooAdapter,adapts=(None,None)) a_default = IFoo(objects=(context,context)) a_default.__class__.__name__, a_default.name ('FooAdapter', 'default') One other thing, where is the code that gets called when you do ISomething(..etc...)? I had a look in interface.py and couldn't find anything :-S I'd be interested in knocking the above set of extras up on a branch and seeing what people think... 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: calling interface returns object, calling queryAdapter returns None?
On Jan 24, 2007, at 8:44 PM, Tres Seaver wrote: -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Chris Withers wrote: Fred Drake wrote: On 1/24/07, Chris Withers [EMAIL PROTECTED] wrote: queryAdapter, for me, is starting with the supplied object, get me something that implements the supplied interface and return None if no such object can be obtained. o = IFoo(ob, None) if os is not None: Ah, now that's what I was looking for, thanks :-) Note that for regularity, I assert that the calling the IFoo interface should allow the follwing use cases, based on the asserition that utilities are zero-order adapters, just as views are binary order adapters. I disagree with this assertion for the reason that Marius stated, which is that adapters are factories and utilities are not. I certainly understand why you would make that assertion. Jim -- Jim Fulton mailto:[EMAIL PROTECTED]Python Powered! CTO (540) 361-1714 http://www.python.org Zope Corporationhttp://www.zope.com http://www.zope.org ___ 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: calling interface returns object, calling queryAdapter returns None?
Jim Fulton wrote at 2007-1-25 09:11 -0500: ... I disagree with this assertion for the reason that Marius stated, which is that adapters are factories and utilities are not. I Should I be really interested in this implementation detail? All I care of, is that the returned object implements the required interface and is related to the object to be adapted (if any). How the adapter is found/created is of no interest to me. -- Dieter ___ Zope3-dev mailing list Zope3-dev@zope.org Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com
[Zope3-dev] Re: calling interface returns object, calling queryAdapter returns None?
Chris Withers wrote: Hot on the heels of my multi-adapter problem: http://mail.zope.org/pipermail/zope3-dev/2007-January/021600.html ...which I'm still waiting for guidance on, I now find that queryAdapter and calling an interface behave unexpectedly differently in the case where an object directly implements an interface: from zope.interface import Interface,implements class ITest(Interface): pass ... class Test: ... implements(ITest) ... t = Test() ITest(t) __main__.Test instance at 0x01C6E300 from zope.component import queryAdapter queryAdapter(t,ITest) How come calling the interface returns t but queryAdapter returns None? Should I log this and my previous multi-adapter example as bugs in the collector? cheers, Chris From the Interface.__call__ docstring: If an object already implements the interface, then it will be returned queryAdapter is looking in the adapter registry. You have not registered any adapters. So this looks like the expected behaviour to me. Laurence ___ 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: calling interface returns object, calling queryAdapter returns None?
Laurence Rowe wrote: From the Interface.__call__ docstring: If an object already implements the interface, then it will be returned queryAdapter is looking in the adapter registry. You have not registered any adapters. So this looks like the expected behaviour to me. Documented maybe, expected no. queryAdapter, for me, is starting with the supplied object, get me something that implements the supplied interface and return None if no such object can be obtained. If there's another function which does this, fine, if not, then I maintain the current behaviour is not correct... 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: calling interface returns object, calling queryAdapter returns None?
On Jan 24, 2007, at 5:37 PM, Chris Withers wrote: Laurence Rowe wrote: From the Interface.__call__ docstring: If an object already implements the interface, then it will be returned queryAdapter is looking in the adapter registry. You have not registered any adapters. So this looks like the expected behaviour to me. Documented maybe, expected no. queryAdapter, for me, is starting with the supplied object, get me something that implements the supplied interface and return None if no such object can be obtained. If there's another function which does this, fine, if not, then I maintain the current behaviour is not correct... Chris, documented behavior is not incorrect just because you expect otherwise. Jim -- Jim Fulton mailto:[EMAIL PROTECTED]Python Powered! CTO (540) 361-1714 http://www.python.org Zope Corporationhttp://www.zope.com http://www.zope.org ___ 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: calling interface returns object, calling queryAdapter returns None?
On 1/24/07, Chris Withers [EMAIL PROTECTED] wrote: queryAdapter, for me, is starting with the supplied object, get me something that implements the supplied interface and return None if no such object can be obtained. o = IFoo(ob, None) if os is not None: ... If there's another function which does this, fine, if not, then I maintain the current behaviour is not correct... Er, wrong. Like Jim said, if it's documented... -Fred -- Fred L. Drake, Jr.fdrake at gmail.com Every sin is the result of a collaboration. --Lucius Annaeus Seneca ___ 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: calling interface returns object, calling queryAdapter returns None?
Jim Fulton wrote: If there's another function which does this, fine, if not, then I maintain the current behaviour is not correct... Chris, documented behavior is not incorrect just because you expect otherwise. Indeed, sorry, that's not what I meant, although I can see it came across like that. Apologies... Les try this again: what's the canonical way to find out if an object either implements a given interface or can be adapted to it? cheers, Chris PS: still looking for confirmation as to whether what I demonstrate in http://mail.zope.org/pipermail/zope3-dev/2007-January/021600.html is a bug or if I'm missing something? -- 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: calling interface returns object, calling queryAdapter returns None?
Fred Drake wrote: On 1/24/07, Chris Withers [EMAIL PROTECTED] wrote: queryAdapter, for me, is starting with the supplied object, get me something that implements the supplied interface and return None if no such object can be obtained. o = IFoo(ob, None) if os is not None: Ah, now that's what I was looking for, thanks :-) 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
[Zope3-dev] Re: calling interface returns object, calling queryAdapter returns None?
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Chris Withers wrote: Fred Drake wrote: On 1/24/07, Chris Withers [EMAIL PROTECTED] wrote: queryAdapter, for me, is starting with the supplied object, get me something that implements the supplied interface and return None if no such object can be obtained. o = IFoo(ob, None) if os is not None: Ah, now that's what I was looking for, thanks :-) Note that for regularity, I assert that the calling the IFoo interface should allow the follwing use cases, based on the asserition that utilities are zero-order adapters, just as views are binary order adapters. First the setup:: from zope.interface import Interface, Attribute from zope.interface import implements, directlyProvides from zope.component import provideUtility, provideAdapter, adapts class IFoo(Interface): ... name = Attribute(u'Name') class FooUtil: ... implements(IFoo) ... def __init__(self, name): ... self.name = name fooUtil = FooUtil('default') provideUtility(fooUtil) fooUtilNamed = FooUtil('named') provideUtility(fooUtilNamed, name='named') class FooAdapter: ... implements(IFoo) ... adapts(None) ... name = 'default' ... def __init__(self, context): ... self.context = context class FooAdapterNamed(FooAdapter): ... name = 'named' provideAdapter(FooAdapter) provideAdapter(FooAdapterNamed, name='named') Now the usage. Things which *don't* work today are marked '(SF)'. Interfaces called with *no* context argument return utilities, not adapters (SF):: u_default = IFoo() # return the default utility u_default.__class__.__name__, u_default.name ('FooUtil', 'default') Named utilities can also be looked up (SF):: u_named = IFoo(name='named') u_named.__class__.__name__, u_named.name ('FooUtil', 'named') Default adapter lookup works as expected:: context = object() a_default = IFoo(context) a_default.__class__.__name__, a_default.name ('FooAdapter', 'default') But we can also do named adapter lookup (SF):: a_named = IFoo(context, name='named') a_named.__class__.__name__, a_named.name ('FooAdapterNamed', 'named') - -- === Tres Seaver +1 540-429-0999 [EMAIL PROTECTED] Palladion Software Excellence by Designhttp://palladion.com -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.2.2 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFFuAt/+gerLs4ltQ4RAmTSAJ0SYhaKb3fPrpeIm1odC02LrbjZuQCfXK/0 NMERLbcjZsBK77OeCWUACYQ= =wI8F -END PGP SIGNATURE- ___ Zope3-dev mailing list Zope3-dev@zope.org Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com