|
1
|
|
|
"Some utility methods" |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
class ClassOperationResult(object): |
|
5
|
|
|
""" |
|
6
|
|
|
Result of Class Operation |
|
7
|
|
|
""" |
|
8
|
|
|
def __init__(self, operation, cls1, cls2): |
|
9
|
|
|
""" |
|
10
|
|
|
""" |
|
11
|
|
|
self.cls1 = cls1 |
|
12
|
|
|
self.cls2 = cls2 |
|
13
|
|
|
self._operation = operation |
|
14
|
|
|
|
|
15
|
|
|
def __getattr__(self, attr): |
|
16
|
|
|
attr1 = getattr(self.cls1, attr) |
|
17
|
|
|
attr2 = getattr(self.cls2, attr) |
|
18
|
|
|
|
|
19
|
|
|
try: |
|
20
|
|
|
return getattr(attr1, self._operation)(attr2) |
|
21
|
|
|
|
|
22
|
|
|
except AttributeError: |
|
23
|
|
|
return (lambda *args, **kwargs: |
|
24
|
|
|
getattr(attr1(*args), self._operation)(attr2(*args))) |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
class ClassOperation(object): |
|
28
|
|
|
""" |
|
29
|
|
|
Define arithmetic operation on Class |
|
30
|
|
|
""" |
|
31
|
|
|
def __add__(self, other): |
|
32
|
|
|
return ClassOperationResult('__add__', self, other) |
|
33
|
|
|
|
|
34
|
|
|
def __sub__(self, other): |
|
35
|
|
|
return ClassOperationResult('__sub__', self, other) |
|
36
|
|
|
|
|
37
|
|
|
def __mul__(self, other): |
|
38
|
|
|
return ClassOperationResult('__mul__', self, other) |
|
39
|
|
|
|
|
40
|
|
|
def __gt__(self, other): |
|
41
|
|
|
return ClassOperationResult('__gt__', self, other) |
|
42
|
|
|
|