Skip to content

Instantly share code, notes, and snippets.

@txels
Created February 23, 2013 13:36
Show Gist options
  • Save txels/5019775 to your computer and use it in GitHub Desktop.
Save txels/5019775 to your computer and use it in GitHub Desktop.
This is a simple demo of using Zope interfaces to verify compliance without forcing users to explicitly declare them.
#!/usr/bin/env python
from zope.interface import Interface, classImplements, verify
class IGreet(Interface):
"""
Be friendly and greet people
"""
def hello(whom):
"""
When they come
"""
def bye(whom, until):
"""
When they go
"""
class Impolite(object):
def dontcare(self):
print("I don't care")
class Incomplete(object):
def hello(self, whom):
print('Hi {0}'.format(whom))
class Impersonal(object):
def hello(self):
print('Hi someone')
def bye(self):
print('Bye someone')
class Friendly(object):
def hello(self, whom):
print('Hi {0}'.format(whom))
def bye(self, whom, until):
print('Bye {0}, see you next {1}!'.format(whom, until))
if __name__ == '__main__':
for cls in (Impolite, Incomplete, Impersonal, Friendly):
try:
classImplements(cls, IGreet)
verify.verifyClass(IGreet, cls)
except Exception as e:
print("{0} fails to implement IGreet, reason:\n{1}"
.format(cls.__name__, e))
else:
print("{0} implements IGreet fully!"
.format(cls.__name__))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment