Conditions | 20 |
Total Lines | 167 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 AbstractGenerator._write_doc_aini2016() 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 | # -*- coding: utf-8 -*- |
||
188 | def _write_doc_aini2016(self, doc, record): |
||
189 | print('"%s"' % record['Title']) |
||
190 | exreg4num = re.compile(r'\((\w+)\)') |
||
191 | |||
192 | font = doc.styles['Normal'].font |
||
193 | font.size = docx.shared.Pt(10) |
||
194 | font.name = 'Times New Roman' |
||
195 | |||
196 | # Program Number |
||
197 | # p = doc.add_paragraph() |
||
198 | # p.paragraph_format.line_spacing = docx.shared.Pt(12) |
||
199 | # p.paragraph_format.space_after = docx.shared.Pt(5) |
||
200 | # r = p.add_run(record['Program No.'].strip()) |
||
201 | |||
202 | # Title |
||
203 | p = doc.add_paragraph() |
||
204 | p.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER |
||
205 | p.paragraph_format.space_before = docx.shared.Pt(25) |
||
206 | p.paragraph_format.space_after = docx.shared.Pt(14) |
||
207 | r = p.add_run(record['Title'].strip()) |
||
208 | r.font.size = docx.shared.Pt(12) |
||
209 | r.bold = True |
||
210 | r.italic = True |
||
211 | |||
212 | # Authors |
||
213 | p = doc.add_paragraph() |
||
214 | p.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER |
||
215 | p.paragraph_format.line_spacing = docx.shared.Pt(12) |
||
216 | p.paragraph_format.space_after = docx.shared.Pt(12) |
||
217 | authors = self._to_array(record['Name'], '\n') |
||
218 | first = True |
||
219 | for author in authors: |
||
220 | m = self.exreg4author.match(author) |
||
221 | if not first: |
||
222 | p.add_run(', ').bold = True |
||
223 | name = m.group(1).strip().replace(' ', '\u00A0') |
||
224 | num = self._remove_parentheses(m.group(2).strip()) |
||
225 | p.add_run(name).bold = True |
||
226 | if num != '': |
||
227 | r = p.add_run('\u00A0' + num) |
||
228 | r.bold = True |
||
229 | r.font.superscript = True |
||
230 | first = False |
||
231 | p.add_run('\n') |
||
232 | |||
233 | # Affiliation |
||
234 | affiliations = self._to_array(record['Affiliation'], '\n') |
||
235 | first = True |
||
236 | for affiliation in affiliations: |
||
237 | m = self.exreg4affiliation.match(affiliation) |
||
238 | if not first: |
||
239 | p.add_run(', ') |
||
240 | num = self._remove_parentheses(m.group(1).strip()) |
||
241 | name = m.group(2).strip() |
||
242 | if num != '': |
||
243 | r = p.add_run(num + '\u00A0') |
||
244 | r.font.superscript = True |
||
245 | p.add_run(name) |
||
246 | first = False |
||
247 | p.add_run('\n' + record['e-mail']) |
||
248 | |||
249 | # DOI |
||
250 | p = doc.add_paragraph('DOI:' + record['DOI'].strip()) |
||
251 | p.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER |
||
252 | p.paragraph_format.space_after = docx.shared.Pt(12) |
||
253 | |||
254 | # Abstract Body |
||
255 | items = self._to_array(record['Abstract'], '\n') |
||
256 | first = True |
||
257 | for item in items: |
||
258 | p = doc.add_paragraph(item) |
||
259 | p.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.JUSTIFY |
||
260 | p.paragraph_format.line_spacing = docx.shared.Pt(11) |
||
261 | p.paragraph_format.space_after = docx.shared.Pt(2) |
||
262 | if not first: |
||
263 | p.paragraph_format.first_line_indent = docx.shared.Pt(12) |
||
264 | first = False |
||
265 | p.paragraph_format.space_after = docx.shared.Pt(12) |
||
266 | |||
267 | # Figure |
||
268 | if not self._empty(record['Figure file Name']): |
||
269 | |||
270 | # Figure File Name |
||
271 | img_fpath = os.path.join(self.image_dir, record['Figure file Name']) |
||
272 | size = self._get_preferred_image_size(img_fpath) |
||
273 | doc.add_picture(img_fpath, width=size[0]) # , height=size[1]) |
||
274 | p = doc.paragraphs[-1] |
||
275 | p.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER |
||
276 | |||
277 | # Figure Comment |
||
278 | items = self._to_array(record['Figure comment'], '\n') |
||
279 | first = True |
||
280 | for item in items: |
||
281 | p = doc.add_paragraph() |
||
282 | p.paragraph_format.line_spacing = docx.shared.Pt(10) |
||
283 | p.paragraph_format.space_after = docx.shared.Pt(0) |
||
284 | p.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.JUSTIFY |
||
285 | if first: |
||
286 | p.add_run('Figure: ').bold = True |
||
287 | first = False |
||
288 | p.add_run(item) |
||
289 | |||
290 | p.paragraph_format.space_after = docx.shared.Pt(14) |
||
291 | |||
292 | # References |
||
293 | items = self._to_array(record['References'], '\n') |
||
294 | first = True |
||
295 | for item in items: |
||
296 | if first: |
||
297 | p = doc.add_paragraph() |
||
298 | p.paragraph_format.line_spacing = docx.shared.Pt(11) |
||
299 | p.paragraph_format.space_after = docx.shared.Pt(0) |
||
300 | p.add_run('References:').bold = True |
||
301 | first = False |
||
302 | p = doc.add_paragraph() |
||
303 | p.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.JUSTIFY |
||
304 | p.paragraph_format.line_spacing = docx.shared.Pt(10) |
||
305 | p.paragraph_format.space_after = docx.shared.Pt(0) |
||
306 | p.add_run(item) |
||
307 | p.paragraph_format.space_after = docx.shared.Pt(10) |
||
308 | |||
309 | # Acknowledgement |
||
310 | items = self._to_array(record['Acknowledgement'], '\n') |
||
311 | first = True |
||
312 | for item in items: |
||
313 | p = doc.add_paragraph() |
||
314 | p.paragraph_format.line_spacing = docx.shared.Pt(10) |
||
315 | p.paragraph_format.space_after = docx.shared.Pt(0) |
||
316 | p.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.JUSTIFY |
||
317 | if first: |
||
318 | p.add_run('Ackknowledgement: ').bold = True |
||
319 | first = False |
||
320 | p.add_run(item) |
||
321 | p.paragraph_format.space_after = docx.shared.Pt(10) |
||
322 | |||
323 | # Funding |
||
324 | items = self._to_array(record['Funding'], '\n') |
||
325 | first = True |
||
326 | for item in items: |
||
327 | p = doc.add_paragraph() |
||
328 | p.paragraph_format.line_spacing = docx.shared.Pt(10) |
||
329 | p.paragraph_format.space_after = docx.shared.Pt(0) |
||
330 | p.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.JUSTIFY |
||
331 | if first: |
||
332 | p.add_run('Funding: ').bold = True |
||
333 | first = False |
||
334 | p.add_run(item) |
||
335 | p.paragraph_format.space_after = docx.shared.Pt(10) |
||
336 | |||
337 | # Citation |
||
338 | p = doc.add_paragraph() |
||
339 | p.paragraph_format.line_spacing = docx.shared.Pt(10) |
||
340 | p.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.JUSTIFY |
||
341 | p.add_run('Citation: ').bold = True |
||
342 | author_tmp = '' |
||
343 | first = True |
||
344 | for author in authors: |
||
345 | m = self.exreg4author.match(author) |
||
346 | if not first: |
||
347 | author_tmp += ', ' |
||
348 | author_tmp += m.group(1).strip() |
||
349 | first = False |
||
350 | p.add_run(author_tmp + ' (2016). ' + record['Title'].strip().replace('\n', ' ') + '. ') |
||
351 | p.add_run('Advances in Neuroinformatics IV. ').italic = True |
||
352 | p.add_run( |
||
353 | 'AINI 2016 and INCF Nodes Workshop Abstract: ' + record['Program No. Long'].strip() + '. DOI:' + record[ |
||
354 | 'DOI'].strip()) |
||
355 | |||
362 |