| Conditions | 5 |
| Total Lines | 78 |
| Code Lines | 48 |
| Lines | 12 |
| Ratio | 15.38 % |
| 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:
| 1 | """ |
||
| 85 | def evaluate_df(self, df, ctx): |
||
| 86 | """ |
||
| 87 | Evaluate heat demand comparison. |
||
| 88 | |||
| 89 | Parameters |
||
| 90 | ---------- |
||
| 91 | df : pd.DataFrame |
||
| 92 | DataFrame with output_demand_twh and input_demand_twh columns |
||
| 93 | ctx : dict |
||
| 94 | Context information |
||
| 95 | |||
| 96 | Returns |
||
| 97 | ------- |
||
| 98 | RuleResult |
||
| 99 | Validation result with success/failure status |
||
| 100 | """ |
||
| 101 | View Code Duplication | if df.empty or df["output_demand_twh"].isna().all() or df["input_demand_twh"].isna().all(): |
|
|
|
|||
| 102 | return RuleResult( |
||
| 103 | rule_id=self.rule_id, |
||
| 104 | task=self.task, |
||
| 105 | table=self.table, |
||
| 106 | kind=self.kind, |
||
| 107 | success=False, |
||
| 108 | message=f"No heat demand data found for {self.scenario}", |
||
| 109 | severity=Severity.ERROR, |
||
| 110 | schema=self.schema, |
||
| 111 | table_name=self.table_name, |
||
| 112 | rule_class=self.__class__.__name__ |
||
| 113 | ) |
||
| 114 | |||
| 115 | output_twh = float(df["output_demand_twh"].values[0]) |
||
| 116 | input_twh = float(df["input_demand_twh"].values[0]) |
||
| 117 | |||
| 118 | # Calculate deviation |
||
| 119 | deviation = abs(output_twh - input_twh) / input_twh |
||
| 120 | deviation_pct = deviation * 100 |
||
| 121 | diff_twh = output_twh - input_twh |
||
| 122 | |||
| 123 | success = deviation <= self.rtol |
||
| 124 | |||
| 125 | if success: |
||
| 126 | return RuleResult( |
||
| 127 | rule_id=self.rule_id, |
||
| 128 | task=self.task, |
||
| 129 | table=self.table, |
||
| 130 | kind=self.kind, |
||
| 131 | success=True, |
||
| 132 | observed=output_twh, |
||
| 133 | expected=input_twh, |
||
| 134 | message=( |
||
| 135 | f"Heat demand valid for {self.scenario}: " |
||
| 136 | f"{output_twh:.2f} TWh vs {input_twh:.2f} TWh expected " |
||
| 137 | f"(deviation: {deviation_pct:.2f}%, tolerance: {self.rtol*100:.2f}%)" |
||
| 138 | ), |
||
| 139 | severity=Severity.INFO, |
||
| 140 | schema=self.schema, |
||
| 141 | table_name=self.table_name, |
||
| 142 | rule_class=self.__class__.__name__ |
||
| 143 | ) |
||
| 144 | else: |
||
| 145 | return RuleResult( |
||
| 146 | rule_id=self.rule_id, |
||
| 147 | task=self.task, |
||
| 148 | table=self.table, |
||
| 149 | kind=self.kind, |
||
| 150 | success=False, |
||
| 151 | observed=output_twh, |
||
| 152 | expected=input_twh, |
||
| 153 | message=( |
||
| 154 | f"Heat demand deviation too large for {self.scenario}: " |
||
| 155 | f"{output_twh:.2f} TWh vs {input_twh:.2f} TWh expected " |
||
| 156 | f"(diff: {diff_twh:+.2f} TWh, deviation: {deviation_pct:.2f}%, " |
||
| 157 | f"tolerance: {self.rtol*100:.2f}%)" |
||
| 158 | ), |
||
| 159 | severity=Severity.ERROR, |
||
| 160 | schema=self.schema, |
||
| 161 | table_name=self.table_name, |
||
| 162 | rule_class=self.__class__.__name__ |
||
| 163 | ) |
||
| 164 |