473,406 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Interfaces (a la PEP 245 and Zope)

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/

Jul 18 '05 #1
3 1447
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/

Jul 18 '05 #2
Terry Hancock <ha*****@anansispaceworks.com> 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
Jul 18 '05 #3
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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: john | last post by:
Hi I am developing a timesheet system. It will have 3 levels of access 1)Administrator 2)Approver -- Will approve or reject the timesheet filled by employees 3)Employees -- Fill in the timesheet...
47
by: Will Stuyvesant | last post by:
Hello all, So Zope still lives, yay. Well, I like that they use Python. <rant> What amazed me is they write that they "added types to the variables and hope that it will be added to the...
4
by: Brian Kazian | last post by:
I want to insure that all subclasses implement a certain method, but could not find anything that would do this for me. Is there anyway in Python to implement this check? Thanks!
4
by: Avery Warren | last post by:
I am investigating converting a wiki site to plone. I am having a lot of difficulty finding good documentation programmatically accessing the ZODB API. A lot of the user feedback is centered on...
21
by: godwin | last post by:
Hi all, I wanna thank Martin for helping out with my ignorance concerning execution of stored procedure with python. Now i have decided to write a web app that googles into my companies...
2
by: zunbeltz | last post by:
Hi, I've installed zope3.2 froma tarball. It has been installed in /usrlocal/Zope-3-2. I am developing a package under ~/zope3instance/lib/python/ (which is my instance directory). My...
42
by: redefined.horizons | last post by:
I'm coming from a Java background, so please don't stone me... I see that Python is missing "interfaces". The concept of an interface is a key to good programming design in Java, but I've read...
6
by: s99999999s2003 | last post by:
hi i come from a non OO environment. now i am learning about classes. can i ask, in JAva, there are things like interface. eg public interface someinterface { public somemethod (); .... ... }...
28
by: jmDesktop | last post by:
Studying OOP and noticed that Python does not have Interfaces. Is that correct? Is my schooling for nought on these OOP concepts if I use Python. Am I losing something if I don't use the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.