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

Utility.get_benchmark()   F

Complexity

Conditions 11

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
c 1
b 0
f 0
dl 0
loc 25
rs 3.1764

How to fix   Complexity   

Complexity

Complex classes like Utility.get_benchmark() 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
"""Utilities for benchmarks."""
2
3
from . import Rastrigin, Rosenbrock, Griewank, \
4
    Sphere, Ackley, Schwefel, Schwefel221, \
5
    Schwefel222, Whitley
6
7
__all__ = ['Utility']
8
9
10
class Utility(object):
11
12
    # pylint: disable=too-many-return-statements
13
    @staticmethod
14
    def get_benchmark(benchmark):
15
        if not isinstance(benchmark, ''.__class__):
16
            return benchmark
17
        else:
18
            if benchmark == 'rastrigin':
19
                return Rastrigin()
20
            elif benchmark == 'rosenbrock':
21
                return Rosenbrock()
22
            elif benchmark == 'griewank':
23
                return Griewank()
24
            elif benchmark == 'sphere':
25
                return Sphere()
26
            elif benchmark == 'ackley':
27
                return Ackley()
28
            elif benchmark == 'schwefel':
29
                return Schwefel()
30
            elif benchmark == 'schwefel221':
31
                return Schwefel221()
32
            elif benchmark == 'schwefel222':
33
                return Schwefel222()
34
            elif benchmark == 'whitley':
35
                return Whitley()
36
            else:
37
                raise TypeError('Passed benchmark is not defined!')
38