-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathframework.py
120 lines (104 loc) · 2.89 KB
/
framework.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
class NameSpace(object):
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super(NameSpace, cls).__new__(cls)
cls._instance.init()
return cls._instance
def init(self):
pass
class Register():
def __init__(self):
self.hooks = {}
def __call__(self, evname, **kwargs):
def register_decorator(func):
hooks = self.hooks.setdefault(evname, [])
hooks.append({'exec': func,
'event': kwargs.get('event'),
'object': kwargs.get('object'),
'subject': kwargs.get('subject')})
return func
return register_decorator
def exec(cmd, obj):
# execute function
if callable(cmd):
return cmd(obj)
#conpare dictionaries
if isinstance(cmd, dict):
#print('compare dictionaries')
for k, v in cmd.items():
if not exec(v, obj.__getattr__(k)):
False
return True
if isinstance(cmd, list):
# compare lists
if isinstance(obj, list):
if len(cmd) != len(obj):
return False
for i in range(len(cmd)):
if not exec(cmd[i], obj.__getattr__(i)):
return False
return True
# this is OR
for i in cmd:
if exec(i, obj):
return True
return False
# and compare
return cmd == obj
class Xor:
def __init__(self, a, b):
self.a = a
self.b = b
def __call__(self, obj):
return exec(self.a, obj) ^ exec(self.b, obj)
class Not:
def __init__(self, arg):
self.arg = arg
def __call__(self, val):
return not exec(self.arg, val)
class And:
def __init__(self, *args):
self.args = args
def __call__(self, obj):
for i in self.args:
if not exec(i, obj): return False
return True
class Or:
def __init__(self, *args):
self.args = args
def __call__(self, obj):
for i in self.args:
if exec(i, obj): return True
return False
class Dividable:
def __init__(self, val):
self.val = val
def __call__(self, val):
return self.val % val == 0
class Ge:
def __init__(self, val):
self.val = val
def __call__(self, val):
return self.val <= val
class Gt:
def __init__(self, val):
self.val = val
def __call__(self, val):
return self.val < val
class Le:
def __init__(self, val):
self.val = val
def __call__(self, val):
return self.val >= val
class Lt:
def __init__(self, val):
self.val = val
def __call__(self, val):
return self.val > val
class Between:
def __init__(self, a, b):
self.a = a
self.b = b
def __call__(self, val):
return self.a <= val and val <= self.b