473,549 Members | 2,616 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=Non e):
"""
Wrap an editor plug-in into a Narya-Editor instance.
"""
for key in editor_core.__d ict__.keys():
setattr(self, key, getattr(editor_ core, key))

if EditorAPI.isImp lementedBy(self ):
raise BrokenImplement ation(
"Editor plugin '%s' fails implementation test." % editor_core.tit le)
else:
self.__implemen ts__ = EditorAPI
BUT, .isImplementedB y(self) only seems to check to see if
self.__implemen ts__ contains an assertion for the interface.

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

Interface.verif y.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 1449
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(se lf, start, end): pass
def SetSelection(se lf, 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*****@anansi spaceworks.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
1591
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 which will ontain info like start time & end for task & task description. There will be different projects with different employees & approvers...
47
2565
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 Python project too". Really. ROFL.
4
3098
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
1592
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 how difficult it is to get good documentation on developing using these technologies. My question to comp.lang.python is "what is your opinion of...
21
1586
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 proprietary database. I need to know whether zope is good for that job. But even the introduction to zope in the zope book was intimidating. Are there any...
2
1286
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 PYTHONPATH is: /home/zunbeltz/libs/python/:/usr/local/Zope-3.2.0/lib/python:/home/zunbeltz/zope3instance/lib/python:/home/zunbeltz/pycontrol
42
2461
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 that they aren't really necessary in Python. I am wondering what technique I can use in Python to get the same benefits to a program design that I...
6
1813
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 (); .... ... } In python , how to implement interface like the above? is it just
28
1652
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 "typical" oop constructs found in other languages (Java, C# come to mind.) I'm afraid that if I never use them I'll lose them and when I need them for...
0
7521
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7451
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7959
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7473
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6044
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5369
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1944
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.