| Conditions | 4 |
| Total Lines | 59 |
| Code Lines | 48 |
| 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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | """ |
||
| 28 | def run_processor( |
||
| 29 | processorClass, |
||
| 30 | ocrd_tool=None, |
||
| 31 | mets_url=None, |
||
| 32 | resolver=None, |
||
| 33 | workspace=None, |
||
| 34 | page_id=None, |
||
| 35 | log_level=None, # TODO actually use this! |
||
| 36 | input_file_grp=None, |
||
| 37 | output_file_grp=None, |
||
| 38 | parameter=None, |
||
| 39 | parameter_override=None, |
||
| 40 | working_dir=None, |
||
| 41 | ): # pylint: disable=too-many-locals |
||
| 42 | """ |
||
| 43 | Create a workspace for mets_url and run processor through it |
||
| 44 | |||
| 45 | Args: |
||
| 46 | parameter (string): URL to the parameter |
||
| 47 | """ |
||
| 48 | workspace = _get_workspace( |
||
| 49 | workspace, |
||
| 50 | resolver, |
||
| 51 | mets_url, |
||
| 52 | working_dir |
||
| 53 | ) |
||
| 54 | log.debug("Running processor %s", processorClass) |
||
| 55 | processor = processorClass( |
||
| 56 | workspace, |
||
| 57 | ocrd_tool=ocrd_tool, |
||
| 58 | page_id=page_id, |
||
| 59 | input_file_grp=input_file_grp, |
||
| 60 | output_file_grp=output_file_grp, |
||
| 61 | parameter=parameter |
||
| 62 | ) |
||
| 63 | ocrd_tool = processor.ocrd_tool |
||
| 64 | name = '%s v%s' % (ocrd_tool['executable'], processor.version) |
||
| 65 | otherrole = ocrd_tool['steps'][0] |
||
| 66 | logProfile = getLogger('ocrd.process.profile') |
||
| 67 | log.debug("Processor instance %s (%s doing %s)", processor, name, otherrole) |
||
| 68 | t0 = time() |
||
| 69 | processor.process() |
||
| 70 | t1 = time() - t0 |
||
| 71 | logProfile.info("Executing processor '%s' took %fs [--input-file-grp='%s' --output-file-grp='%s' --parameter='%s']" % ( |
||
| 72 | ocrd_tool['executable'], |
||
| 73 | t1, |
||
| 74 | input_file_grp if input_file_grp else '', |
||
| 75 | output_file_grp if output_file_grp else '', |
||
| 76 | json.dumps(parameter) if parameter else {} |
||
| 77 | )) |
||
| 78 | workspace.mets.add_agent( |
||
| 79 | name=name, |
||
| 80 | _type='OTHER', |
||
| 81 | othertype='SOFTWARE', |
||
| 82 | role='OTHER', |
||
| 83 | otherrole=otherrole |
||
| 84 | ) |
||
| 85 | workspace.save_mets() |
||
| 86 | return processor |
||
| 87 | |||
| 179 |