Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Interfaces (a la PEP 245 and Zope)

Reply
Thread Tools

Interfaces (a la PEP 245 and Zope)

 
 
Terry Hancock
Guest
Posts: n/a
 
      08-01-2003
So, how did the chips fall on implementing interfaces in Python?

I've been using the Interface module that is included in Zope, and
although, I've found it useful, there are some cases I don't seem to
find a good solution for. To illustrate, I'll just describe my current
problem:

I want to define an object which implements a variety of methods
and attributes required by Zope, and use it to wrap a simpler object
which might be written by a plugin author. The rest of my code
assumes that this plugin will conform to a given interface, so I want
to check that it does (to make life easier for the plugin developer).

I'd *like* to do something like this:

class EditorAPI(Base):
"""
Mandatory interface for Editor plugins.
"""
pass
# Defines stuff that an editor must have to satisfy the rest
# of the program.

class Editor(Folder):
"""
Zope Narya-Editor object is base for all editors.
"""
meta_type = "Narya-Editor"
__implements__ = 'EditorAPI'

def __init__(self, id, editor_core=None):
"""
Wrap an editor plug-in into a Narya-Editor instance.
"""
for key in editor_core.__dict__.keys():
setattr(self, key, getattr(editor_core, key))

if EditorAPI.isImplementedBy(self):
raise BrokenImplementation(
"Editor plugin '%s' fails implementation test." % editor_core.title)
else:
self.__implements__ = EditorAPI


BUT, .isImplementedBy(self) only seems to check to see if
self.__implements__ contains an assertion for the interface.

What I really want is a thorough check, along the lines of
what

Interface.verify.verify_class_implementation(iface , klass)

does. But that's no good, because this object wasn't created by
a simple class statement -- we're creating it dynamically in the
__init__() method.

Now of course, I could pick through the Interface module and
write a check that resembles the verification that it does, but it seems to
me that there ought to be a simpler solution (and if I were going to
do that, I should probably really be improving the Interface module
rather than putting the code in my package).

So, am I missing some existing way to do this?

TIA,
Terry

--
Terry Hancock
Anansi Spaceworks http://www.AnansiSpaceworks.com/

 
Reply With Quote
 
 
 
 
Brendan Hahn
Guest
Posts: n/a
 
      08-01-2003
Terry Hancock <(E-Mail Removed)> wrote:
>Brendan Hahn wrote:
>> How about an Interface metaclass that replaces any class methods with an
>> appropriate exception-raiser.

[...]
>Well Zope's (or should I say Jim Fulton's?) Interface module
>defines an object with a number of useful documentation and
>validation behaviors.


I haven't checked out the Zope stuff...that just popped into my head as a
slightly easier way to implement Heiko's suggestion. You could use it as a
way to generate base classes that enforce the required-implementation
aspect of interfaces (maybe call it 'Abstract') and also inherit from other
sources to provide validation and such.

--
brendan DOT hahn AT hp DOT com
 
Reply With Quote
 
 
 
 
Terry Hancock
Guest
Posts: n/a
 
      08-01-2003
Brendan Hahn wrote:
> How about an Interface metaclass that replaces any class methods with an
> appropriate exception-raiser. It would save a little typing, e.g.
>
> class IEditor():
> __metaclass__ = Interface
> def GetSelection(self, start, end): pass
> def SetSelection(self, start, end, data): pass
>
> ...and so on.


Well Zope's (or should I say Jim Fulton's?) Interface module
defines an object with a number of useful documentation and
validation behaviors.

--
Terry Hancock
Anansi Spaceworks http://www.AnansiSpaceworks.com/

 
Reply With Quote
 
Raymond Hettinger
Guest
Posts: n/a
 
      08-02-2003

> Now of course, I could pick through the Interface module and
> write a check that resembles the verification that it does, but it seems to
> me that there ought to be a simpler solution (and if I were going to
> do that, I should probably really be improving the Interface module
> rather than putting the code in my package).
>
> So, am I missing some existing way to do this?


I posted a simple interface checker in the Python cookbook.
Perhaps, it will meet your needs:

http://aspn.activestate.com/ASPN/Coo.../Recipe/204349



Raymond Hettinger


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for an ASN1 tool to compile h.245 from ITU Filip Lyncker C++ 0 04-14-2005 11:25 AM
ANN: ActivePython 2.4.1 build 245 is available Trent Mick Python 0 03-31-2005 06:10 PM
H.245 port range in Radvision stack yossibh@gmail.com VOIP 0 03-11-2005 10:16 AM
HP Model 245 Compact Photo Printer- Any Good? frank1492 Digital Photography 1 12-09-2004 09:50 PM
h.245 Terminal capability exchange danish VOIP 1 08-05-2003 02:35 PM



Advertisments