Conditions | 34 |
Total Lines | 158 |
Lines | 0 |
Ratio | 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:
Complex classes like zipline.gens.AlgorithmSimulator._process_snapshot() 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 | # |
||
160 | perf_tracker.position_tracker.handle_splits(splits) |
||
161 | |||
162 | with self.processor, ZiplineAPI(self.algo): |
||
163 | for dt, action in self.clock: |
||
164 | if action == DATA_AVAILABLE: |
||
165 | inner_loop(dt) |
||
166 | elif action == ONCE_A_DAY: |
||
167 | once_a_day(dt) |
||
168 | elif action == CALC_DAILY_PERFORMANCE: |
||
169 | algo.perf_tracker.all_benchmark_returns[dt] = \ |
||
170 | self.benchmark_source.get_value(dt) |
||
171 | yield self._get_daily_message(dt, algo, algo.perf_tracker) |
||
172 | elif action == CALC_MINUTE_PERFORMANCE: |
||
173 | algo.perf_tracker.all_benchmark_returns[dt] = \ |
||
174 | self.benchmark_source.get_value(dt) |
||
175 | minute_msg, daily_msg = \ |
||
176 | self._get_minute_message(dt, algo, algo.perf_tracker) |
||
177 | |||
178 | yield minute_msg |
||
179 | |||
180 | if daily_msg: |
||
181 | yield daily_msg |
||
182 | |||
183 | risk_message = algo.perf_tracker.handle_simulation_end() |
||
184 | yield risk_message |
||
185 | |||
186 | @staticmethod |
||
187 | def _get_daily_message(dt, algo, perf_tracker): |
||
188 | """ |
||
189 | Get a perf message for the given datetime. |
||
190 | """ |
||
191 | perf_message = perf_tracker.handle_market_close_daily(dt) |
||
192 | perf_message['daily_perf']['recorded_vars'] = algo.recorded_vars |
||
193 | return perf_message |
||
194 | |||
195 | @staticmethod |
||
196 | def _get_minute_message(dt, algo, perf_tracker): |
||
197 | """ |
||
198 | Get a perf message for the given datetime. |
||
199 | """ |
||
200 | rvars = algo.recorded_vars |
||
201 | |||
202 | minute_message, daily_message = perf_tracker.handle_minute_close(dt) |
||
203 | minute_message['minute_perf']['recorded_vars'] = rvars |
||
204 | |||
205 | if daily_message: |
||
206 | daily_message["daily_perf"]["recorded_vars"] = rvars |
||
207 | |||
208 | return minute_message, daily_message |
||
209 |