Interfaces (a la PEP 245 and Zope)

Discussion in 'Python' started by Terry Hancock, Aug 1, 2003.

  1. 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/
    Terry Hancock, Aug 1, 2003
    #1
    1. Advertising

  2. Terry Hancock

    Brendan Hahn Guest

    Terry Hancock <> 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
    Brendan Hahn, Aug 1, 2003
    #2
    1. Advertising

  3. 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/
    Terry Hancock, Aug 1, 2003
    #3

  4. > 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
    Raymond Hettinger, Aug 2, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Filip Lyncker
    Replies:
    0
    Views:
    546
    Filip Lyncker
    Apr 14, 2005
  2. Shane Hathaway
    Replies:
    2
    Views:
    325
    Michael Hudson
    Aug 29, 2003
  3. Trent Mick
    Replies:
    0
    Views:
    377
    Trent Mick
    Mar 31, 2005
  4. Roy Smith

    pep-8 vs. external interfaces?

    Roy Smith, Jan 4, 2009, in forum: Python
    Replies:
    3
    Views:
    334
  5. ddoherty03
    Replies:
    4
    Views:
    101
    ddoherty03
    Jul 23, 2009
Loading...

Share This Page