Conditions | 8 |
Total Lines | 68 |
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.""" |
||
176 | def meteo_data_from_json(station_id: int, data: dict) -> MeteorologicalData: |
||
177 | """Create a MeteorologicalData object from a JSON object.""" |
||
178 | dt = datetime.datetime.fromisoformat(data[API_DATETIME]) |
||
179 | dt, is_dst = _fix_datetime_offset(dt) |
||
180 | |||
181 | channel_value_dict = {} |
||
182 | for channel_value in data[API_CHANNELS]: |
||
183 | if channel_value[API_VALID] is True and channel_value[API_STATUS] == 1: |
||
184 | channel_value_dict[channel_value[API_NAME]] = float( |
||
185 | channel_value[API_VALUE] |
||
186 | ) |
||
187 | |||
188 | rain = channel_value_dict.get(API_RAIN) |
||
189 | ws_max = channel_value_dict.get(API_WS_MAX) |
||
190 | wd_max = channel_value_dict.get(API_WD_MAX) |
||
191 | ws = channel_value_dict.get(API_WS) |
||
192 | wd = channel_value_dict.get(API_WD) |
||
193 | std_wd = channel_value_dict.get(API_STD_WD) |
||
194 | td = channel_value_dict.get(API_TD) |
||
195 | rh = channel_value_dict.get(API_RH) |
||
196 | td_max = channel_value_dict.get(API_TD_MAX) |
||
197 | td_min = channel_value_dict.get(API_TD_MIN) |
||
198 | ws_1mm = channel_value_dict.get(API_WS_1MM) |
||
199 | ws_10mm = channel_value_dict.get(API_WS_10MM) |
||
200 | tg = channel_value_dict.get(API_TG) |
||
201 | tw = channel_value_dict.get(API_TW) |
||
202 | time_val = channel_value_dict.get(API_TIME) |
||
203 | if time_val: |
||
204 | time_int = int(time_val) |
||
205 | if time_int <= MAX_HOUR_INT: |
||
206 | t = time.strptime(str(time_int), "%H") |
||
207 | else : |
||
208 | t = time.strptime(str(time_int), "%H%M") |
||
209 | time_val = datetime.time(t.tm_hour, t.tm_min, tzinfo=tz) |
||
210 | bp = channel_value_dict.get(API_BP) |
||
211 | diff_r = channel_value_dict.get(API_DIFF) |
||
212 | grad = channel_value_dict.get(API_GRAD) |
||
213 | nip = channel_value_dict.get(API_NIP) |
||
214 | rain_1_min = channel_value_dict.get(API_RAIN_1_MIN) |
||
215 | |||
216 | if is_dst and time_val: |
||
217 | # Strange IMS logic :o |
||
218 | dt = dt + datetime.timedelta(hours=1) |
||
219 | time_val = time_val.replace(hour=(time_val.hour+1)%24) |
||
220 | |||
221 | return MeteorologicalData( |
||
222 | station_id=station_id, |
||
223 | datetime=dt, |
||
224 | rain=rain, |
||
225 | ws=ws, |
||
226 | ws_max=ws_max, |
||
227 | wd=wd, |
||
228 | wd_max=wd_max, |
||
229 | std_wd=std_wd, |
||
230 | td=td, |
||
231 | td_max=td_max, |
||
232 | td_min=td_min, |
||
233 | tg=tg, |
||
234 | tw=tw, |
||
235 | rh=rh, |
||
236 | ws_1mm=ws_1mm, |
||
237 | ws_10mm=ws_10mm, |
||
238 | time=time_val, |
||
239 | bp=bp, |
||
240 | diff_r=diff_r, |
||
241 | grad=grad, |
||
242 | nip=nip, |
||
243 | rain_1_min=rain_1_min |
||
244 | ) |
||
254 |