Conditions | 11 |
Total Lines | 57 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
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.thermoscientific.gallery.Ts9861x.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 | def Import(context, request): |
||
20 | """ Thermo Scientific - Gallery 9861x analysis results |
||
21 | """ |
||
22 | infile = request.form['thermoscientific_gallery_9861x_file'] |
||
23 | fileformat = request.form['thermoscientific_gallery_9861x_format'] |
||
24 | artoapply = request.form['thermoscientific_gallery_9861x_artoapply'] |
||
25 | override = request.form['thermoscientific_gallery_9861x_override'] |
||
26 | instrument = request.form.get('thermoscientific_gallery_9861x_instrument', None) |
||
27 | errors = [] |
||
28 | logs = [] |
||
29 | |||
30 | # Load the most suitable parser according to file extension/options/etc... |
||
31 | parser = None |
||
32 | if not hasattr(infile, 'filename'): |
||
33 | errors.append(_("No file selected")) |
||
34 | if fileformat == 'tsv_40': |
||
35 | parser = ThermoGallery9861xTSVParser(infile) |
||
36 | else: |
||
37 | errors.append(t(_("Unrecognized file format ${fileformat}", |
||
38 | mapping={"fileformat": fileformat}))) |
||
39 | |||
40 | if parser: |
||
41 | # Load the importer |
||
42 | status = ['sample_received', 'attachment_due', 'to_be_verified'] |
||
43 | if artoapply == 'received': |
||
44 | status = ['sample_received'] |
||
45 | elif artoapply == 'received_tobeverified': |
||
46 | status = ['sample_received', 'attachment_due', 'to_be_verified'] |
||
47 | |||
48 | over = [False, False] |
||
49 | if override == 'nooverride': |
||
50 | over = [False, False] |
||
51 | elif override == 'override': |
||
52 | over = [True, False] |
||
53 | elif override == 'overrideempty': |
||
54 | over = [True, True] |
||
55 | |||
56 | importer = ThermoGallery9861xImporter(parser=parser, |
||
57 | context=context, |
||
58 | allowed_ar_states=status, |
||
59 | allowed_analysis_states=None, |
||
60 | override=over, |
||
61 | instrument_uid=instrument) |
||
62 | tbex = '' |
||
63 | try: |
||
64 | importer.process() |
||
65 | except: |
||
66 | tbex = traceback.format_exc() |
||
67 | errors = importer.errors |
||
68 | logs = importer.logs |
||
69 | warns = importer.warns |
||
70 | if tbex: |
||
71 | errors.append(tbex) |
||
72 | |||
73 | results = {'errors': errors, 'log': logs, 'warns': warns} |
||
|
|||
74 | |||
75 | return json.dumps(results) |
||
76 | |||
88 |