-
Notifications
You must be signed in to change notification settings - Fork 0
/
Weather.py
181 lines (140 loc) · 5.45 KB
/
Weather.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
from ForecastReader import JsonForecastReader
from collections import OrderedDict
##################################################
# Static Variables
# Day keys
TODAY = 'today'
DAY_2 = 'day2'
DAY_3 = 'day3'
DAY_4 = 'day4'
DAY_5 = 'day5'
# Weather list index
TODAY_END = 5
DAY_LIST_NUM = 8
DAY_2_END = TODAY_END + DAY_LIST_NUM
DAY_3_END = DAY_2_END + DAY_LIST_NUM
DAY_4_END = DAY_3_END + DAY_LIST_NUM
DAY_5_END = DAY_4_END + DAY_LIST_NUM
# Weather list keys
DATE_TIME_KEY = 'dt_txt'
WEATHER_KEY = 'weather'
RAIN_KEY = 'rain'
MAIN_TEMP_KEY = 'main'
# All keys for specific temperatures
TEMP_KEY = 'temp'
MAX_TEMP_KEY = 'temp_max'
MIN_TEMP_KEY = 'temp_min'
# Weather dict key
WEATHER_DESCRIPTION_KEY = 'description'
# Initial Min and Max Temp
INIT_MIN_TEMP = 99999
INIT_MAX_TEMP = -99999
#####################################################
# Class Definitions
class Weather:
""" Class representing weather objects
"""
def __init__(self, city_name, country_code="us", unit="imperial"):
self.forecast_reader = JsonForecastReader(city_name, country_code, unit)
self.weather_list = self.forecast_reader.getWeatherList()
#print(self.weather_list[0])
self.days_dict = self.get_days_dict()
def get_days_dict(self):
""" get days dict
"""
days_dict = OrderedDict()
days_dict[TODAY] = Day(self.weather_list[:TODAY_END])
days_dict[DAY_2] = Day(self.weather_list[TODAY_END:DAY_2_END])
days_dict[DAY_3] = Day(self.weather_list[DAY_2_END:DAY_3_END])
days_dict[DAY_4] = Day(self.weather_list[DAY_3_END:DAY_4_END])
days_dict[DAY_5] = Day(self.weather_list[DAY_4_END:DAY_5_END])
return days_dict
def get_day_from_string(self, day_string):
return self.days_dict[day_string]
def get_day_max_temp_and_time(self, day_string):
day = self.get_day_from_string(day_string)
return day.get_max_temp_and_time()
def get_day_min_temp_and_time(self, day_string):
day = self.get_day_from_string(day_string)
return day.get_min_temp_and_time()
def get_day_rain_description_and_time(self, day_string):
day = self.get_day_from_string(day_string)
return day.get_rain_description_and_time_tuple_list()
def get_time_weather_description(self, day_string, time_string):
day = self.get_day_from_string(day_string)
return day.get_time_object(time_string).get_weather_description()
def get_time_max_temp(self, day_string, time_string):
day = self.get_day_from_string(day_string)
return day.get_time_object(time_string).get_max_temp()
def get_time_min_temp(self, day_string, time_string):
day = self.get_day_from_string(day_string)
return day.get_time_object(time_string).get_min_temp()
class Day:
""" Class representing the Day
"""
def __init__(self, day_data_list):
self.date = self.get_data_date(day_data_list[0])
self.day_data_list = day_data_list
self.times_dict = self.get_times_dict()
def get_date(self):
return self.date
def get_time_object(self, time):
return self.times_dict[time]
@staticmethod
def get_data_date(data_dict):
return data_dict.get(DATE_TIME_KEY)[0:10]
@staticmethod
def get_data_time(data_dict):
return data_dict.get(DATE_TIME_KEY)[11:]
def get_times_dict(self):
times_dict = OrderedDict()
for time_data_dict in self.day_data_list:
#print(self.get_data_time(time_data_dict))
#print(self.get_data_date(time_data_dict), 'vs', self.date)
assert self.get_data_date(time_data_dict) == self.date, "Can't have different dates yo!"
times_dict[self.get_data_time(time_data_dict)] = Time(time_data_dict)
return times_dict
def get_max_temp_and_time(self):
max_temp = INIT_MAX_TEMP
max_temp_time = None
for time in self.times_dict.keys():
temp = self.get_time_object(time).get_temp()
if temp >= max_temp:
max_temp = temp
max_temp_time = time
return max_temp, max_temp_time
def get_min_temp_and_time(self):
min_temp = INIT_MIN_TEMP
min_temp_time = None
for time in self.times_dict.keys():
temp = self.get_time_object(time).get_temp()
if temp <= min_temp:
min_temp = temp
min_temp_time = time
return min_temp, min_temp_time
def get_rain_description_and_time_tuple_list(self):
lst = []
for time in self.times_dict.keys():
weather_des = self.get_time_object(time).get_weather_description()
if "rain" in weather_des:
lst.append((weather_des, time))
return lst
class Time:
""" Class
"""
def __init__(self, time_data_dict):
self.time_data_dict = time_data_dict
self.time = self.get_data_time()
def get_time(self):
return self.time
def get_data_time(self):
return self.time_data_dict.get(DATE_TIME_KEY)[11:]
def get_temp(self):
return self.time_data_dict.get(MAIN_TEMP_KEY).get(TEMP_KEY)
def get_max_temp(self):
return self.time_data_dict.get(MAIN_TEMP_KEY).get(MAX_TEMP_KEY)
def get_min_temp(self):
return self.time_data_dict.get(MAIN_TEMP_KEY).get(MIN_TEMP_KEY)
def get_weather_description(self):
return self.time_data_dict.get(WEATHER_KEY)[0].get(WEATHER_DESCRIPTION_KEY)
#get weather status