Passed
Pull Request — master (#101)
by Simon
12:36
created

hyperactive.experiment._utility.add_callback()   A

Complexity

Conditions 3

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nop 2
dl 0
loc 13
rs 9.85
c 0
b 0
f 0
1
def add_callback(before=None, after=None):
2
    def decorator(function):
3
        def wrapper(self, param):
4
            if before:
5
                [before_callback(self, param) for before_callback in before]
6
            result = function(self, param)
7
            if after:
8
                [after_callback(self, param) for after_callback in after]
9
            return result
10
11
        return wrapper
12
13
    return decorator
14
15
16
def add_catch(catch):
17
    def decorator(function):
18
        def wrapper(self, param):
19
            try:
20
                result = function(self, param)
21
            except tuple(catch.keys()) as e:
22
                result = catch[e.__class__]
23
24
            return result
25
26
        return wrapper
27
28
    return decorator
29