Conditions | 12 |
Total Lines | 63 |
Code Lines | 50 |
Lines | 63 |
Ratio | 100 % |
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.lifetechnologies.qubit.qubit.Import() 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 -*- |
||
19 | View Code Duplication | def Import(context, request): |
|
|
|||
20 | """ Read qubit analysis results |
||
21 | """ |
||
22 | infile = request.form['filename'] |
||
23 | fileformat = request.form['format'] |
||
24 | artoapply = request.form['artoapply'] |
||
25 | override = request.form['override'] |
||
26 | |||
27 | instrument = request.form.get('instrument', None) |
||
28 | errors = [] |
||
29 | logs = [] |
||
30 | warns = [] |
||
31 | |||
32 | # Load the suitable parser |
||
33 | parser = None |
||
34 | if not hasattr(infile, 'filename'): |
||
35 | errors.append(_("No file selected")) |
||
36 | elif fileformat == 'csv': |
||
37 | analysis = request.form.get('analysis', None) |
||
38 | if analysis: |
||
39 | parser = QuBitCSVParser(infile, analysis) |
||
40 | else: |
||
41 | errors.append(t(_("No analysis selected"))) |
||
42 | else: |
||
43 | errors.append(t(_("Unrecognized file format ${fileformat}", |
||
44 | mapping={"fileformat": fileformat}))) |
||
45 | |||
46 | if parser: |
||
47 | # Load the importer |
||
48 | status = ['sample_received', 'attachment_due', 'to_be_verified'] |
||
49 | if artoapply == 'received': |
||
50 | status = ['sample_received'] |
||
51 | elif artoapply == 'received_tobeverified': |
||
52 | status = ['sample_received', 'attachment_due', 'to_be_verified'] |
||
53 | |||
54 | over = [False, False] |
||
55 | if override == 'nooverride': |
||
56 | over = [False, False] |
||
57 | elif override == 'override': |
||
58 | over = [True, False] |
||
59 | elif override == 'overrideempty': |
||
60 | over = [True, True] |
||
61 | |||
62 | importer = QuBitImporter(parser=parser, |
||
63 | context=context, |
||
64 | allowed_ar_states=status, |
||
65 | allowed_analysis_states=None, |
||
66 | override=over, |
||
67 | instrument_uid=instrument) |
||
68 | |||
69 | tbex = '' |
||
70 | try: |
||
71 | importer.process() |
||
72 | except: |
||
73 | tbex = traceback.format_exc() |
||
74 | errors = importer.errors |
||
75 | logs = importer.logs |
||
76 | warns = importer.warns |
||
77 | if tbex: |
||
78 | errors.append(tbex) |
||
79 | |||
80 | results = {'errors': errors, 'log': logs, 'warns': warns} |
||
81 | return json.dumps(results) |
||
82 |