OptimizerAttributes._add_result_attributes()   C
last analyzed

Complexity

Conditions 11

Size

Total Lines 47
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 33
nop 8
dl 0
loc 47
rs 5.4
c 0
b 0
f 0

How to fix   Complexity    Many Parameters   

Complexity

Complex classes like hyperactive.optimizers.strategies.optimizer_attributes.OptimizerAttributes._add_result_attributes() 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.

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import pandas as pd
6
7
8
class OptimizerAttributes:
9
    def __init__(self):
10
        self.best_para = None
11
        self.best_score = None
12
        self.best_since_iter = None
13
        self.eval_times = None
14
        self.iter_times = None
15
        self.search_data = None
16
        self.random_seed = None
17
18
    def _add_result_attributes(
19
        self,
20
        best_para,
21
        best_score,
22
        best_since_iter,
23
        eval_times,
24
        iter_times,
25
        search_data,
26
        random_seed,
27
    ):
28
        if self.best_para is None:
29
            self.best_para = best_para
30
        else:
31
            if best_score > self.best_score:
32
                self.best_para = best_para
33
34
        if self.best_score is None:
35
            self.best_score = best_score
36
        else:
37
            if best_score > self.best_score:
38
                self.best_score = best_score
39
40
        if self.best_since_iter is None:
41
            self.best_since_iter = best_since_iter
42
        else:
43
            if best_score > self.best_score:
44
                self.best_since_iter = best_since_iter
45
46
        if self.eval_times is None:
47
            self.eval_times = eval_times
48
        else:
49
            self.eval_times = self.eval_times + eval_times
50
51
        if self.iter_times is None:
52
            self.iter_times = iter_times
53
        else:
54
            self.iter_times = self.iter_times + eval_times
55
56
        if self.search_data is None:
57
            self.search_data = search_data
58
        else:
59
            self.search_data = pd.concat(
60
                [self.search_data, search_data], ignore_index=True
61
            )
62
63
        if self.random_seed is None:
64
            self.random_seed = random_seed
65