Amit Sethi wrote:
<snip ... the top-posted portion>
. On Mon, Jun 29, 2009 at 5:01 PM, Dave Angel <da...@ieee.org> wrote:
>
> Amit Sethi ?wrote:
>
>> Well I want to implement plug-in like mechanism for an application . I want
>> to define some minimum functions that any body writing a plugin has to
>> implement. For that i thought an interface would be best because in a
>> scenario where the function is not implemented some kind of error would
>> occur. I would love to hear if you think their is a better way to achieve
>> this
>
> In Java, deriving a class from such an interface, but neglecting to implement
those methods will cause a compile error (if I recall correctly, it's been several
years). ?In Python, the error will happen at run time. ?But an existing runtime
error will occur even without such an interface, so it wouldn't seem you gain
much. ?In Python, the interface does two things:
>
> 1) it's a comment, a common place to look for certain behavior.
> 2) it's potentially a source for an IDE to provide tool-tips or code
completion
> 3) it can generate a different error, which is perhaps more useful to the
developer unsure of how the method is spelled or used. ?This way he/she knows
whether to fix the caller or the implementation.
>
> #3 seems valid to me.
>
>
>
> However, for the particular use-case, you might want to stretch a bit further. ?Since you've got
one "user" (your code), and many "providers" (the plug-in writers) ?perhaps you
could arrange that when the plugin is first encountered, you validate that it has all the required
methods and data members. ?Not by calling them, but by scanning the object for their existence.
>
It'd sure be easier if you had a sample of what you're already expecting
to do. Then I could use similar names, and refer to things much more
easily. It also might matter which version/implementation of Python
you're using. I'm using CPython 2.6 on XP.
I'll guess that you're using __import__ to import a module based on its
name or location. Further, I'll guess that the class name the user is
supposed to implement is called Doit, and that if you had used
interfaces, they would derive from your interface. So you have code
something like:
#interfacemodule.py
class MyInterface(object):
def __init__(self, modulename):
for method in ["doSomething", "doSomethingElse"]:
m = getattr(self, method, None)
if not m:
print "Missing attribute %s in module %s, class Doit" %
(method, modulename)
raise NotImplementedError
else:
print "Found :", method, m
and they have code like:
import interfacemodule
class Doit(MyInterface):
def doSomething(self, line):
print "line=", line
def doSomethingElse42(self, line): #method name deliberately
spelled wrong, to trigger the error
print "line=", line
After doing the __import__, you instantiate the new class. Something
like (untested):
x = __import__(filename)
newobject = x.Doit(filename)
Instantiating that object will test the attributes, making sure
everything in your list exists. Once you're sure they do, add the
newobject to your list of plugins.
There's lots of other choices, but I suspect most will be trickier than
this. You may need them, depending on how much you trust your plugin
writers. For example, this doesn't help if they neglect to derive from
MyInterface. That's no big deal, since you could use a regular function
instead of __init__ method, and run it on the object.
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor