-
Notifications
You must be signed in to change notification settings - Fork 0
/
Event_List.py
110 lines (92 loc) · 3.33 KB
/
Event_List.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
# Created by Zachary Andrews
# Github: ZachAndrews98
import datetime
file_name = '.events/event_list.evt'
# list of events
class event_list:
event_list = list()
def __init__(self):
self.event_list.clear()
# adds event to list
def add_event(self, event):
self.event_list.append(event)
with open(file_name, 'a') as file:
file.write(str(event) + '\n')
file.close()
self.remove_blank_lines()
# returns the entire list of events
def get_event_list(self):
return self.event_list
# returns a single specified event
def get_event(self, event_name):
for event in self.event_list:
if event.name == event_name:
return event
# returns all events on a certain date
def get_events_by_date(self, date):
events = list()
for event in self.event_list:
if event.date == date:
events.append(event)
return events
# deletes event by name
def delete_event(self, event_name):
with open(file_name, 'w+') as file:
for event in list(self.event_list):
if event.name != event_name:
file.write(str(event) + '\n')
if event.name == event_name:
self.event_list.remove(event)
file.close()
self.remove_blank_lines()
# deletes all events on or before a given date
def delete_event_before_date(self, date):
with open(file_name, 'w+') as file:
for event in list(self.event_list):
if event.date > date:
file.write(str(event) + '\n')
if event.date <= date:
self.event_list.remove(event)
file.close()
self.remove_blank_lines()
# run at startup, checks if any events are stored, if there are adds them
# to the list
def check_for_events(self):
# if storage file exists
try:
with open(file_name, 'r') as file:
for line in file:
line = line.replace("\n", "")
if "\n" in line:
print("\t\tTRUE")
line = line.split(', ')
date = line[0].split('/')
self.event_list.append(event(line[1], datetime.date(
int(date[2]), int(date[0]), int(date[1])), line[2]))
file.close()
# creates file if doesn't exist
except BaseException:
file = open(file_name, 'w+')
file.close()
return
# removes blank lines in ./.events/event_list.evt
def remove_blank_lines(self):
with open(file_name) as filehandle:
lines = filehandle.readlines()
with open(file_name, 'w+') as file:
lines = filter(lambda x: x.strip(), lines)
file.writelines(lines)
filehandle.close()
file.close()
# event class, stores information about an event
class event:
name = None
date = None
desc = None
def __init__(self, event_name, event_date, event_desc):
self.name = event_name
self.date = event_date
self.desc = event_desc
def __repr__(self):
return str(self.date.month) + '/' + str(self.date.day) + \
'/' + str(self.date.year) + ', ' + self.name + ', ' + self.desc