| Conditions | 11 |
| Total Lines | 53 |
| Code Lines | 44 |
| 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.instrument.GenericImport() 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 -*- |
||
| 21 | def GenericImport(context, request, parser, importer=None): |
||
| 22 | infile = getResultsInputFile(request) |
||
| 23 | fileformat = getFileFormat(request) |
||
| 24 | artoapply = request.form['artoapply'] |
||
| 25 | override = request.form['results_override'] |
||
| 26 | |||
| 27 | instrument = request.form.get('instrument', None) |
||
| 28 | errors = [] |
||
| 29 | logs = [] |
||
| 30 | warns = [] |
||
| 31 | # Load the most suitable parser according to file extension/options/etc... |
||
| 32 | if not hasattr(infile, 'filename'): |
||
| 33 | errors.append(_("No file selected")) |
||
| 34 | |||
| 35 | if parser: |
||
| 36 | # Load the importer |
||
| 37 | status = ['sample_received', 'attachment_due', 'to_be_verified'] |
||
| 38 | if artoapply == 'received': |
||
| 39 | status = ['sample_received'] |
||
| 40 | elif artoapply == 'received_tobeverified': |
||
| 41 | status = ['sample_received', 'attachment_due', 'to_be_verified'] |
||
| 42 | |||
| 43 | over = [False, False] |
||
| 44 | if override == 'nooverride': |
||
| 45 | over = [False, False] |
||
| 46 | elif override == 'override': |
||
| 47 | over = [True, False] |
||
| 48 | elif override == 'overrideempty': |
||
| 49 | over = [True, True] |
||
| 50 | |||
| 51 | imp = importer |
||
| 52 | if not imp: |
||
| 53 | imp = AnalysisResultsImporter(parser=parser, |
||
| 54 | context=context, |
||
| 55 | allowed_ar_states=status, |
||
| 56 | allowed_analysis_states=None, |
||
| 57 | override=over, |
||
| 58 | instrument_uid=instrument) |
||
| 59 | |||
| 60 | tbex = '' |
||
| 61 | try: |
||
| 62 | imp.process() |
||
| 63 | except: |
||
| 64 | tbex = traceback.format_exc() |
||
| 65 | errors = imp.errors |
||
| 66 | logs = imp.logs |
||
| 67 | warns = imp.warns |
||
| 68 | if tbex: |
||
| 69 | errors.append(tbex) |
||
| 70 | |||
| 71 | results = {'errors': errors, 'log': logs, 'warns': warns} |
||
| 72 | |||
| 73 | return json.dumps(results) |
||
| 74 | |||
| 88 |