| Conditions | 24 |
| Total Lines | 138 |
| Code Lines | 63 |
| 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.api.analysis.is_out_of_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 -*- |
||
| 40 | def is_out_of_range(brain_or_object, result=_marker): |
||
| 41 | """Checks if the result for the analysis passed in is out of range and/or |
||
| 42 | out of shoulders range. |
||
| 43 | |||
| 44 | min max |
||
| 45 | warn min max warn |
||
| 46 | ·········|---------------|=====================|---------------|········· |
||
| 47 | ----- out-of-range -----><----- in-range ------><----- out-of-range ----- |
||
| 48 | <-- shoulder --><----- in-range ------><-- shoulder --> |
||
| 49 | |||
| 50 | :param brain_or_object: A single catalog brain or content object |
||
| 51 | :param result: Tentative result. If None, use the analysis result |
||
| 52 | :type brain_or_object: ATContentType/DexterityContentType/CatalogBrain |
||
| 53 | :returns: Tuple of two elements. The first value is `True` if the result is |
||
| 54 | out of range and `False` if it is in range. The second value is `True` if |
||
| 55 | the result is out of shoulder range and `False` if it is in shoulder range |
||
| 56 | :rtype: (bool, bool) |
||
| 57 | """ |
||
| 58 | analysis = api.get_object(brain_or_object) |
||
| 59 | if not IAnalysis.providedBy(analysis) and \ |
||
| 60 | not IReferenceAnalysis.providedBy(analysis): |
||
| 61 | api.fail("{} is not supported. Needs to be IAnalysis or " |
||
| 62 | "IReferenceAnalysis".format(repr(analysis))) |
||
| 63 | |||
| 64 | if result is _marker: |
||
| 65 | result = api.safe_getattr(analysis, "getResult", None) |
||
| 66 | |||
| 67 | if result in [None, '']: |
||
| 68 | # Empty result |
||
| 69 | return False, False |
||
| 70 | |||
| 71 | if IDuplicateAnalysis.providedBy(analysis): |
||
| 72 | # Result range for duplicate analyses is calculated from the original |
||
| 73 | # result, applying a variation % in shoulders. If the analysis has |
||
| 74 | # result options enabled or string results enabled, system returns an |
||
| 75 | # empty result range for the duplicate: result must match %100 with the |
||
| 76 | # original result |
||
| 77 | original = analysis.getAnalysis() |
||
| 78 | original_result = original.getResult() |
||
| 79 | |||
| 80 | # Does original analysis have a valid result? |
||
| 81 | if original_result in [None, '']: |
||
| 82 | return False, False |
||
| 83 | |||
| 84 | # Does original result type matches with duplicate result type? |
||
| 85 | if api.is_floatable(result) != api.is_floatable(original_result): |
||
| 86 | return True, True |
||
| 87 | |||
| 88 | # Does analysis has result options enabled or non-floatable? |
||
| 89 | if analysis.getResultOptions() or not api.is_floatable(original_result): |
||
| 90 | # Let's always assume the result is 'out from shoulders', cause we |
||
| 91 | # consider the shoulders are precisely the duplicate variation % |
||
| 92 | out_of_range = original_result != result |
||
| 93 | return out_of_range, out_of_range |
||
| 94 | |||
| 95 | elif not api.is_floatable(result): |
||
| 96 | results = api.parse_json(result) |
||
| 97 | if not results: |
||
| 98 | # Single, non-duplicate, non-floatable result. There is no chance |
||
| 99 | # to know if the result is out-of-range |
||
| 100 | return False, False |
||
| 101 | |||
| 102 | # Multiselect result, remove empty and non-floatable 'sub' results |
||
| 103 | results = filter(api.is_floatable, results) |
||
| 104 | if not results: |
||
| 105 | # No values set yet, we cannot know if out-of-range yet |
||
| 106 | return False, False |
||
| 107 | |||
| 108 | # Out of range only when none of the 'sub' results are within range |
||
| 109 | for sub_result in results: |
||
| 110 | out_range, out_shoulders = is_out_of_range(analysis, sub_result) |
||
| 111 | if not out_range: |
||
| 112 | # sub result within range |
||
| 113 | return False, False |
||
| 114 | |||
| 115 | # None of the 'sub' results are within range |
||
| 116 | return True, True |
||
| 117 | |||
| 118 | # Convert result to a float |
||
| 119 | result = api.to_float(result) |
||
| 120 | |||
| 121 | # Note that routine analyses, duplicates and reference analyses all them |
||
| 122 | # implement the function getResultRange: |
||
| 123 | # - For routine analyses, the function returns the valid range based on the |
||
| 124 | # specs assigned during the creation process. |
||
| 125 | # - For duplicates, the valid range is the result of the analysis the |
||
| 126 | # the duplicate was generated from +/- the duplicate variation. |
||
| 127 | # - For reference analyses, getResultRange returns the valid range as |
||
| 128 | # indicated in the Reference Sample from which the analysis was created. |
||
| 129 | result_range = api.safe_getattr(analysis, "getResultsRange", None) |
||
| 130 | if not result_range: |
||
| 131 | # No result range defined or the passed in object does not suit |
||
| 132 | return False, False |
||
| 133 | |||
| 134 | # Maybe there is a custom adapter |
||
| 135 | adapters = getAdapters((analysis,), IResultOutOfRange) |
||
| 136 | for name, adapter in adapters: |
||
| 137 | ret = adapter(result=result, specification=result_range) |
||
| 138 | if not ret or not ret.get('out_of_range', False): |
||
| 139 | continue |
||
| 140 | if not ret.get('acceptable', True): |
||
| 141 | # Out of range + out of shoulders |
||
| 142 | return True, True |
||
| 143 | # Out of range, but in shoulders |
||
| 144 | return True, False |
||
| 145 | |||
| 146 | result_range = ResultsRangeDict(result_range) |
||
| 147 | |||
| 148 | # The assignment of result as default fallback for min and max guarantees |
||
| 149 | # the result will be in range also if no min/max values are defined |
||
| 150 | specs_min = api.to_float(result_range.min, result) |
||
| 151 | specs_max = api.to_float(result_range.max, result) |
||
| 152 | |||
| 153 | in_range = False |
||
| 154 | min_operator = result_range.min_operator |
||
| 155 | if min_operator == "geq": |
||
| 156 | in_range = result >= specs_min |
||
| 157 | else: |
||
| 158 | in_range = result > specs_min |
||
| 159 | |||
| 160 | max_operator = result_range.max_operator |
||
| 161 | if in_range: |
||
| 162 | if max_operator == "leq": |
||
| 163 | in_range = result <= specs_max |
||
| 164 | else: |
||
| 165 | in_range = result < specs_max |
||
| 166 | |||
| 167 | # If in range, no need to check shoulders |
||
| 168 | if in_range: |
||
| 169 | return False, False |
||
| 170 | |||
| 171 | # Out of range, check shoulders. If no explicit warn_min or warn_max have |
||
| 172 | # been defined, no shoulders must be considered for this analysis. Thus, use |
||
| 173 | # specs' min and max as default fallback values |
||
| 174 | warn_min = api.to_float(result_range.warn_min, specs_min) |
||
| 175 | warn_max = api.to_float(result_range.warn_max, specs_max) |
||
| 176 | in_shoulder = warn_min <= result <= warn_max |
||
| 177 | return True, not in_shoulder |
||
| 178 | |||
| 414 |