Conditions | 44 |
Total Lines | 216 |
Code Lines | 135 |
Lines | 131 |
Ratio | 60.65 % |
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:
Complex classes like bika.lims.exportimport.instruments.panalytical.omnia.AxiosXrfCSVParser.parse_headerline() 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 -*- |
||
207 | def parse_headerline(self, line): |
||
208 | #Process incoming header line |
||
209 | """ |
||
210 | 29/11/2013 10:15:44 |
||
211 | PANalytical |
||
212 | "Quantification of sample ESFERA CINZA - 1g H3BO3 - 1:0,5 - NO PPC", |
||
213 | |||
214 | R.M.S.:,"0,035" |
||
215 | Result status:, |
||
216 | Sum before normalization:,"119,5 %" |
||
217 | Normalised to:,"100,0 %" |
||
218 | Sample type:,Pressed powder |
||
219 | Initial sample weight (g):,"2,000" |
||
220 | Weight after pressing (g):,"3,000" |
||
221 | Correction applied for medium:,No |
||
222 | Correction applied for film:,No |
||
223 | Used Compound list:,Oxides |
||
224 | Results database:,omnian 2013 |
||
225 | Results database in:,c:\panalytical\superq\userdata |
||
226 | """ |
||
227 | |||
228 | if line.startswith('"Quantification of sample') or line.startswith('Quantification of sample'): |
||
229 | line = to_unicode(line) |
||
230 | if len(self._header) == 0: |
||
231 | self.warn('Unexpected header format', numline=self._numline) |
||
232 | return -1 |
||
233 | # Remove non important string and double comas to obtein |
||
234 | # the sample name free |
||
235 | line = line.replace("Quantification of sample ", "") |
||
236 | line = line.replace('"', "") |
||
237 | splitted = line.split(' - ') |
||
238 | |||
239 | if len(splitted) > 3:# Maybe we don't need this, i could be all the sample's identifier... |
||
240 | self._header['Sample'] = splitted[0].strip(' ') |
||
241 | self._header['Quantity'] = splitted[1] |
||
242 | self._header['????'] = splitted[2]# At present we |
||
243 | # don't know what |
||
244 | # is that |
||
245 | self._header['PPC'] = splitted[3] |
||
246 | |||
247 | elif len(splitted) == 1: |
||
248 | self._header['Sample'] = splitted[0].replace('Quantification of sample','').strip(' ') |
||
249 | |||
250 | else: |
||
251 | self.warn('Unexpected header format', numline=self._numline) |
||
252 | return 1 |
||
253 | # Save each header field (that we know) and its own value in the dict |
||
254 | View Code Duplication | if line.startswith('R.M.S.'): |
|
255 | |||
256 | if len(self._header) == 0: |
||
257 | self.err("No header found", numline=self._numline) |
||
258 | return -1 |
||
259 | |||
260 | splitted = self.splitLine(line) |
||
261 | if len(splitted) > 1: |
||
262 | self._header['R.M.S.'] = splitted[1].replace('"', '').strip() |
||
263 | else: |
||
264 | self.warn('Unexpected header format', numline=self._numline) |
||
265 | return 0 |
||
266 | |||
267 | View Code Duplication | if line.startswith('Result status'): |
|
268 | if len(self._header) == 0: |
||
269 | self.err("No header found", numline=self._numline) |
||
270 | |||
271 | splitted = self.splitLine(line) |
||
272 | if len(splitted) > 1: |
||
273 | self._header['Result status'] = splitted[1].replace('"', '').strip() |
||
274 | else: |
||
275 | self.warn('Unexpected header format', numline=self._numline) |
||
276 | |||
277 | return 0 |
||
278 | |||
279 | View Code Duplication | if line.startswith('Sum before normalization'): |
|
280 | if len(self._header) == 0: |
||
281 | self.err("No header found", numline=self._numline) |
||
282 | return -1 |
||
283 | |||
284 | splitted = self.splitLine(line) |
||
285 | if len(splitted) > 1: |
||
286 | self._header['Sum'] = splitted[1].replace('"', '').strip() |
||
287 | else: |
||
288 | self.warn('Unexpected header format', numline=self._numline) |
||
289 | |||
290 | return 0 |
||
291 | |||
292 | View Code Duplication | if line.startswith('Normalised to'): |
|
293 | if len(self._header) == 0: |
||
294 | self.err("No header found", numline=self._numline) |
||
295 | return -1 |
||
296 | |||
297 | splitted = self.splitLine(line) |
||
298 | if len(splitted) > 1: |
||
299 | self._header['Normalized'] = splitted[1].replace('"', '').strip() |
||
300 | else: |
||
301 | self.warn('Unexpected header format', numline=self._numline) |
||
302 | |||
303 | return 0 |
||
304 | |||
305 | View Code Duplication | if line.startswith('Sample type'): |
|
306 | if len(self._header) == 0: |
||
307 | self.err("No header found", numline=self._numline) |
||
308 | return -1 |
||
309 | |||
310 | splitted = self.splitLine(line) |
||
311 | if len(splitted) > 1: |
||
312 | self._header['Sample type'] = splitted[1].strip() |
||
313 | else: |
||
314 | self.warn('Unexpected header format', numline=self._numline) |
||
315 | |||
316 | return 0 |
||
317 | |||
318 | View Code Duplication | if line.startswith('Initial sample weight (g)'): |
|
319 | if len(self._header) == 0: |
||
320 | self.err("No header found", numline=self._numline) |
||
321 | return -1 |
||
322 | |||
323 | splitted = self.splitLine(line) |
||
324 | if len(splitted) > 1: |
||
325 | self._header['Initial sample weight'] = splitted[1].replace('"', '').strip() |
||
326 | else: |
||
327 | self.warn('Unexpected header format', numline=self._numline) |
||
328 | |||
329 | return 0 |
||
330 | |||
331 | View Code Duplication | if line.startswith('Weight after pressing (g)'): |
|
332 | if len(self._header) == 0: |
||
333 | self.err("No header found", numline=self._numline) |
||
334 | return -1 |
||
335 | |||
336 | splitted = self.splitLine(line) |
||
337 | if len(splitted) > 1: |
||
338 | self._header['Weight after pressing'] = splitted[1].replace('"', '').strip() |
||
339 | else: |
||
340 | self.warn('Unexpected header format', numline=self._numline) |
||
341 | |||
342 | return 0 |
||
343 | |||
344 | View Code Duplication | if line.startswith('Correction applied for medium'): |
|
345 | if len(self._header) == 0: |
||
346 | self.warn('Unexpected header format', numline=self._numline) |
||
347 | return -1 |
||
348 | |||
349 | splitted = self.splitLine(line) |
||
350 | if len(splitted) > 1: |
||
351 | self._header['Correction medium'] = splitted[1].replace('"', '').strip() |
||
352 | else: |
||
353 | self.warn('Unexpected header format', numline=self._numline) |
||
354 | |||
355 | return 0 |
||
356 | |||
357 | View Code Duplication | if line.startswith('Correction applied for film'): |
|
358 | if len(self._header) == 0: |
||
359 | self.err("No header found", numline=self._numline) |
||
360 | return -1 |
||
361 | |||
362 | splitted = self.splitLine(line) |
||
363 | if len(splitted) > 1: |
||
364 | self._header['Correction film'] = splitted[1].replace('"', '').strip() |
||
365 | else: |
||
366 | self.warn('Unexpected header format', numline=self._numline) |
||
367 | |||
368 | return 0 |
||
369 | |||
370 | View Code Duplication | if line.startswith('Used Compound list'): |
|
371 | if len(self._header) == 0: |
||
372 | self.err("No header found", numline=self._numline) |
||
373 | return -1 |
||
374 | |||
375 | splitted = self.splitLine(line) |
||
376 | if len(splitted) > 1: |
||
377 | self._header['Used compound'] = splitted[1].replace('"', '').strip() |
||
378 | else: |
||
379 | self.warn('Unexpected header format', numline=self._numline) |
||
380 | |||
381 | return 0 |
||
382 | View Code Duplication | if line.startswith('Results database:'): |
|
383 | if len(self._header) == 0: |
||
384 | self.err("No header found", numline=self._numline) |
||
385 | return -1 |
||
386 | |||
387 | splitted = self.splitLine(line) |
||
388 | if len(splitted) > 1: |
||
389 | self._header['Result database'] = splitted[1].replace('"', '').strip() |
||
390 | else: |
||
391 | self.warn('Unexpected header format', numline=self._numline) |
||
392 | |||
393 | return 0 |
||
394 | |||
395 | |||
396 | if self.columns_name: |
||
397 | if len(self._header) == 0: |
||
398 | self.err("No header found", numline=self._numline) |
||
399 | return -1 |
||
400 | |||
401 | #Grab column names |
||
402 | self._end_header = True |
||
403 | self._columns = self.splitLine(line) |
||
404 | return 1 |
||
405 | |||
406 | if line.startswith('Results database in'): |
||
407 | if len(self._header) == 0: |
||
408 | self.err("No header found", numline=self._numline) |
||
409 | return -1 |
||
410 | |||
411 | splitted = self.splitLine(line) |
||
412 | if len(splitted) > 1: |
||
413 | self._header['Database path'] = splitted[1]+splitted[2] |
||
414 | self.columns_name = True |
||
415 | else: |
||
416 | self.warn('Unexpected header format', numline=self._numline) |
||
417 | |||
418 | return 1 |
||
419 | |||
420 | else: |
||
421 | self._header['Date'] = line |
||
422 | return 1 |
||
423 | |||
524 |