Conditions | 9 |
Total Lines | 73 |
Code Lines | 55 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
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 |
||
93 | def search( |
||
94 | self, |
||
95 | objective_function, |
||
96 | n_iter, |
||
97 | max_time=None, |
||
98 | max_score=None, |
||
99 | early_stopping=None, |
||
100 | memory=True, |
||
101 | memory_warm_start=None, |
||
102 | verbosity=["progress_bar", "print_results", "print_times"], |
||
103 | ): |
||
104 | self.start_time = time.time() |
||
105 | |||
106 | if verbosity is False: |
||
107 | verbosity = [] |
||
108 | |||
109 | self.objective_function = objective_function |
||
110 | self.n_iter = n_iter |
||
111 | self.max_time = max_time |
||
112 | self.max_score = max_score |
||
113 | self.early_stopping = early_stopping |
||
114 | self.memory = memory |
||
115 | self.memory_warm_start = memory_warm_start |
||
116 | self.verbosity = verbosity |
||
117 | |||
118 | self.stop = StopRun(max_time, max_score, early_stopping) |
||
119 | |||
120 | self._init_search() |
||
121 | |||
122 | if isinstance(memory, DictProxy): |
||
123 | mem = Memory(memory_warm_start, self.conv, dict_proxy=memory) |
||
124 | self.score = self.results_mang.score(mem.memory(objective_function)) |
||
125 | elif memory is True: |
||
126 | mem = Memory(memory_warm_start, self.conv) |
||
127 | self.score = self.results_mang.score(mem.memory(objective_function)) |
||
128 | else: |
||
129 | self.score = self.results_mang.score(objective_function) |
||
130 | |||
131 | # loop to initialize N positions |
||
132 | for init_pos, nth_iter in zip(self.init_positions, range(n_iter)): |
||
133 | if self.stop.check(self.start_time, self.p_bar.score_best, self.score_l): |
||
134 | break |
||
135 | self._initialization(init_pos, nth_iter) |
||
136 | |||
137 | self.finish_initialization() |
||
138 | |||
139 | # loop to do the iterations |
||
140 | for nth_iter in range(len(self.init_positions), n_iter): |
||
141 | if self.stop.check(self.start_time, self.p_bar.score_best, self.score_l): |
||
142 | break |
||
143 | self._iteration(nth_iter) |
||
144 | |||
145 | self.search_data = pd.DataFrame(self.results_mang.results_list) |
||
146 | |||
147 | self.best_score = self.p_bar.score_best |
||
148 | self.best_value = self.conv.position2value(self.p_bar.pos_best) |
||
149 | self.best_para = self.conv.value2para(self.best_value) |
||
150 | |||
151 | if memory not in [False, None]: |
||
152 | self.memory_dict = mem.memory_dict |
||
153 | else: |
||
154 | self.memory_dict = {} |
||
155 | |||
156 | self.p_bar.close() |
||
157 | |||
158 | self.print_info( |
||
159 | verbosity, |
||
160 | self.objective_function, |
||
161 | self.best_score, |
||
162 | self.best_para, |
||
163 | self.eval_times, |
||
164 | self.iter_times, |
||
165 | self.n_iter, |
||
166 | ) |
||
167 |