Completed
Push — master ( 6f1632...83b5e1 )
by Ben
01:39
created

ClassOperationResult.__init__()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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