-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
333 lines (310 loc) · 15.1 KB
/
main.py
File metadata and controls
333 lines (310 loc) · 15.1 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import json
from datetime import datetime
from datetime import timedelta
from dataclasses import dataclass
import requests
## Global Variable Declaration:###
weatherDeCoded = [] ##
holidayObjectsList = [] ##
dictOfHolidays = {} ##
deleted = 0 ##
##################################
def deleteprint(decoratedfunc):
def inner_fn(*args, **kwargs):
print("Deleting the %s from the stored holidays" % str(args[1]))
evaluated = decoratedfunc(*args, **kwargs)
return evaluated
return inner_fn
## Each input checker uses a try-except pair to avoid program errors due to invalid inputs.
## User-input authenticators##########################################
##
def inputyn(string): ##
invalidInput = True ##
while invalidInput: ##
try: ##
userinput = str(input(string)) ##
if userinput == 'y' or userinput == 'n': ##
invalidInput = False ##
else: ##
print("Invalid input: Not in the form of y or n!") ##
except: ##
print("Invalid input: Not a string") ##
return userinput ##
######################################################################
def inputint(string): ##
invalidInput = True ##
while invalidInput: ##
try: ##
userinput = int(input(string)) ##
invalidInput = False ##
except: ##
print("Invalid input: Not a integer") ##
return userinput ##
######################################################################
def inputstr(string): ##
invalidInput = True ##
while invalidInput: ##
try: ##
userinput = str(input(string)) ##
invalidInput = False ##
except: ##
print("Invalid input: Not a string") ##
return userinput ##
######################################################################
## Creates the class object "Holiday" that is used to store each holiday as object.
@dataclass
class Holidays:
holidayName: str
holidayDate: str
@property
def holidayname(self):
return self.holidayName
@property
def holidaydate(self):
return self.holidayDate
def __str__(self):
return self.holidayName + " " + str(self.holidayDate)
## Used to load in the initial holidayStarter.json file as save it as a dictionary of holidays.
def openInitialFile():
global dictOfHolidays
with open(r"C:\Users\Eddy Doering\PycharmProjects\Assessment7\venv\holidaysStarter.json") as infile1:
dictOfHolidays = json.load(infile1)
## Used to load in the scrapped data from the HolidayScraper.py program and store merge it with the initial file.
def openScrapedFile():
global dictOfHolidays
with open(r"C:\Users\Eddy Doering\PycharmProjects\Assessment7\venv\HolidaysScraped.json") as infile2:
jsonFile2 = json.load(infile2)
dictOfHolidays['holidays'].extend(jsonFile2)
# dictOfHolidays = json.dumps(dictOfHolidays, indent=4)
def initilizeholidays():
global holidayObjectsList
holidayObjectsList = []
holidayObjects = dictOfHolidays["holidays"]
for index in range(len(holidayObjects)):
holidayObjectsList.insert(index, 'holidayItem' + str(index))
# print(holidayObjectsList[index])
for key in holidayObjects[index]:
holidayObjectsList[index] = Holidays(holidayObjects[index]["name"], holidayObjects[index]["date"])
# print (holidayObjects[index][key])
# print(holidayObjectsList[0].holidayName)
# print(holidayObjectsList[0].holidayDate)
# print(holidayObjectsList)
def findobject(find):
for index in range(len(holidayObjectsList)):
if holidayObjectsList[index].holidaydate == find:
found = holidayObjectsList[index].holidayname
return found
def apicall():
global weatherDeCoded
response = requests.get('https://api.tomorrow.io/v4/timelines?location=44.986656,-93.258133&fields=weatherCode×teps=1d&units=metric&apikey=iNmVrPeULGBSY7hAG8r90w9oA4lzIPEz')
weatherDic = response.json()
weatherCode = []
for i in range(len(weatherDic['data']['timelines'][0]['intervals'])):
weatherCode.append(weatherDic['data']['timelines'][0]['intervals'][i]['values']['weatherCode'])
for i in range(len(weatherCode)):
if weatherCode[i] == 0:
weatherDeCoded.insert(i, "unknown")
if weatherCode[i] == 1000:
weatherDeCoded.insert(i, "clear")
if weatherCode[i] == 1001:
weatherDeCoded.insert(i, "cloudy")
if weatherCode[i] == 1100:
weatherDeCoded.insert(i, "mostly clear")
if weatherCode[i] == 1101:
weatherDeCoded.insert(i, "partially cloudy")
if weatherCode[i] == 1102:
weatherDeCoded.insert(i, "mostly cloudy")
if weatherCode[i] == 2000:
weatherDeCoded.insert(i, "fog")
if weatherCode[i] == 2100:
weatherDeCoded.insert(i, "light fog")
if weatherCode[i] == 3000:
weatherDeCoded.insert(i, "breezy")
if weatherCode[i] == 3001:
weatherDeCoded.insert(i, "windy")
if weatherCode[i] == 3002:
weatherDeCoded.insert(i, "strong wind")
if weatherCode[i] == 4000:
weatherDeCoded.insert(i, "drizzle")
if weatherCode[i] == 4001:
weatherDeCoded.insert(i, "rain")
if weatherCode[i] == 4200:
weatherDeCoded.insert(i, "light rain")
if weatherCode[i] == 4201:
weatherDeCoded.insert(i, "heavy rain")
if weatherCode[i] == 5000:
weatherDeCoded.insert(i, "snow")
if weatherCode[i] == 5001:
weatherDeCoded.insert(i, "flurries")
if weatherCode[i] == 5100:
weatherDeCoded.insert(i, "light snow")
if weatherCode[i] == 5101:
weatherDeCoded.insert(i, "heavy snow")
if weatherCode[i] == 6000:
weatherDeCoded.insert(i, "freezing drizzle")
if weatherCode[i] == 6001:
weatherDeCoded.insert(i, "freezing rain")
if weatherCode[i] == 6200:
weatherDeCoded.insert(i, "light freezing rain")
if weatherCode[i] == 6201:
weatherDeCoded.insert(i, "heavy freezing rain")
if weatherCode[i] == 7000:
weatherDeCoded.insert(i, "ice pellets")
if weatherCode[i] == 7101:
weatherDeCoded.insert(i, "heavy ice pellets")
if weatherCode[i] == 7102:
weatherDeCoded.insert(i, "light ice pellets")
if weatherCode[i] == 8000:
weatherDeCoded.insert(i, "thunderstorm")
def startup():
print('''
--------------------------------------------------------|
HOLIDAY MANAGEMENT SYSTEM (HMS 1.1) |
--------------------------------------------------------|
''')
print("Currently there are", len(holidayObjectsList), "holidays stored in the system")
def mainmenu():
print('''
--------------------------------------------------------|
MAIN HOLIDAY MENU |
--------------------------------------------------------|
1. add a holiday
2. remove a holiday
3. save a holiday list
4. view holidays
5. exit
''')
print("Currently there are", len(holidayObjectsList), "holidays stored in the system")
def holidayadd():
print('''
--------------------------------------------------------|
ADD HOLIDAY |
--------------------------------------------------------|
''')
newHolidayName = inputstr("Enter holiday name: ")
invalidDate = True
while invalidDate:
newHolidayDate = inputstr("enter date in format yyyy-mm-dd: ")
try:
x = datetime.strptime(newHolidayDate, '%Y-%m-%d')
invalidDate = False
except:
print("Invalid input: not in format yyyy-mm-dd, please try again")
index = len(holidayObjectsList)
newHoliday = Holidays(newHolidayName, newHolidayDate)
newHolidayList = [newHoliday]
holidayObjectsList.extend(newHolidayList)
print("Success, the holiday: ", holidayObjectsList[index].holidayName, " has been added")
@deleteprint
def deleteobject(holidayObjectList,removeHoliday):
global holidayObjectsList
deletedObject = False
for i in range(len(holidayObjectsList)):
if holidayObjectsList[i].holidayName.lower() == removeHoliday:
holidayObjectsList.pop(i)
deletedObject = True
print("Deletion Successful!")
break
if deletedObject is False:
print(removeHoliday, " was not found, try a different input or check the list for correct spelling")
#return holidayObjectList
def holidayremove():
print('''
--------------------------------------------------------|
REMOVE HOLIDAY |
--------------------------------------------------------|
''')
removeHoliday = inputstr("Select Holiday to be removed: ").lower()
deleteobject(holidayObjectsList,removeHoliday)
def listsave():
print("Saving holiday list to JSON file.")
listDict = []
for i in range(len(holidayObjectsList)):
dict = {}
dict["name"] = holidayObjectsList[i].holidayName
dict["date"] = holidayObjectsList[i].holidayDate
listDict.append(dict)
listJSON = json.dumps(listDict, indent=4)
#print(listJSON)
with open(r"C:\Users\Eddy Doering\PycharmProjects\Assessment7\SavedHolidaysList.json", 'w') as outfile:
outfile.write(listJSON)
def holidayview():
global holidayObjectsList
print('''
--------------------------------------------------------|
VIEW HOLIDAYS |
--------------------------------------------------------|
''')
invalidSelectView = True
while invalidSelectView:
today = datetime.today().isocalendar()[1]
selectYear = inputint("Which year: ")
print("todays week is: ", today+1, "enter this for weather forecast")
selectWeek = inputint("Which week? [1-52]: ")
if 1 <= selectWeek <= 52 and 2021 <= selectYear <= 2024:
invalidSelectView = False
else:
print("Invalid input: Please select a year between 2021 and 2024 and a week between 1 and 52")
if selectWeek == today:
apicall()
# holidayWeekList = []
# holidayYearList = []
holidayObjectsListFiltered1 = filter(lambda x: datetime.strptime(x.holidayDate, '%Y-%m-%d').year == selectYear,
holidayObjectsList)
holidayObjectsListFiltered2 = filter(
lambda x: datetime.strptime(x.holidayDate, '%Y-%m-%d').isocalendar()[1] == selectWeek,
holidayObjectsListFiltered1)
holidayObjectsListFiltered2 = list(holidayObjectsListFiltered2)
print('-------------------------------------------------------- |')
for index in range(len(holidayObjectsListFiltered2)):
print(holidayObjectsListFiltered2[index])
print('-------------------------------------------------------- |')
if selectWeek == today+1:
apicall()
date = datetime.today().date()
for i in range(7):
print(date + timedelta(days=i), " ", weatherDeCoded[i])
# for i in range(len(holidayObjectsList)):
# holidayWeekList.insert(i, datetime.strptime(holidayObjectsList[i].holidayDate, '%Y-%m-%d').isocalendar()[1])
# holidayYearList.insert(i, datetime.strptime(holidayObjectsList[i].holidayDate, '%Y-%m-%d').year)
# if holidayWeekList[i] == 53:
# holidayWeekList[i] = 1
# if holidayWeekList[i] == selectWeek and holidayYearList[i] == selectYear:
# print(holidayObjectsList[i].holidaydate)
def exit():
global selectStartUp
exityn = inputyn("Are you sure you want to exit? [y/n]: ")
if exityn == "y":
selectStartUp = "z"
## open and save to variable a initial .json file as a dictionary with a list of dictionaries with name, and date being keys to each holidays name and date of occurrence as values.
openInitialFile()
## open and save to variable a scraped .json file as a list of dictionaries with the same key-value pair setup as previous file.
openScrapedFile()
## Initialize the main program here by appending scrapped data to initial data and creating a class object for holidays.
initilizeholidays()
## Start user input and begin printing displays.
selectStartUp = inputyn("Do you want to start the super cool holiday management system? [y/n]: ")
## User interface print statements:
startup()
mainmenu()
## Main program while loop. This loop keeps the program cycling until the exit menu option has been chosen.
while selectStartUp == "y":
print()
selectMainMenu = inputint("Please select an option from the above menu: ")
## Main menu selection options. Refer to individual function decorations for insights into functionality.
if selectMainMenu == 1: ## Summarized functionality:
holidayadd() ## Adds a new holiday to the global variable that tracks holiday objects
if selectMainMenu == 2:
holidayremove() ## Removes a existing holiday from the global variable that tracks holiday objects
if selectMainMenu == 3:
listsave() ## Saves file as a .json
if selectMainMenu == 4:
holidayview() ## Lists holiday objects based on user input.
if selectMainMenu == 5:
exit() ## Exits program.
if 1 > selectMainMenu < 5:
print("input invalid: Please enter a number between 1 and 5")
if selectStartUp == "n":
print("okay, smartass! Go shove it where the sun don't shine because this application is daBOMB!")
else:
print("program exit successful")