Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import inspect
- class MInterface(type):
- #prekryjeme spravu triede
- def __call__(self, *args, **kwargs):
- raise Exception("Z interface sa neda vytvorit objekt")
- #prekrytie instanceof
- def __instancecheck__(self, instance):
- for interface in getattr(instance,'__implements__',()):
- if issubclass(interface,self): #ci je potomkom -issubclass
- return True
- return False
- class Interface(metaclass=MInterface):
- pass
- #metatried - potomok type!
- class MClass(type):
- #implements tuple ak je viac
- def __init__(self, name, bases, attrs,implements=None):
- super().__init__(name,bases,attrs)
- if implements is None:
- self.__implements__ = set() #prazdna mnozina
- elif isinstance(implements,tuple):
- self.__implements__ = set(implements)
- else:
- self.__implements__ = {implements} #jednoprvkovy set
- for base in bases:
- base_implements = getattr(base,'__implements__',())
- self.__implements__.update(base_implements) #zoberie prvky z base_implements a tie co tam nie su sa vlozia
- #**********************************************KONTROLA *******************************************************
- predkovia = list(self.__mro__)
- p = dict(self.__dict__.items())
- # ziskam vsetky atributy z predkov
- for predok in predkovia:
- if name == "Class" or predok is Class:
- break
- else:
- p.update(predok.__dict__.items())
- allatributes = list() # toto su vsetky atributy kontrolovanej triedy
- print("***********"+self.__name__ +"***************")
- for kluc, hodnota in p.items():
- # odfiltrujem iba tie ktore nie su privatne
- if (not str(kluc).startswith("__")):
- allatributes.append((kluc, hodnota))
- for imp in self.__implements__:
- #prechazam po vsetko co je na interface
- for atribut in list((imp.__dict__.items())):
- nazovAtributu = atribut[0]
- if str(nazovAtributu).startswith("__"):
- continue
- atr = [(x, y) for x, y in allatributes if x == nazovAtributu]
- if atr.__len__() == 0:
- raise Exception("Nie je implemntovany " + nazovAtributu + " v " + self.__name__)
- else:
- #ak to zhodny typ
- if isinstance((getattr(self,nazovAtributu,None)),type(getattr(imp,nazovAtributu,None))) or \
- getattr(imp,nazovAtributu,None) is type(getattr(self,nazovAtributu,None)) :
- if not isinstance(getattr(self,nazovAtributu),property) :
- if not inspect.getargspec(getattr(imp,nazovAtributu)) == inspect.getargspec(getattr(self,nazovAtributu)):
- raise Exception(nazovAtributu + " nemá tie iste parametere ako interface(musi byt zhodny aj nazov)")
- else:
- raise Exception("Typ nie je zhodny "+ nazovAtributu+" v "+self.__name__)
- #v type je new ina ako inde...
- def __new__(cls, name, bases, attrs, implements=None):
- return super().__new__(cls, name, bases, attrs)
- # tu sa nieco robi
- pass
- # def __call__(self, *args, **kwargs):
- # instancia = self.__new__(*args,**kwargs) #vrati novy neinicializovany objekt, bez zavolania konstruktora! prekryt sa da
- # if instancia is not None:
- # instancia.__init__(*args,**kwargs)
- # pass
- class Class(metaclass=MClass):
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement