Conditions | 5 |
Total Lines | 54 |
Code Lines | 50 |
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.""" |
||
149 | def meteo_data_from_json(station_id: int, data: dict) -> MeteorologicalData: |
||
150 | """Create a MeteorologicalData object from a JSON object.""" |
||
151 | dt = datetime.fromisoformat(data[API_DATETIME]) |
||
152 | channel_value_dict = {} |
||
153 | for channel_value in data[API_CHANNELS]: |
||
154 | if channel_value[API_VALID] is True and channel_value[API_STATUS] == 1: |
||
155 | channel_value_dict[channel_value[API_NAME]] = float( |
||
156 | channel_value[API_VALUE] |
||
157 | ) |
||
158 | |||
159 | rain = channel_value_dict.get(API_RAIN) |
||
160 | ws_max = channel_value_dict.get(API_WS_MAX) |
||
161 | wd_max = channel_value_dict.get(API_WD_MAX) |
||
162 | ws = channel_value_dict.get(API_WS) |
||
163 | wd = channel_value_dict.get(API_WD) |
||
164 | std_wd = channel_value_dict.get(API_STD_WD) |
||
165 | td = channel_value_dict.get(API_TD) |
||
166 | rh = channel_value_dict.get(API_RH) |
||
167 | td_max = channel_value_dict.get(API_TD_MAX) |
||
168 | td_min = channel_value_dict.get(API_TD_MIN) |
||
169 | ws_1mm = channel_value_dict.get(API_WS_1MM) |
||
170 | ws_10mm = channel_value_dict.get(API_WS_10MM) |
||
171 | tg = channel_value_dict.get(API_TG) |
||
172 | tw = channel_value_dict.get(API_TW) |
||
173 | time_val = channel_value_dict.get(API_TIME) |
||
174 | if time_val: |
||
175 | time_val = time.strptime(str(int(time_val)), "%H%M") |
||
176 | bp = channel_value_dict.get(API_BP) |
||
177 | diff_r = channel_value_dict.get(API_DIFF) |
||
178 | grad = channel_value_dict.get(API_GRAD) |
||
179 | nip = channel_value_dict.get(API_NIP) |
||
180 | |||
181 | return MeteorologicalData( |
||
182 | station_id=station_id, |
||
183 | datetime=dt, |
||
184 | rain=rain, |
||
185 | ws=ws, |
||
186 | ws_max=ws_max, |
||
187 | wd=wd, |
||
188 | wd_max=wd_max, |
||
189 | std_wd=std_wd, |
||
190 | td=td, |
||
191 | td_max=td_max, |
||
192 | td_min=td_min, |
||
193 | tg=tg, |
||
194 | tw=tw, |
||
195 | rh=rh, |
||
196 | ws_1mm=ws_1mm, |
||
197 | ws_10mm=ws_10mm, |
||
198 | time=time_val, |
||
199 | bp=bp, |
||
200 | diff_r=diff_r, |
||
201 | grad=grad, |
||
202 | nip=nip |
||
203 | ) |
||
213 |