Re: [Zope3-dev] Re: wildcard adapter

2007-01-19 Thread Chris Withers

Marius Gedminas wrote:

Now when you try to adapt anything to ITest, zope.component will call
your ``adapter`` function and then check the return value.  A return
value of None means the adapter is not available, and results in a
TypeError you see here:


Yes, apologies, both you and Philipp are correct, I was trying to show a 
simple version of a problem and oversimplified.


Here's what I really meant:

 from zope.component import provideAdapter
 from zope.interface import Interface
 from zope.component import getMultiAdapter
 class ITest(Interface): pass
...
 def adapter(*args): return args
...

 provideAdapter(adapter,adapts=(None,),provides=ITest)
 ITest(1)
(1,)

Yay, as expected...

 getMultiAdapter((1,),ITest)
(1,)

Good, still works...

 provideAdapter(adapter,adapts=(None,None),provides=ITest)
 getMultiAdapter((1,1),ITest)
Traceback (most recent call last):
...
zope.component.interfaces.ComponentLookupError:
 ((1, 1), InterfaceClass __main__.ITest, u'')

Oh dear, what have I done wrong here?

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



[Zope3-dev] Re: wildcard adapter

2007-01-19 Thread Martijn Faassen

Marius Gedminas wrote:

On Thu, Jan 18, 2007 at 08:55:17AM +, Chris Withers wrote:

Marius Gedminas wrote:
BTW passing interfaces to provideAdapter/adapts is out of fashion. 
This stuff is getting almost perl-ish in it's there's many ways to do 
it, pick the style you like nature..


There's a slight difference, I think.  In the Zope 3 world, there's
usually one style that is considered to be the best way of doing stuff.
Only it varies with time and with individual Zope 3 developers. ;-)


Well, I'm sure with Perl the best way of doing stuff also varies with 
time and with individual Perl developers. :)


I agree the you should now not write this bit in ZCML but in the class 
itself is frustrating, as the other way isn't forbidden and there is 
endless example code out there which does it that way.


Grok lets you write everything in the class and do away with the ZCML 
entirely. I believe this to be a superior pattern as it's quick to 
write, simple to understand and doesn't require switching context to 
another file to figure out what's going on. It also gives you error 
messages if you do something Grok cannot figure out.


Regards,

Martijn

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



[Zope3-dev] Re: wildcard adapter

2007-01-19 Thread Tres Seaver
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Chris Withers wrote:
 Marius Gedminas wrote:
 Now when you try to adapt anything to ITest, zope.component will call
 your ``adapter`` function and then check the return value.  A return
 value of None means the adapter is not available, and results in a
 TypeError you see here:
 
 Yes, apologies, both you and Philipp are correct, I was trying to show a 
 simple version of a problem and oversimplified.
 
 Here's what I really meant:
 
   from zope.component import provideAdapter
   from zope.interface import Interface
   from zope.component import getMultiAdapter
   class ITest(Interface): pass
 ...
   def adapter(*args): return args
 ...
 
   provideAdapter(adapter,adapts=(None,),provides=ITest)
   ITest(1)
 (1,)
 
 Yay, as expected...
 
   getMultiAdapter((1,),ITest)
 (1,)
 
 Good, still works...
 
   provideAdapter(adapter,adapts=(None,None),provides=ITest)
   getMultiAdapter((1,1),ITest)
 Traceback (most recent call last):
 ...
 zope.component.interfaces.ComponentLookupError:
   ((1, 1), InterfaceClass __main__.ITest, u'')
 
 Oh dear, what have I done wrong here?

The order of your adapter registration is *one*, but you are trying to
look it up against *two* objects (like a view).


Tres.
- --
===
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

iD8DBQFFsSp++gerLs4ltQ4RApjSAKCYZwOp8b7LlUYEdk5E54bMSStY+ACgzdRY
8ZKbrbRcpyGUhel5YXXeuw4=
=nAls
-END PGP 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: wildcard adapter

2007-01-19 Thread Chris Withers

Tres Seaver wrote:

  provideAdapter(adapter,adapts=(None,None),provides=ITest)
  getMultiAdapter((1,1),ITest)
Traceback (most recent call last):
...
zope.component.interfaces.ComponentLookupError:
  ((1, 1), InterfaceClass __main__.ITest, u'')

Oh dear, what have I done wrong here?


The order of your adapter registration is *one*, 


Does the following not register for an order of two?

provideAdapter(adapter,adapts=(None,None),provides=ITest)

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: wildcard adapter

2007-01-18 Thread Chris Withers

Philipp von Weitershausen wrote:
If it is, then which of the following should I use to register a 
generic adapter for any single object to an interface:


provideAdapter(...,adapts=(None,),...)


This one.


Given that you can register adapters for any class, whether or not it 
implements any interface, this doesn't work as it should:


 from zope.component import provideAdapter
 def adapter(*args): pass
...
 from zope.interface import Interface
 class ITest(Interface): pass
...
 provideAdapter(adapter,adapts=(None,),provides=ITest)

 ITest(1)
Traceback (most recent call last):
...
TypeError: ('Could not adapt', 1, InterfaceClass __main__.ITest)

 class test: pass
...
 ITest(test())
...
TypeError: ('Could not adapt', __main__.test instance at ..., 
InterfaceClass __main__.ITest)


I have no idea what a generic adapter is, but the zope.component API 
docs (== interfaces) are certainly complete in this sense. There are 
also doctests.


Where can I find the docs and interfaces you mention above? I looked 
through both zope.interface and zope.component and obviously missed them :-(


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: wildcard adapter

2007-01-18 Thread Marius Gedminas
On Thu, Jan 18, 2007 at 08:53:36AM +, Chris Withers wrote:
 Philipp von Weitershausen wrote:
 If it is, then which of the following should I use to register a 
 generic adapter for any single object to an interface:
 
 provideAdapter(...,adapts=(None,),...)
 
 This one.
 
 Given that you can register adapters for any class, whether or not it 
 implements any interface, this doesn't work as it should:
 
  from zope.component import provideAdapter
  def adapter(*args): pass
 ...
  from zope.interface import Interface
  class ITest(Interface): pass
 ...
  provideAdapter(adapter,adapts=(None,),provides=ITest)

It's a bit subtle.  The function you pass to provideAdapter is not the
adapter, it is an adapter factory.  Consider the difference between
classes and objects.  One is a factory for the other.  Adapters are
usually objects, and but you register classes with provideAdapter.

Now when you try to adapt anything to ITest, zope.component will call
your ``adapter`` function and then check the return value.  A return
value of None means the adapter is not available, and results in a
TypeError you see here:

  ITest(1)
 Traceback (most recent call last):
 ...
 TypeError: ('Could not adapt', 1, InterfaceClass __main__.ITest)

This is useful when you want to have an adapter that is available
conditionally.

Insert a print statement in your adapter function, and you will see that
it does get called:

   from zope.component import provideAdapter
   def myAdapter(*args):
  ... print Hi
  ...
   from zope.interface import Interface
   class ITest(Interface): pass
  ...
   provideAdapter(myAdapter,adapts=(None,),provides=ITest)
   ITest(1)
  Hi
  Traceback (most recent call last):
File stdin, line 1, in ?
  TypeError: ('Could not adapt', 1, InterfaceClass __main__.ITest)

 I have no idea what a generic adapter is, but the zope.component API 
 docs (== interfaces) are certainly complete in this sense. There are 
 also doctests.
 
 Where can I find the docs and interfaces you mention above? I looked 
 through both zope.interface and zope.component and obviously missed them :-(

src/zope/component/README.txt would be my guess.  I do not remember
what's inside, I learnt adapters by osmosis and occasional studying of
the source code.

Marius Gedminas
-- 
Change is inevitable, except from a vending machine.


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



Re: [Zope3-dev] Re: wildcard adapter

2007-01-18 Thread Philipp von Weitershausen

On 18 Jan 2007, at 09:53 , Chris Withers wrote:

Philipp von Weitershausen wrote:
If it is, then which of the following should I use to register a  
generic adapter for any single object to an interface:


provideAdapter(...,adapts=(None,),...)

This one.


Given that you can register adapters for any class, whether or not  
it implements any interface, this doesn't work as it should:


 from zope.component import provideAdapter
 def adapter(*args): pass
...


This is your problem. The 'adapter' function is a factory so it's  
supposed to actually return the value of the adaption. If ITest(1)  
returns None, then the component architecture thinks adaption has  
failed. This is a well-known and intended behaviour.



 from zope.interface import Interface
 class ITest(Interface): pass
...
 provideAdapter(adapter,adapts=(None,),provides=ITest)

 ITest(1)
Traceback (most recent call last):
...
TypeError: ('Could not adapt', 1, InterfaceClass __main__.ITest)

 class test: pass
...
 ITest(test())
...
TypeError: ('Could not adapt', __main__.test instance at ...,  
InterfaceClass __main__.ITest)


I have no idea what a generic adapter is, but the zope.component  
API docs (== interfaces) are certainly complete in this sense.  
There are also doctests.


Where can I find the docs and interfaces you mention above? I  
looked through both zope.interface and zope.component and obviously  
missed them :-(




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



[Zope3-dev] Re: wildcard adapter

2007-01-17 Thread Philipp von Weitershausen

Chris Withers wrote:

Hi All,

I was wondering if someone could give me a definitive explanation of 
what the following means:


adapter
for=* ISomething *
...

...means?

Is the following equivalent:

provideAdapter(...,adapts=(None,ISomething,None),...)

?


Yes.

If it is, then which of the following should I use to register a generic 
adapter for any single object to an interface:


provideAdapter(...,adapts=(None,),...)


This one.


provideAdapter(...,adapts=None,...)


This will give you most likely an error.

I'm not having a lot of joy registering generic adapters so I thought 
I'd come and check my understanding here ;-)


I have no idea what a generic adapter is, but the zope.component API 
docs (== interfaces) are certainly complete in this sense. There are 
also doctests.



--
http://worldcookery.com -- Professional Zope documentation and training
2nd edition of Web Component Development with Zope 3 is now shipping!

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



[Zope3-dev] Re: wildcard adapter

2007-01-17 Thread Philipp von Weitershausen

Martin Aspeli wrote:



Chris Withers wrote:

Hi All,

I was wondering if someone could give me a definitive explanation of 
what the following means:


adapter
 for=* ISomething *
 ...

...means?

Is the following equivalent:

provideAdapter(...,adapts=(None,ISomething,None),...)



No, but this is:

provideAdapter(...,adapts=(Interface, ISomething, Interface),...)


Actually, ZCML translates a * to None. And as of Zope 3.3, adapting 
None or Interface is equivalent.



--
http://worldcookery.com -- Professional Zope documentation and training
2nd edition of Web Component Development with Zope 3 is now shipping!

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