Passed
Pull Request — master (#25)
by Grega
01:09
created

Utility   B

Complexity

Total Complexity 49

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 9
Bugs 4 Features 1
Metric Value
c 9
b 4
f 1
dl 0
loc 79
rs 8.5454
wmc 49

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 2 1
F get_benchmark() 0 69 47
A __raiseLowerAndUpperNotDefined() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like Utility often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
"""Implementation of benchmarks utility function."""
2
3
from . import Rastrigin, Rosenbrock, Griewank, \
4
    Sphere, Ackley, Schwefel, Schwefel221, \
5
    Schwefel222, Whitley
6
7
__all__ = ['Utility']
8
9
10
# pylint: disable=too-many-return-statements
11
class Utility(object):
12
13
    def __init__(self):
14
        pass
15
16
    # pylint: disable=inconsistent-return-statements
17
    def get_benchmark(self, benchmark, Upper=None, Lower=None):
18
        if not isinstance(benchmark, ''.__class__):
19
            return benchmark
20
        else:
21
            if benchmark == 'rastrigin':
22
                if Lower is None and Upper is None:
23
                    return Rastrigin()
24
                elif Lower is not None and Upper is not None:
25
                    return Rastrigin(Lower, Upper)
26
                else:
27
                    self.__raiseLowerAndUpperNotDefined()
28
            elif benchmark == 'rosenbrock':
29
                if Lower is None and Upper is None:
30
                    return Rosenbrock()
31
                elif Lower is not None and Upper is not None:
32
                    return Rosenbrock(Lower, Upper)
33
                else:
34
                    self.__raiseLowerAndUpperNotDefined()
35
            elif benchmark == 'griewank':
36
                if Lower is None and Upper is None:
37
                    return Griewank()
38
                elif Lower is not None and Upper != None:
39
                    return Griewank(Lower, Upper)
40
                else:
41
                    self.__raiseLowerAndUpperNotDefined()
42
            elif benchmark == 'sphere':
43
                if Lower is None and Upper is None:
44
                    return Sphere()
45
                elif Lower is not None and Upper is not None:
46
                    return Sphere(Lower, Upper)
47
                else:
48
                    self.__raiseLowerAndUpperNotDefined()
49
            elif benchmark == 'ackley':
50
                if Lower is None and Upper is None:
51
                    return Ackley()
52
                elif Lower is not None and Upper is not None:
53
                    return Ackley(Lower, Upper)
54
                else:
55
                    self.__raiseLowerAndUpperNotDefined()
56
            elif benchmark == 'schwefel':
57
                if Lower is None and Upper is None:
58
                    return Schwefel()
59
                elif Lower is not None and Upper is not None:
60
                    return Schwefel(Lower, Upper)
61
                else:
62
                    self.__raiseLowerAndUpperNotDefined()
63
            elif benchmark == 'schwefel221':
64
                if Lower is None and Upper is None:
65
                    return Schwefel221()
66
                elif Lower is not None and Upper is not None:
67
                    return Schwefel221(Lower, Upper)
68
                else:
69
                    self.__raiseLowerAndUpperNotDefined()
70
            elif benchmark == 'schwefel222':
71
                if Lower is None and Upper is None:
72
                    return Schwefel222()
73
                elif Lower is not None and Upper is not None:
74
                    return Schwefel222(Lower, Upper)
75
                else:
76
                    self.__raiseLowerAndUpperNotDefined()
77
            elif benchmark == 'whitley':
78
                if Lower is None and Upper is None:
79
                    return Whitley()
80
                elif Lower is not None and Upper is not None:
81
                    return Whitley(Lower, Upper)
82
                else:
83
                    self.__raiseLowerAndUpperNotDefined()
84
            else:
85
                raise TypeError('Passed benchmark is not defined!')
86
87
    @classmethod
88
    def __raiseLowerAndUpperNotDefined(cls):
89
        raise TypeError('Upper and Lower value must be defined!')
90