| Conditions | 7 |
| Total Lines | 70 |
| Code Lines | 60 |
| 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:
| 1 | """Data Class for IMS Meteorological Readings.""" |
||
| 153 | def meteo_data_from_json(station_id: int, data: dict) -> MeteorologicalData: |
||
| 154 | """Create a MeteorologicalData object from a JSON object.""" |
||
| 155 | tz = pytz.timezone("Asia/Jerusalem") |
||
| 156 | is_dst = bool(time.localtime(time.time()).tm_isdst) |
||
| 157 | |||
| 158 | dt = datetime.datetime.fromisoformat(data[API_DATETIME]) |
||
| 159 | dt.replace(tzinfo=tz) |
||
| 160 | if is_dst: |
||
| 161 | # Strange IMS logic :o |
||
| 162 | dt = dt + datetime.timedelta(hours=1) |
||
| 163 | |||
| 164 | channel_value_dict = {} |
||
| 165 | for channel_value in data[API_CHANNELS]: |
||
| 166 | if channel_value[API_VALID] is True and channel_value[API_STATUS] == 1: |
||
| 167 | channel_value_dict[channel_value[API_NAME]] = float( |
||
| 168 | channel_value[API_VALUE] |
||
| 169 | ) |
||
| 170 | |||
| 171 | rain = channel_value_dict.get(API_RAIN) |
||
| 172 | ws_max = channel_value_dict.get(API_WS_MAX) |
||
| 173 | wd_max = channel_value_dict.get(API_WD_MAX) |
||
| 174 | ws = channel_value_dict.get(API_WS) |
||
| 175 | wd = channel_value_dict.get(API_WD) |
||
| 176 | std_wd = channel_value_dict.get(API_STD_WD) |
||
| 177 | td = channel_value_dict.get(API_TD) |
||
| 178 | rh = channel_value_dict.get(API_RH) |
||
| 179 | td_max = channel_value_dict.get(API_TD_MAX) |
||
| 180 | td_min = channel_value_dict.get(API_TD_MIN) |
||
| 181 | ws_1mm = channel_value_dict.get(API_WS_1MM) |
||
| 182 | ws_10mm = channel_value_dict.get(API_WS_10MM) |
||
| 183 | tg = channel_value_dict.get(API_TG) |
||
| 184 | tw = channel_value_dict.get(API_TW) |
||
| 185 | time_val = channel_value_dict.get(API_TIME) |
||
| 186 | if time_val: |
||
| 187 | t = time.strptime(str(int(time_val)), "%H%M") |
||
| 188 | time_val = datetime.time(t.tm_hour, t.tm_min, tzinfo=tz) |
||
| 189 | if is_dst: |
||
| 190 | # Strange IMS logic, everything is GMT+2 |
||
| 191 | time_val = time_val.replace(hour=(time_val.hour+1)%24) |
||
| 192 | bp = channel_value_dict.get(API_BP) |
||
| 193 | diff_r = channel_value_dict.get(API_DIFF) |
||
| 194 | grad = channel_value_dict.get(API_GRAD) |
||
| 195 | nip = channel_value_dict.get(API_NIP) |
||
| 196 | rain_1_min = channel_value_dict.get(API_RAIN_1_MIN) |
||
| 197 | |||
| 198 | |||
| 199 | |||
| 200 | return MeteorologicalData( |
||
| 201 | station_id=station_id, |
||
| 202 | datetime=dt, |
||
| 203 | rain=rain, |
||
| 204 | ws=ws, |
||
| 205 | ws_max=ws_max, |
||
| 206 | wd=wd, |
||
| 207 | wd_max=wd_max, |
||
| 208 | std_wd=std_wd, |
||
| 209 | td=td, |
||
| 210 | td_max=td_max, |
||
| 211 | td_min=td_min, |
||
| 212 | tg=tg, |
||
| 213 | tw=tw, |
||
| 214 | rh=rh, |
||
| 215 | ws_1mm=ws_1mm, |
||
| 216 | ws_10mm=ws_10mm, |
||
| 217 | time=time_val, |
||
| 218 | bp=bp, |
||
| 219 | diff_r=diff_r, |
||
| 220 | grad=grad, |
||
| 221 | nip=nip, |
||
| 222 | rain_1_min=rain_1_min |
||
| 223 | ) |
||
| 233 |