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