Conditions | 9 |
Total Lines | 64 |
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:
1 | import os |
||
134 | def test_main_resume_by_expanding_pool(self): |
||
135 | # SetUp |
||
136 | call(self.launch_command, shell=True) |
||
137 | batch_uid = os.listdir(self.logs_dir)[0] |
||
138 | |||
139 | # Simulate that some commands are in the running state. |
||
140 | nb_commands_files = 2 # 'commands.txt' and 'running_commands.txt' |
||
141 | path_job_commands = os.path.join(self.logs_dir, batch_uid, "commands") |
||
142 | pending_commands_file = pjoin(path_job_commands, "commands.txt") |
||
143 | running_commands_file = pjoin(path_job_commands, "running_commands.txt") |
||
144 | commands = open(pending_commands_file).read().strip().split("\n") |
||
145 | with open(running_commands_file, 'w') as running_commands: |
||
146 | running_commands.write("\n".join(commands[::2]) + "\n") |
||
147 | with open(pending_commands_file, 'w') as pending_commands: |
||
148 | pending_commands.write("\n".join(commands[1::2]) + "\n") |
||
149 | |||
150 | # Remove PBS files so we can check that new ones are going to be created. |
||
151 | print os.listdir(path_job_commands) |
||
152 | for f in os.listdir(path_job_commands): |
||
153 | if f.startswith('job_commands_') and f.endswith('.sh'): |
||
154 | os.remove(pjoin(path_job_commands, f)) |
||
155 | |||
156 | # Actual test. |
||
157 | # Should NOT move running commands back to pending |
||
158 | # but should add new workers. |
||
159 | command_line = self.resume_command.format(batch_uid) |
||
160 | command_line += " --expandPool" |
||
161 | exit_status = call(command_line, shell=True) |
||
162 | |||
163 | # Test validation |
||
164 | assert_equal(exit_status, 0) |
||
165 | assert_true(os.path.isdir(self.logs_dir)) |
||
166 | assert_equal(len(os.listdir(self.logs_dir)), 1) |
||
167 | assert_equal(len(open(running_commands_file).readlines()), len(commands[::2])) |
||
168 | assert_equal(len(open(pending_commands_file).readlines()), len(commands[1::2])) |
||
169 | |||
170 | nb_commands_files = 2 # 'commands.txt' and 'running_commands.txt' |
||
171 | nb_job_commands_files = len(os.listdir(path_job_commands)) |
||
172 | assert_equal(nb_job_commands_files-nb_commands_files, len(commands[1::2])) |
||
173 | |||
174 | # Remove PBS files so we can check that new ones are going to be created. |
||
175 | print os.listdir(path_job_commands) |
||
176 | for f in os.listdir(path_job_commands): |
||
177 | if f.startswith('job_commands_') and f.endswith('.sh'): |
||
178 | os.remove(pjoin(path_job_commands, f)) |
||
179 | |||
180 | # Actual test. |
||
181 | # Should NOT move running commands back to pending |
||
182 | # but should add `nb_workers_to_add` new workers. |
||
183 | nb_workers_to_add = 3 |
||
184 | command_line = self.resume_command.format(batch_uid) |
||
185 | command_line += " --expandPool {}".format(nb_workers_to_add) |
||
186 | exit_status = call(command_line, shell=True) |
||
187 | |||
188 | # Test validation |
||
189 | assert_equal(exit_status, 0) |
||
190 | assert_true(os.path.isdir(self.logs_dir)) |
||
191 | assert_equal(len(os.listdir(self.logs_dir)), 1) |
||
192 | assert_equal(len(open(running_commands_file).readlines()), len(commands[::2])) |
||
193 | assert_equal(len(open(pending_commands_file).readlines()), len(commands[1::2])) |
||
194 | |||
195 | nb_commands_files = 2 # 'commands.txt' and 'running_commands.txt' |
||
196 | nb_job_commands_files = len(os.listdir(path_job_commands)) |
||
197 | assert_equal(nb_job_commands_files-nb_commands_files, nb_workers_to_add) |
||
198 |