-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjet_CalenderV.py
More file actions
220 lines (199 loc) · 11.4 KB
/
Projet_CalenderV.py
File metadata and controls
220 lines (199 loc) · 11.4 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
'''
The following text-based code try to make a event calendar by adding new event based on its name, date and time interval.
The program allows the user to save the calendar to the same path as the code directory.
User could allow th euser to remove any preiously inserted and saved data.
The code will be saved if ctrl+c is used to get out of the program.
User could see the previous saved calendar at the beginning of and during the program.
There is exit option to close the program and allow the user to save the code again.
The code checks any overlap among input to prevent any event overlap.
'''
#Import the required libraries into python environment.
import os
import json
import datetime
#Method to save calendar to the same path as the python.
def save_calendar(CalenderName, FileName):
with open(FileName, 'w') as fs:
json.dump(CalenderName, fs)
#Method to load the previously saved calendar.
def load_calendar(Name_of_Saved_Calender):
try:
with open(Name_of_Saved_Calender, 'r') as fl:
return json.load(fl)
except FileNotFoundError:
return {}
#Method to check the entered date and time format to be in the correct format as YYYY-MM-DD HH:MM.
def DateTime(DT):
while True:
try:
inputDT = input(DT)
return datetime.datetime.strptime(inputDT, '%Y-%m-%d %H:%M')
except ValueError:
print("The correct time format is: YYYY-MM-DD HH:MM")
#method to add new event into the calendar by the given name, date and time.
def NewEvent(CalenderName1, name, Start, End):
while End < Start:
print("End time must be later than start time.")
Start = DateTime("New start time (YYYY-MM-DD HH:MM):")
End = DateTime("New end time (YYYY-MM-DD HH:MM):")
new_event = {"name": name, "Start": Start.strftime('%Y-%m-%d %H:%M'), "End": End.strftime('%Y-%m-%d %H:%M')}
# Check if there is any overlap between the new and previously saved events in the calendar.
overlapping_events = [event for event in CalenderName1.values()
if "End" in event and datetime.datetime.strptime(event["End"], '%Y-%m-%d %H:%M') > Start and
datetime.datetime.strptime(event["Start"], '%Y-%m-%d %H:%M') < End]
while overlapping_events:
print("The new event overlaps with the existing events.")
cancel_entry = input("Do you want to go back (yes/no)?")
#User could cancel the event entry if they found any overlap in their schedule.
if cancel_entry in ['yes', 'YES', 'Yes', 'y', 'Y', 'ok']:
return
Start = DateTime("Enter a new start time (YYYY-MM-DD HH:MM):")
End = DateTime("Enter a new end time (YYYY-MM-DD HH:MM):")
#One primary timing check to prevent common mistake that start time should be sooner or equal to the end time of the event.
while End < Start:
print("End time must be later than start time.")
Start = DateTime("New start time (YYYY-MM-DD HH:MM):")
End = DateTime("New end time (YYYY-MM-DD HH:MM):")
new_event["Start"] = Start.strftime('%Y-%m-%d %H:%M')
new_event["End"] = End.strftime('%Y-%m-%d %H:%M')
overlapping_events = [event for event in CalenderName1.values()
if datetime.datetime.strptime(event["End"], '%Y-%m-%d %H:%M') > Start and
datetime.datetime.strptime(event["Start"], '%Y-%m-%d %H:%M') < End]
CalenderName1[name] = new_event
print(f"Event '{name}' added successfully.")
return CalenderName1
#Method to remove previously saved event in the calendar.
def RemoveEvent(CalenderName2):
#First the previously saved events are shown to user to be able to have a correct selection for event removal.
if CalenderName2:
print("Existing Calendars:")
for i, CalenderName2_name in enumerate(CalenderName2.keys(), start=1):
print(f"{i}. {CalenderName2_name}")
#To avoid any intruption due to the common input error, try and except are used to catch wrong indices and values, and notifying the user to enter the correct input.
while True:
try:
CalenderName2_choice = int(input("Enter the number of the CalenderName2 to remove: "))
CalenderName2_name = list(CalenderName2.keys())[CalenderName2_choice - 1]
if CalenderName2_name in CalenderName2:
event = CalenderName2[CalenderName2_name]
print(f"\nEvent Details:")
print(f"Name: {event['name']}")
print(f"Start Time: {event['Start']}")
print(f"End Time: {event['End']}\n")
#Asking for removal confiramtion.
confirm = input("Do you want to remove this event? (yes/no): ").lower()
if confirm in ['yes', 'YES', 'Yes', 'y', 'Y', 'ok']:
del CalenderName2[CalenderName2_name]
print(f"Event '{CalenderName2_name}' removed successfully.")
else:
print("Event removal canceled.")
break
#The user may select the wrong input and this line avoid the simple interuption in program and ask again for the correct input by user.
else:
print(f"Calendar '{CalenderName2_name}'Enter a valid number.")
except (ValueError, IndexError):
print("Please enter a valid number.")
else:
print("Calendar is empty.")
#This method is defined to show the saved events in the calendar.
def Display(CalenderName3, Start=None, End=None):
if CalenderName3:
print("\nEvents in the Calendar:")
for event in CalenderName3.values():
event_start = datetime.datetime.strptime(event["Start"], '%Y-%m-%d %H:%M')
event_end = datetime.datetime.strptime(event["End"], '%Y-%m-%d %H:%M')
if Start is None or (Start <= event_end and End >= event_start):
print(f"Name: {event['name']}")
print(f"Start Time: {event['Start']}")
print(f"End Time: {event['End']}\n")
else:
print("No events to display. Calendar is empty.")
#This method shows the previously saved calendar to be loaded by user or the user could start to make a new calendar.
def Load_or_Make_Calendar():
print("\nWelcome to thre Calendar.\n")
SavedCalendars = os.listdir()
SavedCalendars = [calendar for calendar in SavedCalendars if calendar.endswith('.json')]
if SavedCalendars:
print("Previously saved calendars that you could load:")
for idx, Namee in enumerate(SavedCalendars, start=1):
print(f"{idx}- {Namee}")
Load = input("Do you want to load any previous saved calendar? (yes/no)")
if Load in ['yes', 'YES', 'Yes', 'y', 'Y', 'ok']:
CalendarLoad = int(input("Which calendar do you want to load: (Choose one of the calendar's numbers)"))
SelectedCalendar = SavedCalendars[CalendarLoad - 1]
calendar = load_calendar(SelectedCalendar)
print(f"Loaded calendar '{SelectedCalendar}' successfully.")
CalendarName = SelectedCalendar
today_date = datetime.datetime.now().date()
start_time = datetime.datetime.combine(today_date, datetime.datetime.min.time())
end_time = datetime.datetime.combine(today_date, datetime.datetime.max.time())
format_date = today_date.strftime("%Y-%m-%d")
display(calendar, start=start_time.strftime("%Y-%m-%d %H:%M"), end=end_time.strftime("%Y-%m-%d %H:%M"))
return calendar, CalendarName
else:
calendar = {}
#The extension of .json is used to be able to read and identify the already saved calenders.
CalendarName = input("Select a name for your new calendar:") + '.json'
return calendar, CalendarName
else:
calendar = {}
CalendarName = input("Enter a name for the new calendar:") + '.json'
return calendar, CalendarName
return calendar, CalendarName
#The method shows the available options to the user to apply the possible modification on the calendar.
def Option_List(calendar, CalendarName):
while True:
print("\nSelection Menu:\n")
print("0. Show and load previously saved calendars (0)")
print("1. Add a new event to the calendar (1)")
print("2. Remove event from calender (2)")
print("3. Show all events in the calender (3)")
print("4. Show events in the specific interval (4)")
print("5. Save the calendar(5)")
print("6. Exit and save the calandar (6)")
selection = input("\nWhat do you want to do?")
if selection == '0':
Load_or_Make_Calendar()
elif selection == '1':
Name = input("Select the name for your event:")
Start = DateTime(f"What is the start time of {Name} (YYYY-MM-DD HH:MM)?")
End = DateTime(f"What is the end time of {Name} (YYYY-MM-DD HH:MM)?")
calendar=NewEvent(calendar, Name, Start, End)
elif selection == '2':
RemoveEvent(calendar)
elif selection == '3':
try:
Display(calendar, Start=None, End=None)
except KeyError:
print('\nThere is no scheduled event in the calendar.')
elif selection == '4':
StartTime = DateTime("What is the start time (YYYY-MM-DD HH:MM)?")
EndTime = DateTime("What is the end time (YYYY-MM-DD HH:MM)?")
Display(calendar, StartTime, EndTime)
elif selection == '5':
save_calendar(calendar, CalendarName)
print(f"'{CalendarName}' saved successfully.")
elif selection == '6':
save_selection = input("Do you want to save the data? (yes/no):")
if save_selection in ['yes', 'YES', 'Yes', 'y', 'Y', 'ok']:
save_calendar(calendar, CalendarName)
print(f"'{CalendarName}' saved successfully.")
print("\nHave a nice day.")
break
else:
print("Selection should be a number between 1 and 6.")
#This mehtod is used to show simple steps meaning that the program first try to load previously data or create a new calendar and then using available options to modify the calendar.
def main():
#Using try and except to safely save the data if there is any intruption by ctrl+c.
try:
#Show the previously saved calendar or start new calendar.
Calendar, CalendarName = Load_or_Make_Calendar()
#Show the menu options and the possible available or defined modifications.
Option_List(Calendar, CalendarName)
except KeyboardInterrupt:
save_calendar(Calendar, CalendarName)
print(f"\n'{CalendarName}' is now saved safely.")
print("\nHave a nice day.")
#This method is defined to run the code as soon as it is read by python.
if __name__ == "__main__":
main()