1from abc import ABC, abstractmethod
2
3class AccountingSystem(ABC):
4
5 @abstractmethod
6 def create_purchase_invoice(self, purchase):
7 pass
8
9 @abstractmethod
10 def create_sale_invoice(self, sale):
11 log.debug('Creating sale invoice', sale)
12
1class GizmoAccountingSystem(AccountingSystem):
2
3 def create_purchase_invoice(self, purchase):
4 submit_to_gizmo_purchase_service(purchase)
5
6 def create_sale_invoice(self, sale):
7 super().create_sale_invoice(sale)
8 submit_to_gizmo_sale_service(sale)
9
1from abc import ABCMeta, abstractmethod
2
3class IInterface:
4 __metaclass__ = ABCMeta
5
6 @classmethod
7 def version(self): return "1.0"
8 @abstractmethod
9 def show(self): raise NotImplementedError
10
11class MyServer(IInterface):
12 def show(self):
13 print 'Hello, World 2!'
14
15class MyBadServer(object):
16 def show(self):
17 print 'Damn you, world!'
18
19
20class MyClient(object):
21
22 def __init__(self, server):
23 if not isinstance(server, IInterface): raise Exception('Bad interface')
24 if not IInterface.version() == '1.0': raise Exception('Bad revision')
25
26 self._server = server
27
28
29 def client_show(self):
30 self._server.show()
31
32
33# This call will fail with an exception
34try:
35 x = MyClient(MyBadServer)
36except Exception as exc:
37 print 'Failed as it should!'
38
39# This will pass with glory
40MyClient(MyServer()).client_show()
41