| Conditions | 10 |
| Total Lines | 55 |
| Code Lines | 27 |
| 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.adapters.dynamicresultsrange.DynamicResultsRange.get_results_range() 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 -*- |
||
| 81 | def get_results_range(self): |
||
| 82 | """Return the dynamic results range |
||
| 83 | |||
| 84 | The returning dicitionary should containe at least the `min` and `max` |
||
| 85 | values to override the ResultsRangeDict data. |
||
| 86 | |||
| 87 | :returns: An `IResultsRangeDict` compatible dict |
||
| 88 | :rtype: dict |
||
| 89 | """ |
||
| 90 | if self.dynamicspec is None: |
||
| 91 | return {} |
||
| 92 | # A matching Analysis Keyword is mandatory for any further matches |
||
| 93 | keyword = self.analysis.getKeyword() |
||
| 94 | by_keyword = self.dynamicspec.get_by_keyword() |
||
| 95 | # Get all specs (rows) from the Excel with the same Keyword |
||
| 96 | specs = by_keyword.get(keyword) |
||
| 97 | if not specs: |
||
| 98 | return {} |
||
| 99 | |||
| 100 | # Generate a match data object, which match both the column names and |
||
| 101 | # the field names of the Analysis. |
||
| 102 | match_data = self.get_match_data() |
||
| 103 | |||
| 104 | rr = {} |
||
| 105 | |||
| 106 | # Iterate over the rows and return the first where **all** values match |
||
| 107 | # with the analysis' values |
||
| 108 | for spec in specs: |
||
| 109 | skip = False |
||
| 110 | |||
| 111 | for k, v in match_data.items(): |
||
| 112 | # break if the values do not match |
||
| 113 | if v != spec[k]: |
||
| 114 | skip = True |
||
| 115 | break |
||
| 116 | |||
| 117 | # skip the whole specification row |
||
| 118 | if skip: |
||
| 119 | continue |
||
| 120 | |||
| 121 | # at this point we have a match, update the results range dict |
||
| 122 | for key in self.range_keys: |
||
| 123 | value = spec.get(key, marker) |
||
| 124 | # skip if the range key is not set in the Excel |
||
| 125 | if value is marker: |
||
| 126 | continue |
||
| 127 | # skip if the value is not floatable |
||
| 128 | if not api.is_floatable(value): |
||
| 129 | continue |
||
| 130 | # set the range value |
||
| 131 | rr[key] = value |
||
| 132 | # return the updated result range |
||
| 133 | return rr |
||
| 134 | |||
| 135 | return rr |
||
| 136 | |||
| 139 |