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