| Conditions | 18 |
| Total Lines | 70 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 ScheduledTrainingServer.handle_control() 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 | #!/usr/bin/env python |
||
| 98 | def handle_control(self, req, worker_id): |
||
| 99 | """ |
||
| 100 | Handles a control_request received from a worker. |
||
| 101 | Returns: |
||
| 102 | string or dict: response |
||
| 103 | |||
| 104 | 'stop' - the worker should quit |
||
| 105 | 'wait' - wait for 1 second |
||
| 106 | 'eval' - evaluate on valid and test set to start a new epoch |
||
| 107 | 'sync_hyperparams' - set learning rate |
||
| 108 | 'valid' - evaluate on valid and test set, then save the params |
||
| 109 | 'train' - train next batches |
||
| 110 | """ |
||
| 111 | if self.start_time is None: self.start_time = time.time() |
||
| 112 | response = "" |
||
| 113 | |||
| 114 | if req == 'next': |
||
| 115 | if self.num_train_batches == 0: |
||
| 116 | response = "get_num_batches" |
||
| 117 | elif self._done: |
||
| 118 | response = "stop" |
||
| 119 | self.worker_is_done(worker_id) |
||
| 120 | elif self._evaluating: |
||
| 121 | response = 'wait' |
||
| 122 | elif not self.batch_pool: |
||
| 123 | # End of one iter |
||
| 124 | response = 'eval' |
||
| 125 | self._evaluating = True |
||
| 126 | else: |
||
| 127 | # Continue training |
||
| 128 | if worker_id not in self.prepared_worker_pool: |
||
| 129 | response = {"sync_hyperparams": self.feed_hyperparams()} |
||
| 130 | self.prepared_worker_pool.add(worker_id) |
||
| 131 | elif self._iters_from_last_valid >= self._valid_freq: |
||
| 132 | response = 'valid' |
||
| 133 | self._iters_from_last_valid = 0 |
||
| 134 | else: |
||
| 135 | response = {"train": self.feed_batches()} |
||
| 136 | elif 'eval_done' in req: |
||
| 137 | messages = req['eval_done'] |
||
| 138 | self._evaluating = False |
||
| 139 | sys.stdout.write("\r") |
||
| 140 | sys.stdout.flush() |
||
| 141 | for msg in messages: |
||
| 142 | logging.info(msg) |
||
| 143 | continue_training = self.prepare_epoch() |
||
| 144 | if not continue_training: |
||
| 145 | self._done = True |
||
| 146 | logging.info("training time {:.4f}s".format(time.time() - self.start_time)) |
||
| 147 | response = "stop" |
||
| 148 | elif 'valid_done' in req: |
||
| 149 | messages = req['valid_done'] |
||
| 150 | sys.stdout.write("\r") |
||
| 151 | sys.stdout.flush() |
||
| 152 | for msg in messages: |
||
| 153 | logging.info(msg) |
||
| 154 | elif 'train_done' in req: |
||
| 155 | costs = req['costs'] |
||
| 156 | self._train_costs.append(costs) |
||
| 157 | sys.stdout.write("\x1b[2K\r> %d%% | J=%.2f" % (self._current_iter * 100 / self.num_train_batches, |
||
| 158 | costs[0])) |
||
| 159 | sys.stdout.flush() |
||
| 160 | elif 'get_num_batches_done' in req: |
||
| 161 | self.num_train_batches = req['get_num_batches_done'] |
||
| 162 | elif 'get_easgd_alpha' in req: |
||
| 163 | response = self._easgd_alpha |
||
| 164 | elif 'sync_hyperparams' in req: |
||
| 165 | response = {"sync_hyperparams": self.feed_hyperparams()} |
||
| 166 | |||
| 167 | return response |
||
| 168 | |||
| 188 |