-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcourse.py
More file actions
256 lines (219 loc) · 7.05 KB
/
Copy pathcourse.py
File metadata and controls
256 lines (219 loc) · 7.05 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
"""
CMPUT 297/115 - Final Project - Due 2013-04-12
Version 1.0 2013-04-12
By: Cody Otto
Brittany Lamorie
Ryan Thornhill
This assignment has been done under the full collaboration model,
and any extra resources are cited in the code below.
"""
import random
class Course:
"""
Will initialize a class using the following format for info:
info = ( "course name", [start time, end time], [days], color=)
>>> course = Course(("ECE 212", [8, 9], ["Monday", "Wednesday", "Friday"]))
>>> course.name == "ECE 212"
True
>>> course.start
8
>>> course.end
9
>>> course.days
['Monday', 'Wednesday', 'Friday']
>>> course.each_day()
[('Monday', 8, 9), ('Wednesday', 8, 9), ('Friday', 8, 9)]
"""
# create an empty list, will contain all
# instances of Course objects
_instances = []
def __init__(self, info, color = None):
"""
Initialize an instance of a Course object
"""
self.name = info[0]
times = info[1]
self.start = times[0]
self.end = times[1]
self.days = info[2]
# color will only be None if it is not
# a homework type Course object
if color != None:
self.color = color
else:
self.color = color_rand()
# insert into collection of instances
Course._instances.append(self)
def get_name(self):
"""
Returns the name of the course
"""
return self.name
def get_days(self):
"""
Returns a list of all days of the course
"""
return self.days
def get_start_time(self):
"""
Returns start time of course
"""
return self.start
def get_end_time(self):
"""
Returns end time of course
"""
return self.end
def get_color(self):
"""
Returns the color of the object
"""
return self.color
def get_all_instances():
"""
Gets all instances of Course class, allows
for you to be able to find all possible
courses in current schedule
"""
return [i for i in Course._instances]
def del_instances(self):
"""
Deletes all instances of the same name,
but not necessarily the same object handler.
Returns NONE
"""
for i in Course.get_all_instances():
if i.name == self.name:
Course._instances.remove(i)
def change_time(self, start, end):
"""
Change the time to a new (start,end) pair
>>> course = Course(("ECE 212", [8, 9], ["Monday", "Wednesday", "Friday"]))
>>> course.change_time("9:00","10:00")
>>> course.start
'9:00'
>>> course.end
'10:00'
"""
self.start = start
self.end = end
def change_days(self, deleted, added):
"""
Modifies the days of each course, used to edit an
already existing course.
[deleted] should be a list of days you want removed
[added] should be a list of days you want added
If you only wish to add or delete, simply leave the other list blank ( [] )
>>> course = Course(("ECE 212", ["8:00", "9:00"], ["Monday", "Wednesday", "Friday"]))
>>> course.change_days(["Monday","Friday", "Wednesday"], ["Tuesday", "Thursday"])
>>> course.days
['Tuesday', 'Thursday']
"""
for i in added:
self.days.append(i)
for i in deleted:
if i in self.days:
self.days.remove(i)
def each_day(self):
"""
Returns a list of lists, each entry containing a
compact list with the day, start and end times
>>> course = Course(("ECE 212", ["8:00", "9:00"], ["Monday", "Wednesday", "Friday"]))
>>> course.each_day()
[('Monday', '8:00', '9:00'), ('Wednesday', '8:00', '9:00'), ('Friday', '8:00', '9:00')]
"""
per_day = []
for i in self.days:
per_day.append((i, self.start, self.end))
return per_day
def get_all_times():
"""
Returns a dictionary with the days of
the week as keys and the total amount
of time used in that day as the value.
Used in the homework algorithm to
determine what day is best to place
homework assignments.
"""
all_courses = Course.get_all_instances()
days_of_week = {'Sunday': 0, 'Monday': 0, 'Tuesday': 0, 'Wednesday': 0, 'Thursday': 0, 'Friday': 0, 'Saturday': 0}
courses = []
# removes all possible duplicate instances for calculation
for j in all_courses:
if j not in courses:
courses.append(j)
# calculates the time of each course for each day of the week,
# and keeps a running total
for i in courses:
for j in i.days:
days_of_week[j] = days_of_week[j] + (i.end-i.start)
# returns a dictionary with the values as total occupied
# time in that day
return days_of_week
def color_rand():
"""
This function creates a random color and returns it in the form
#rgb wher r,g and b are a hexidecimal value from 0 to f
"""
r = str(hex(random.randint(1,16))[2])
g = str(hex(random.randint(1,16))[2])
b = str(hex(random.randint(1,16))[2])
return '#'+r+g+b
def save():
"""
Saves all objects that exist as instances of a Course
object. Called whenever you wish to save.
"""
# retrieve all instances of courses to save
courses = Course.get_all_instances()
saved = []
opened = open("save.txt", 'w')
# remove duplicates (because an instance saves per day of class)
for j in courses:
if j not in saved:
saved.append(j)
for i in saved:
# save in specific format for reloading, saves
# a course object per line
st_start = str(i.start)
st_end = str(i.end)
opened.write(i.name)
opened.write(': ')
opened.write(st_start)
opened.write(' ')
opened.write(st_end)
opened.write(': ')
for x in i.days:
opened.write(x)
opened.write(' ')
opened.write(':')
opened.write(str(i.color))
opened.write(':')
opened.write('\n')
#close file, saves memory
opened.close()
def load():
"""
Loads from a text file as many previously used
instances of a course as are contained in the text file.
This is done on startup to recreate the environment
from previous shutdown.
"""
opened = open("save.txt", 'r')
# read through all lines of the file (one course per
# line)
all_courses = opened.readlines()
# due to specific format of saving, load back in
for i in all_courses:
i = i.split(':')
name = i[0]
times = i[1]
times = times.split()
times[0] = float(times[0])
times[1] = float(times[1])
days = i[2]
days = days.split()
Course((i[0], times, days), color=i[3])
if __name__ == "__main__":
import doctest
doctest.testmod()