-
Notifications
You must be signed in to change notification settings - Fork 3
/
appointments.py
105 lines (81 loc) · 3.17 KB
/
appointments.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
# A few comments:
# After testing the implementation, i realize i should have revealed a bit of useful components in
# the "datetime" module.
# This is particularly important for the "nextOccurrence" method (that the book didn't include).
#
# It also wasn't very logical to have a monthly appointment, as the months don't all have the same number of days
# (I somehow missed this when i thought up the question), so I switched this for a weekly appointment instead.
#
# It should also have been worth more points (perhaps 8p) on an exam, instead of the (6p) i wrote on the question.
#
# / Mikael
import datetime
class Appointment:
def __init__(self, description, date):
"""
Constructor for appointments, storing the description and starting date.
"""
self.description = description
self.date = date
def occurs_on(self, date):
"""
Checks if the appointment occurs on the given date
"""
raise NotImplementedError("Missing 'occursOn' implementation in appointment")
def next_occurrence(self, date):
"""
Returns the date of the next occurrence if any, otherwise None
"""
raise NotImplementedError("Missing 'nextOccurrence' implementation in appointment")
def save(self, fid):
"""
Saves the file to the descriptor
"""
date = "{}-{}-{}".format(self.date.year, self.date.month, self.date.day)
fid.write('<Appointment type="{}", date="{}">\n'.format(self.type, date))
fid.write(self.description + '\n')
fid.write('</Appointment>\n'.format(type))
class OneTime(Appointment):
type = 'onetime'
def occurs_on(self, date):
return date == self.date
def next_occurrence(self, date):
if date <= date:
return self.date
class Daily(Appointment):
type = 'daily'
def occurs_on(self, date):
return date >= self.date
def next_occurrence(self, date):
if date <= self.date:
return date
else:
return date + datetime.timedelta(days=1)
class Weekly(Appointment):
type = 'weekly'
def occurs_on(self, date):
if date >= self.date:
return self.date.weekday() == date.weekday()
def next_occurrence(self, date):
if date <= self.date:
return self.date
else:
return date + datetime.timedelta(days=7)
# Testing part A:
appointments = [OneTime('Wedding', datetime.date(2015, 4, 10)),
Daily('Bachelor thesis group meeting', datetime.date(2015, 1, 20)),
Weekly('Ninja practice', datetime.date(2014, 1, 1))]
for appointment in appointments:
d = datetime.date(2015, 3, 5)
occurs = appointment.occurs_on(d)
print("'{}' {} occurs on {}".format(appointment.description,
'does' if occurs else "doesn't",
d))
if not occurs:
next_d = appointment.next_occurrence(d)
if next_d is not None:
print("It will occur next on {}".format(next_d))
# Testing part B:
with open('calendar.data', 'w') as f:
for appointment in appointments:
appointment.save(f)