Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

interface, abstract python?

from abc import ABCMeta, abstractmethod

class IInterface:
    __metaclass__ = ABCMeta

    @classmethod
    def version(self): return "1.0"
    @abstractmethod
    def show(self): raise NotImplementedError

class MyServer(IInterface):
    def show(self):
        print 'Hello, World 2!'

class MyBadServer(object):
    def show(self):
        print 'Damn you, world!'


class MyClient(object):

    def __init__(self, server):
        if not isinstance(server, IInterface): raise Exception('Bad interface')
        if not IInterface.version() == '1.0': raise Exception('Bad revision')

        self._server = server


    def client_show(self):
        self._server.show()


# This call will fail with an exception
try:
    x = MyClient(MyBadServer)
except Exception as exc:
    print 'Failed as it should!'

# This will pass with glory
MyClient(MyServer()).client_show()
Comment

interface, abstract python?

from abc import ABC, abstractmethod

class AccountingSystem(ABC):

    @abstractmethod
    def create_purchase_invoice(self, purchase):
        pass

    @abstractmethod
    def create_sale_invoice(self, sale):
        log.debug('Creating sale invoice', sale)
Comment

interface, abstract python?

class GizmoAccountingSystem(AccountingSystem):

    def create_purchase_invoice(self, purchase):
        submit_to_gizmo_purchase_service(purchase)

    def create_sale_invoice(self, sale):
        super().create_sale_invoice(sale)
        submit_to_gizmo_sale_service(sale)
Comment

PREVIOUS NEXT
Code Example
Python :: ImportError: No module named _bootlocale 
Python :: download latest chromedriver python code 
Python :: from django.urls import re_path 
Python :: permutation of a string in python 
Python :: condition in python 
Python :: split by backslash python 
Python :: function to remove punctuation in python 
Python :: what are args and kwargs in python 
Python :: replace nan 
Python :: seaborn modificar o tamanho dos graficos 
Python :: best algorithm for classification 
Python :: messages in django 
Python :: kdeplot python 
Python :: argparse positional arguments 
Python :: how to create a button using tkinter 
Python :: python is prime 
Python :: matplotlib pie chart order 
Python :: python qr decomposition 
Python :: string equals python 
Python :: clear variable jupyter notebook 
Python :: session in django 
Python :: Django delete a session value 
Python :: .counter python 
Python :: django cheat sheet pdf 
Python :: radix sort strings python 
Python :: create an empty array numpy 
Python :: lamda in pyton 
Python :: python3 tuple 
Python :: python import 
Python :: python type hint list of possible values 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =