-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatatype.py
57 lines (56 loc) · 1.95 KB
/
datatype.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#coding=utf-8
class TypeMeta(type):
__name__ = "ExprMeta"
__subs__ = {}
def __new__(cls,name,parents,attrs):
#print("new metaclass info: ", cls, name, parents, attrs)
attrs["__name__"] = name
#print( attrs.keys() )
if '__init__' not in attrs.keys() and parents != ( ):
attrs["__init__"] = lambda self: None
if "__repr__" not in attrs.keys():
attrs["__repr__"] = lambda self: self.__name__
if parents == ( ):
if name not in cls.__subs__.keys():
cls.__subs__[name] = [ ] # init
else:
for p in parents:
cls.__subs__[p.__name__] += [name]
return type.__new__(cls, name, parents, attrs)
def __instancecheck__(cls,instance):
if hasattr(instance,"__name__") and hasattr(cls,"__subs__"):
name = instance.__name__
if cls.__name__ in cls.__subs__.keys():
return name in cls.__subs__[cls.__name__]
return False
def __repr__(self):
return self.__name__
class species(object):
def __init__(self,func):
self.func = func
def __get__(self,obj,typ=None):
def wrapper(*args,**kw):
return self.func(typ,*args,**kw)
return wrapper
class static(object):
def __init__(self,func):
self.func = func
def __get__(self,obj,typ=None):
def wrapper(*args,**kw):
return self.func(*args,**kw)
return wrapper
class prop(object):
def __init__(self,func):
self.func = func
def __get__(self,obj,typ=None):
def wrapper(*args,**kw):
return self.func(obj,*args,**kw)
return wrapper()
class class_prop(object):
def __init__(self,func):
self.func = func
def __get__(self,obj,typ=None):
def wrapper(*args,**kw):
return self.func(typ,*args,**kw)
return wrapper()
__all__ = ["TypeMeta","species","static","prop","class_prop"]