Advertisement
davidm1766

Interface-interface.py

May 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.67 KB | None | 0 0
  1. import inspect
  2. class MInterface(type):
  3.  
  4.     #prekryjeme spravu triede
  5.     def __call__(self, *args, **kwargs):
  6.         raise Exception("Z interface sa neda vytvorit objekt")
  7.  
  8.     #prekrytie instanceof
  9.     def __instancecheck__(self, instance):
  10.         for interface in getattr(instance,'__implements__',()):
  11.             if issubclass(interface,self): #ci je potomkom -issubclass
  12.                 return True
  13.         return False
  14.  
  15.  
  16.  
  17. class Interface(metaclass=MInterface):
  18.     pass
  19.  
  20.  
  21. #metatried - potomok type!
  22. class MClass(type):
  23.  
  24.  
  25.     #implements tuple ak je viac
  26.     def __init__(self, name, bases, attrs,implements=None):
  27.         super().__init__(name,bases,attrs)
  28.         if implements is None:
  29.             self.__implements__ = set() #prazdna mnozina
  30.         elif isinstance(implements,tuple):
  31.             self.__implements__ = set(implements)
  32.         else:
  33.             self.__implements__ = {implements} #jednoprvkovy set
  34.  
  35.         for base in bases:
  36.             base_implements = getattr(base,'__implements__',())
  37.             self.__implements__.update(base_implements) #zoberie prvky z base_implements a tie co tam nie su sa vlozia
  38.  
  39.         #**********************************************KONTROLA *******************************************************
  40.         predkovia = list(self.__mro__)
  41.         p = dict(self.__dict__.items())
  42.  
  43.         # ziskam vsetky atributy z predkov
  44.         for predok in predkovia:
  45.             if name == "Class" or predok is Class:
  46.                 break
  47.             else:
  48.                 p.update(predok.__dict__.items())
  49.  
  50.         allatributes = list()  # toto su vsetky atributy kontrolovanej triedy
  51.         print("***********"+self.__name__ +"***************")
  52.         for kluc, hodnota in p.items():
  53.             # odfiltrujem iba tie ktore nie su privatne
  54.             if (not str(kluc).startswith("__")):
  55.                 allatributes.append((kluc, hodnota))
  56.  
  57.  
  58.         for imp in self.__implements__:
  59.             #prechazam po vsetko co je na interface
  60.             for atribut in list((imp.__dict__.items())):
  61.                 nazovAtributu = atribut[0]
  62.                 if str(nazovAtributu).startswith("__"):
  63.                     continue
  64.  
  65.                 atr = [(x, y) for x, y in allatributes if x == nazovAtributu]
  66.                 if atr.__len__() == 0:
  67.                     raise Exception("Nie je implemntovany " + nazovAtributu + " v " + self.__name__)
  68.                 else:
  69.                     #ak to zhodny typ
  70.                     if isinstance((getattr(self,nazovAtributu,None)),type(getattr(imp,nazovAtributu,None))) or \
  71.                         getattr(imp,nazovAtributu,None) is type(getattr(self,nazovAtributu,None)) :
  72.  
  73.                         if not isinstance(getattr(self,nazovAtributu),property) :
  74.                             if not inspect.getargspec(getattr(imp,nazovAtributu)) == inspect.getargspec(getattr(self,nazovAtributu)):
  75.                                 raise Exception(nazovAtributu + " nemá tie iste parametere ako interface(musi byt zhodny aj nazov)")
  76.  
  77.  
  78.                     else:
  79.                         raise Exception("Typ nie je zhodny "+ nazovAtributu+" v "+self.__name__)
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.     #v type je new ina ako inde...
  87.     def __new__(cls, name, bases, attrs, implements=None):
  88.         return super().__new__(cls, name, bases, attrs)
  89.         # tu sa nieco robi
  90.         pass
  91.  
  92.     # def __call__(self, *args, **kwargs):
  93.     #     instancia = self.__new__(*args,**kwargs) #vrati novy neinicializovany objekt, bez zavolania konstruktora! prekryt sa da
  94.     #     if instancia is not None:
  95.     #         instancia.__init__(*args,**kwargs)
  96.     #     pass
  97.  
  98.  
  99.  
  100. class Class(metaclass=MClass):
  101.     pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement