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