-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
76 lines (52 loc) · 1.84 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import arrow
from statistics import mode
degree_sign = u'\N{DEGREE SIGN}'
def unix_to_dt(unix):
timestamp = arrow.get(unix)
return timestamp.to('Europe/London')
def time_from_dt(dt_object):
return arrow.Arrow.time(dt_object)
def increment_day_dt(dt_object, days):
return dt_object.shift(days=days)
def temp_desc(forecast):
values = []
for forecast_time in forecast:
values.append(forecast_time['main']['temp'])
return f"{kelvin_to_cels(min(values))} - {kelvin_to_cels(max(values))}{degree_sign}C"
def icon_desc(forecast):
values = []
for forecast_time in forecast:
values.append(forecast_time['weather'][0]['icon'])
return mode(values)
def weather_desc(forecast):
value = {}
ret_list = []
ret_str = ''
for forecast_time in forecast:
desc = forecast_time['weather'][0]['description']
time_ = time_from_dt(unix_to_dt(forecast_time['dt']))
time_string = time_.strftime("%H:%M")
value[time_string] = desc
for i, (t, description) in enumerate(value.items()):
if not ret_list:
ret_list.append((t, description))
continue
if description in ret_list[-1] and i < (len(list(value.keys()))-1):
continue
else:
ret_list.append((t, description))
for time, descr in ret_list:
ret_str += f'<p>{time}: {descr}</p>'
return ret_str
def kelvin_to_cels(K_temp):
cels = K_temp - 273.15
return round(cels, 1)
def m_s_to_mph(ms_speed):
return round((ms_speed * 2.12585), 1)
def wind_desc(forecast):
speed_values = []
gust_values = []
for forecast_time in forecast:
speed_values.append(forecast_time['wind']['speed'])
gust_values.append(forecast_time['wind']['gust'])
return f"{m_s_to_mph(min(speed_values))} - {m_s_to_mph(max(speed_values))} mph"