SV: Python Productivity over C++

David Bolen db3l at fitlinxx.com
Wed Jun 14 19:11:08 EDT 2000


hzhu at knowledgetrack.com (Huaiyu Zhu) writes:

> I'm not sure if this is feasible in general, but even a half-hearted job
> would still be helpful.

How about just writing a function to verify methods against a
reference class.  The reference class could either be an actual class
that you are mimicing the interface for, or a dummy class that is just
defined with the appropriate methods.

For a crude quick example, perhaps something like:

    class MyInterface:

	def method1():
	    pass
	def method2():
	    pass
	def method3():
	    pass


    class MyImplementingInterface:

	def method1():
	    # Actual method 1 implementation
	    pass
	def method3():
	    # Actual method 3 implementation
	    pass


    def verify_class (Reference, Instance):

	import types

	print "Checking %s for conformity with interface %s" % \
	      (Instance.__name__,Reference.__name__)

	for curmethod in Reference.__dict__.keys():
	    if type(Reference.__dict__[curmethod]) == types.FunctionType:
		if (not Instance.__dict__.has_key(curmethod) or
		    type(Instance.__dict__[curmethod]) != types.FunctionType):
		    print "Method %s not supported" % curmethod


    if __name__ == "__main__":

	verify_class(MyInterface,MyImplementingInterface)


when run, yields

Checking MyImplementingInterface for conformity with interface MyInterface
Method method2 not supported


I'm sure there's lots more that could be done - the dummy interface
class might provide special members to control optional functions, or
an interface could just be specified as a list, or you could also try
to check the signature of the function, etc...


--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list