Welcome, guest | Sign In | My Account | Store | Cart

Tiny "zope interface" dependent recipe for contract verfification.

Python, 31 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import zope.interface.verify

def contractVerifier(implementer):
   for iface in list(implementer.__implemented__):
      implementer_name = implementer.__name__
      try:
         if zope.interface.verify.verifyClass(iface, implementer):
            print "OK: %s correctly implements %s" % (implementer_name, iface.__name__)
       except Exception, err:
           print "Error detected with %s's implementation: %s" % (implementer_name, err)

class IFoo(zope.interface.Interface):
   def foo(arg1): pass
   def bar(): pass # self is not reqd as Interfaces document how obj is used 

class Foo(object):
   zope.interface.implements(IFoo)
   def foo(self): pass

class Foo2(object):
   zope.interface.implements(IFoo)
   def foo(self, arg1): pass

class Foo3(object):
   zope.interface.implements(IFoo)
   def foo(self, arg1): pass
   def bar(self): pass

contractVerifier(Foo)
contractVerifier(Foo2)
contractVerifier(Foo3)

Zope Interfaces is a nice package especially when you are designing something moderate to large size. Documentation page is a bit outdated and looks dirty though. Here are the links to documentation/articles on zope interfaces. http://wiki.zope.org/Interfaces/InterfaceUserDocumentation (main) http://twistedmatrix.com/projects/core/documentation/howto/components.html And if zope/twisted is installed file:///usr/lib/python/site-packages/zope/interface/README.txt

Created by Shekhar Tiwatne on Tue, 14 Aug 2007 (PSF)
Python recipes (4591)
Shekhar Tiwatne's recipes (6)

Required Modules

  • (none specified)

Other Information and Tasks