Interfaces (a la PEP 245 and Zope)

T

Terry Hancock

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
 
B

Brendan Hahn

Terry Hancock said:
Brendan said:
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.
 
T

Terry Hancock

Brendan said:
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.
 
R

Raymond Hettinger

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/Cookbook/Python/Recipe/204349



Raymond Hettinger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top