Re: [Zope3-dev] Re: most specific interface?

2005-09-16 Thread Fred Drake
On 9/16/05, Florent Guillaume [EMAIL PROTECTED] wrote:
 (Or use a convenience method to do that, I'm not sure if alsoProvides() was
 ever implemented.)

zope.interface.alsoProvides() works like a champ.


  -Fred

-- 
Fred L. Drake, Jr.fdrake at gmail.com
Zope Corporation
___
Zope3-dev mailing list
Zope3-dev@zope.org
Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com



[Zope3-dev] Re: most specific interface?

2005-09-16 Thread Jean-Marc Orliaguet
Philipp von Weitershausen wrote:

Florent Guillaume wrote:
  

Philipp von Weitershausen wrote:



   from zope.app.content.interfaces import IContentType
   from zope.app.file.interfaces import IFile
   from zope.interface import directlyProvides
   directlyProvides(IFile, IContentType)
  

Watch out! directlyProvides is evil, it *replaces* the interfaces
provided by something. Here, if IFile implemented something else, it
would be lost.

You should always use:

  directlyProvides(ob, ISomething, directlyProvidedBy(ob))

(Or use a convenience method to do that, I'm not sure if alsoProvides()
was ever implemented.)



Yes, alsoProvides() is available in Zope 3.1. So,

   alsoProvides(ob, ISomething)

is the shorter spelling of Florent's line above.

Philipp


  


does alsoProvides() check for interfaces already listed?

apparently not:

interface/declarations.py:

def alsoProvides(object, *interfaces):
   ...
directlyProvides(object, directlyProvidedBy(object), *interfaces)

what happens if you write:

 alsoProvides(ob, ISomething)
 alsoProvides(ob, ISomething)

will ISomething be provided twice? this could be a memory leak in that case.

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



[Zope3-dev] Re: most specific interface?

2005-09-16 Thread Philipp von Weitershausen
Jean-Marc Orliaguet wrote:
 does alsoProvides() check for interfaces already listed?

No, but directlyProvides does. Issuing the same directlyProvides(...)
call twice has the same effect as issuing it once.

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: most specific interface?

2005-09-14 Thread Philipp von Weitershausen
Jean-Marc Orliaguet wrote:
 Philipp von Weitershausen wrote:
 
 
Jean-Marc Orliaguet wrote:
 


is the order of the list of interfaces implemented by an object subject
to internal changes?

I have identified the need for such a pattern:

   iface = object.interface()

with:

class someObject(object):
   implements(IMainInterface, ISecondaryInterface, ...)
   def interface():
   Return the most specific interface implemented by the element.
   return list(providedBy(self))[0]

to be able in that case to get access to the first interface implemented
by an object, as a sort of main object type.
   


We usually do this differently. If some interfaces are special types
(e.g. IFile is a content type) then we have this interface provide
ISpecialType (e.g. IFile provides IContentType). ISpecialType is an
interface extending IInterface.

Then, no matter where in the list of provided interfaces the type is, it
can be fetch with queryType. Let's take the IFile example from above and
set it up as a content type:

  from zope.app.content.interfaces import IContentType
  from zope.app.file.interfaces import IFile
  from zope.interface import directlyProvides
  directlyProvides(IFile, IContentType)
...

Philipp
 

 
 
 I see, this is clever, and it simplifies the code.
 
 the idea is that you define as many categories as you need: IMetaType,
 ISomeCategory, IWidgetType ... and you create relations between
 interfaces with:
 
 directlyProvides(IFile, IContentType)

Exactly.

 as if you had a relation tool, then every object that implements IFile
 (no matter in what position) will have the IFile content type?

Yes.

 But where do you put the 'directlyProvides' statement?

Like Jim said, anywhere in Python code. The fact that IFile directly
provides IContentType isn't bound to any class statement.

The typical case, though, is to use ZCML, as Jim also mentioned:

  interface
  interface=.interfaces.IFile
  type=zope.app.content.interfaces.IContentType
  /

See the beginning of chapter 5 (page 55) of my book for a further
explanation and an example from the interpreter shell.

Philipp


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: most specific interface?

2005-09-13 Thread Philipp von Weitershausen
Jean-Marc Orliaguet wrote:
 is the order of the list of interfaces implemented by an object subject
 to internal changes?
 
 I have identified the need for such a pattern:
 
 iface = object.interface()
 
 with:
 
 class someObject(object):
 implements(IMainInterface, ISecondaryInterface, ...)
 def interface():
 Return the most specific interface implemented by the element.
 return list(providedBy(self))[0]
 
 to be able in that case to get access to the first interface implemented
 by an object, as a sort of main object type.

We usually do this differently. If some interfaces are special types
(e.g. IFile is a content type) then we have this interface provide
ISpecialType (e.g. IFile provides IContentType). ISpecialType is an
interface extending IInterface.

Then, no matter where in the list of provided interfaces the type is, it
can be fetch with queryType. Let's take the IFile example from above and
set it up as a content type:

   from zope.app.content.interfaces import IContentType
   from zope.app.file.interfaces import IFile
   from zope.interface import directlyProvides
   directlyProvides(IFile, IContentType)

Now let's make a File object. We see that its first interface isn't
IFile but IFileContent:

   from zope.app.file.file import File
   f = File()
   list(providedBy(f))
  [InterfaceClass zope.app.publication.interfaces.IFileContent,
   InterfaceClass zope.app.file.interfaces.IFile,
   InterfaceClass persistent.interfaces.IPersistent]

Yet, when we query the content type, we get IFile as expected.

   from zope.app.interface import queryType
   queryType(f, IContentType)
  InterfaceClass zope.app.file.interfaces.IFile

 the zope/app/component/registration.txt documentation mentions:
 
 
 We can now write another `IComponentRegistration` implementation that knows
 about the interface; in fact, it will pick the most specific one of the
 component:
 
from zope.interface import providedBy
class SomethingRegistration(Registration):
   ...
   ... def interface(self):
   ... return list(providedBy(self._component))[0]
   ... interface = property(interface)
 
 
 
 but I haven't seen it used anywhere else. Is it safe to use this?

I think it's not quite safe to rely on it the way you seem to want (as a
main object type). It is correct that the first interface *is* the
most specific one, e.g. when it comes to adaption:

   from zope.component import *
   from zope.interface import *

   class IA(Interface): pass
  ...
   class IB(Interface): pass
  ...
   class IC(Interface): pass
  ...

   class Adapter(object):
  ... adapts(IA)
  ... implements(IC)
  ... def __init__(self, context): pass
  ...
   class Bedapter(object):
  ... adapts(IB)
  ... implements(IC)
  ... def __init__(self, context): pass
  ...

   provideAdapter(Adapter)
   provideAdapter(Bedapter)

   class Klass(object):
  ... implements(IA, IB)
  ...
   IC(Klass())
  __main__.Adapter object at 0x1424e30

   class Klass(object):
  ... implements(IB, IA)
  ...
   IC(Klass())
  __main__.Bedapter object at 0x1424e10

Philipp
___
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: most specific interface?

2005-09-13 Thread Jean-Marc Orliaguet
Janko Hauser wrote:


 Am 13.09.2005 um 12:43 schrieb Jean-Marc Orliaguet:

 But where do you put the 'directlyProvides' statement? in the class :


 Can't this be put in the interface definition module for 
 IContentType? You mark other interfaces with the interface IContentType.

 __Janko


I don't know.. but the content type is just one type of categorization.
I already have 3 or 4 categories that apply to a same object apart from
zope's IContentType.

/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: most specific interface?

2005-09-13 Thread Jim Fulton

Jean-Marc Orliaguet wrote:
...

the idea is that you define as many categories as you need: IMetaType,
ISomeCategory, IWidgetType ... and you create relations between
interfaces with:

directlyProvides(IFile, IContentType)


Exactly.


as if you had a relation tool,


There are all sorts of ways of expressing relationships in Python
without central databases.

 then every object that implements IFile

(no matter in what position) will have the IFile content type?


Right.  Note that a content type is a specific kind of type.

If an object provides interface I, then the object is said to
be of type I.  If I is a content type, then the object is said
to have that content type.

As a matter of policy, we expect an object to have a single content
type and have a helper function for getting the content type by
finding an object's most specifiuc type that is a content type.


But where do you put the 'directlyProvides' statement?


There are two options.  You put it in some python module, such
as the module that defines IFile:

  class IFile(...):
 ...

  directlyProvides(IFile, IContentType)

or, since this is often a configuration choice, you put in in
zcml:


  interface
  interface=.interfaces.IFile
  type=zope.app.content.interfaces.IContentType
  /

Jim

--
Jim Fulton   mailto:[EMAIL PROTECTED]   Python Powered!
CTO  (540) 361-1714http://www.python.org
Zope Corporation http://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