-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgym.py
615 lines (498 loc) · 20.9 KB
/
gym.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
"""
Assignment 0 starter code
CSC148, Winter 2020
This code is provided solely for the personal and private use of
students taking the CSC148 course at the University of Toronto.
Copying for purposes other than this use is expressly prohibited.
All forms of distribution of this code, whether as given or with
any changes, are expressly prohibited.
Authors: Mario Badr, Christine Murad, Diane Horton, Misha Schwartz, Sophia Huynh
and Jaisie Sin
All of the files in this directory and all subdirectories are:
Copyright (c) 2020 Mario Badr, Christine Murad, Diane Horton, Misha Schwartz,
Sophia Huynh and Jaisie Sin
"""
from datetime import datetime
from typing import Dict, List, TextIO, Tuple
# The additional pay per hour instructors receive for each certificate they
# hold.
BONUS_RATE = 1.50
class WorkoutClass:
"""A workout class that can be offered at a gym.
=== Private Attributes ===
_name: The name of this WorkoutClass.
_required_certificates: The certificates that an instructor must hold to
teach this WorkoutClass.
"""
_name: str
_required_certificates: List[str]
def __init__(self, name: str, required_certificates: List[str]) -> None:
"""Initialize a new WorkoutClass called <name> and with the
<required_certificates>.
>>> workout_class = WorkoutClass('Kickboxing', ['Strength Training'])
>>> workout_class.get_name()
'Kickboxing'
"""
self._name = name
self._required_certificates = required_certificates[:]
def get_name(self) -> str:
"""Return the name of this WorkoutClass.
>>> workout_class = WorkoutClass('Kickboxing', ['Strength Training'])
>>> workout_class.get_name()
'Kickboxing'
"""
return self._name
def get_required_certificates(self) -> List[str]:
"""Return all the certificates required to teach this WorkoutClass.
>>> workout_class = WorkoutClass('Kickboxing', ['Strength Training'])
>>> workout_class.get_required_certificates()
['Strength Training']
"""
return self._required_certificates[:]
class Instructor:
"""An instructor at a Gym.
Each instructor may hold certificates that allows them to teach specific
workout classes.
=== Public Attributes ===
name: This Instructor's name.
=== Private Attributes ===
_id: This Instructor's identifier.
_certificates: The certificates held by this Instructor.
"""
name: str
_id: int
_certificates: List[str]
def __init__(self, instructor_id: int, instructor_name: str) -> None:
"""Initialize a new Instructor with an <instructor_id> and their
<instructor_name>. Initially, the instructor holds no certificates.
>>> instructor = Instructor(1, 'Matylda')
>>> instructor.get_id()
1
>>> instructor.name
'Matylda'
"""
# TODO: implement this method!
pass
def get_id(self) -> int:
"""Return the id of this Instructor.
>>> instructor = Instructor(1, 'Matylda')
>>> instructor.get_id()
1
"""
# TODO: implement this method!
pass
def add_certificate(self, certificate: str) -> bool:
"""Add the <certificate> to this instructor's list of certificates iff
this instructor does not already hold the <certificate>.
Return True iff the <certificate> was added.
>>> instructor = Instructor(1, 'Matylda')
>>> instructor.add_certificate('Strength Training')
True
>>> instructor.add_certificate('Strength Training')
False
"""
# TODO: implement this method!
pass
def get_num_certificates(self) -> int:
"""Return the number of certificates held by this instructor.
>>> instructor = Instructor(1, 'Matylda')
>>> instructor.add_certificate('Strength Training')
True
>>> instructor.get_num_certificates()
1
"""
# TODO: implement this method!
pass
def can_teach(self, workout_class: WorkoutClass) -> bool:
"""Return True iff this instructor has all the required certificates to
teach the workout_class.
>>> matylda = Instructor(1, 'Matylda')
>>> kickboxing = WorkoutClass('Kickboxing', ['Strength Training'])
>>> matylda.can_teach(kickboxing)
False
>>> matylda.add_certificate('Strength Training')
True
>>> matylda.can_teach(kickboxing)
True
"""
# TODO: implement this method!
pass
class Gym:
"""A gym that hosts workout classes taught by instructors.
All offerings of workout classes start on the hour and are 1 hour long.
=== Public Attributes ===
name: The name of the gym.
=== Private Attributes ===
_instructors: The roster of instructors who work at this Gym.
Each key is an instructor's ID and its value is the Instructor object
representing them.
_workouts: The workout classes that are taught at this Gym.
Each key is the name of a workout class and its value is the
WorkoutClass object representing it.
_rooms: The rooms in this Gym.
Each key is the name of a room and its value is its capacity, that is,
the number of people who can register for a class in this room.
_schedule: The schedule of classes offered at this gym. Each key is a date
and time and its value is a nested dictionary describing all offerings
that start then. Each key in the nested dictionary is the name of a room
that has an offering scheduled then, and its value is a tuple describing
the offering. The tuple elements record the instructor teaching the
class, the workout class itself, and a list of registered clients. Each
client is represented by a unique string.
=== Representation Invariants ===
- Each key in _schedule is for a time that is on the hour.
- No instructor is recorded as teaching two workout classes at the same
time.
- No client is recorded as registered for two workout classes at the same
time.
- If an instructor is recorded as teaching a workout class, they have the
necessary qualifications.
- If there are no offerings scheduled at date and time <d> in room <r> then
<r> does not occur as a key in _schedule[d]
- If there are no offerings at date and time <d> in any room at all, then
<d> does not occur as a key in _schedule
"""
name: str
_instructors: Dict[int, Instructor]
_workouts: Dict[str, WorkoutClass]
_rooms: Dict[str, int]
_schedule: Dict[datetime,
Dict[str, Tuple[Instructor, WorkoutClass, List[str]]]]
def __init__(self, gym_name: str) -> None:
"""Initialize a new Gym with <name> that has no instructors, workout
classes, rooms, or offerings.
>>> ac = Gym('Athletic Centre')
>>> ac.name
'Athletic Centre'
"""
self.name = gym_name
self._instructors = {}
self._workouts = {}
self._rooms = {}
self._schedule = {}
def add_instructor(self, instructor: Instructor) -> bool:
"""Add a new <instructor> to this Gym's roster iff the <instructor>
has not already been added to this Gym's roster.
Return True iff the instructor was added.
>>> ac = Gym('Athletic Centre')
>>> diane = Instructor(1, 'Diane')
>>> ac.add_instructor(diane)
True
"""
# TODO: implement this method!
pass
def add_workout_class(self, workout_class: WorkoutClass) -> bool:
"""Add a <workout_class> to this Gym iff the <workout_class> has not
already been added this Gym.
Return True iff the workout class was added.
>>> ac = Gym('Athletic Centre')
>>> kickboxing = WorkoutClass('Kickboxing', ['Strength Training'])
>>> ac.add_workout_class(kickboxing)
True
"""
# TODO: implement this method!
pass
def add_room(self, name: str, capacity: int) -> bool:
"""Add a room with a <name> and <capacity> to this Gym iff the room
has not already been added to this Gym.
Return True iff the room was added.
>>> ac = Gym('Athletic Centre')
>>> ac.add_room('Dance Studio', 50)
True
"""
# TODO: implement this method!
pass
def schedule_workout_class(self, time_point: datetime, room_name: str,
workout_name: str, instr_id: int) -> bool:
"""Add an offering to this Gym at a <time_point> iff:
- the room with <room_name> is available,
- the instructor with <instr_id> is qualified to teach the workout
class with <workout_name>, and
- the instructor is not teaching another workout class during the
same <time_point>.
A room is available iff it does not already have another workout class
scheduled at that date and time.
The added offering should start with no registered clients.
Return True iff the offering was added.
Preconditions:
- The room has already been added to this Gym.
- The Instructor has already been added to this Gym.
- The WorkoutClass has already been added to this Gym.
>>> ac = Gym('Athletic Centre')
>>> diane = Instructor(1, 'Diane')
>>> ac.add_instructor(diane)
True
>>> diane.add_certificate('Cardio 1')
True
>>> ac.add_room('Dance Studio', 50)
True
>>> boot_camp = WorkoutClass('Boot Camp', ['Cardio 1'])
>>> ac.add_workout_class(boot_camp)
True
>>> sep_9_2019_12_00 = datetime(2019, 9, 9, 12, 0)
>>> ac.schedule_workout_class(sep_9_2019_12_00, 'Dance Studio',\
boot_camp.get_name(), diane.get_id())
True
"""
# TODO: implement this method!
pass
def register(self, time_point: datetime, client: str, workout_name: str) \
-> bool:
"""Add <client> to the WorkoutClass with <workout_name> that is being
offered at <time_point> iff the client has not already been registered
in any course (including <workout_name>) at <time_point>, and the room
is not full.
If the WorkoutClass is being offered in more than one room at
<time_point>, then the client is added to any one of the rooms (i.e.,
the room chosen does not matter).
Return True iff the client was added.
Precondition: the WorkoutClass with <workout_name> is being offered in
some room at <time_point>.
>>> ac = Gym('Athletic Centre')
>>> diane = Instructor(1, 'Diane')
>>> diane.add_certificate('Cardio 1')
True
>>> ac.add_instructor(diane)
True
>>> ac.add_room('Dance Studio', 50)
True
>>> boot_camp = WorkoutClass('Boot Camp', ['Cardio 1'])
>>> ac.add_workout_class(boot_camp)
True
>>> sep_9_2019_12_00 = datetime(2019, 9, 9, 12, 0)
>>> ac.schedule_workout_class(sep_9_2019_12_00, 'Dance Studio',\
boot_camp.get_name(), diane.get_id())
True
>>> ac.register(sep_9_2019_12_00, 'Philip', 'Boot Camp')
True
>>> ac.register(sep_9_2019_12_00, 'Philip', 'Boot Camp')
False
"""
# TODO: implement this method!
pass
def offerings_at(self, time_point: datetime) -> List[Tuple[str, str, str]]:
"""Return all the offerings that start at <time_point>.
Return a list of 3-element tuples containing: the instructor's name, the
workout class's name, and the room's name. If there are no offerings at
<time_point>, return an empty list.
The order of the elements in the returned list does not matter.
>>> ac = Gym('Athletic Centre')
>>> diane = Instructor(1, 'Diane')
>>> diane.add_certificate('Cardio 1')
True
>>> ac.add_instructor(diane)
True
>>> ac.add_room('Dance Studio', 50)
True
>>> boot_camp = WorkoutClass('Boot Camp', ['Cardio 1'])
>>> ac.add_workout_class(boot_camp)
True
>>> t1 = datetime(2019, 9, 9, 12, 0)
>>> ac.schedule_workout_class(t1, 'Dance Studio',\
boot_camp.get_name(), diane.get_id())
True
>>> ('Diane', 'Boot Camp', 'Dance Studio') in ac.offerings_at(t1)
True
"""
# TODO: implement this method!
pass
def instructor_hours(self, time1: datetime, time2: datetime) -> \
Dict[int, int]:
"""Return a dictionary reporting the hours worked by instructors
between <time1> and <time2>, inclusive.
Each key is an instructor ID and its value is the total number of hours
worked by that instructor between <time1> and <time2> inclusive. Both
<time1> and <time2> specify the start time for an hour when an
instructor may have taught.
Precondition: time1 < time2
>>> ac = Gym('Athletic Centre')
>>> diane = Instructor(1, 'Diane')
>>> david = Instructor(2, 'David')
>>> diane.add_certificate('Cardio 1')
True
>>> ac.add_instructor(diane)
True
>>> ac.add_instructor(david)
True
>>> ac.add_room('Dance Studio', 50)
True
>>> boot_camp = WorkoutClass('Boot Camp', ['Cardio 1'])
>>> ac.add_workout_class(boot_camp)
True
>>> t1 = datetime(2019, 9, 9, 12, 0)
>>> ac.schedule_workout_class(t1, 'Dance Studio', boot_camp.get_name(),
... 1)
True
>>> t2 = datetime(2019, 9, 10, 12, 0)
>>> ac.instructor_hours(t1, t2) == {1: 1, 2: 0}
True
"""
# TODO: implement this method!
pass
def payroll(self, time1: datetime, time2: datetime, base_rate: float) \
-> List[Tuple[int, str, int, float]]:
"""Return a sorted list of tuples reporting the total wages earned by
instructors between <time1> and <time2>, inclusive.
Each tuple contains 4 elements, in this order:
- the instructor's ID,
- the instructor's name,
- the number of hours worked by the instructor between <time1>
and <time2> inclusive, and
- the instructor's total wages earned between <time1> and <time2>
inclusive.
The list is sorted by instructor ID.
Both <time1> and <time2> specify the start time for an hour when an
instructor may have taught.
Each instructor earns a <base_rate> per hour plus an additional
BONUS_RATE per hour for each certificate they hold.
Precondition: time1 < time2
>>> ac = Gym('Athletic Centre')
>>> diane = Instructor(1, 'Diane')
>>> david = Instructor(2, 'David')
>>> diane.add_certificate('Cardio 1')
True
>>> ac.add_instructor(diane)
True
>>> ac.add_instructor(david)
True
>>> ac.add_room('Dance Studio', 50)
True
>>> boot_camp = WorkoutClass('Boot Camp', ['Cardio 1'])
>>> ac.add_workout_class(boot_camp)
True
>>> t1 = datetime(2019, 9, 9, 12, 0)
>>> ac.schedule_workout_class(t1, 'Dance Studio', boot_camp.get_name(),
... 1)
True
>>> t2 = datetime(2019, 9, 10, 12, 0)
>>> ac.payroll(t1, t2, 25.0)
[(1, 'Diane', 1, 26.5), (2, 'David', 0, 0.0)]
"""
# TODO: implement this method!
pass
def parse_instructor(file: TextIO, header: str) -> Instructor:
"""Return a new Instructor based on the data found in the file and the
header.
Precondition: header has the format 'Instructor <ID> <Full Name>'
"""
# Extract instructor information from the header
header_elements = header.split()
instr_id = int(header_elements[1].strip())
name = ' '.join(header_elements[2:])
# Create a new instructor object.
instr = Instructor(instr_id, name)
# Add any certificates that the instructor holds.
line = file.readline().strip()
while line != '':
certificate = line.strip()
instr.add_certificate(certificate)
line = file.readline().strip()
return instr
def parse_workout_class(file: TextIO, header: str) -> WorkoutClass:
"""Return a new WorkoutClass based on the data found in the file and the
header.
Precondition: header has the format 'Class <Workout Class Name>'
"""
name = header.replace('Class', '').strip()
required_certificates = []
line = file.readline().strip()
while line != '':
required_certificates.append(line.strip())
line = file.readline().strip()
return WorkoutClass(name, required_certificates)
def parse_room(file: TextIO, header: str) -> Tuple[str, int]:
"""Return a new Room based on the data found in the file and the header.
Precondition: header has the format 'Room <Room Name>'
"""
room_name = header.split()[1].strip()
# Ignore the full name.
file.readline()
# Parse the capacity.
capacity = int(file.readline().strip())
return room_name, capacity
def parse_offerings(file: TextIO, header: str) -> \
Tuple[datetime, List[Tuple[int, str, str]]]:
"""Return a tuple where the first element is a datetime for when the
offerings are scheduled. The second element is a list of all offerings.
Each offering is a tuple with three elements: the instructor ID, the
workout class name, and the room name in that order.
Precondition: header has the format 'Offerings <Date and Time>', where the
date and time are in the following format: %Y-%m-%d %H:%M
"""
date_time = header.replace('Offerings', '').strip()
when = datetime.strptime(date_time, '%Y-%m-%d %H:%M')
offerings = []
line = file.readline().strip()
while line != '':
elements = line.split(sep=',')
instr_id = int(elements[0].strip())
workout_name = elements[1].strip()
room_id = elements[2].strip()
offerings.append((instr_id, workout_name, room_id))
line = file.readline().strip()
return when, offerings
def parse_registrations(file: TextIO, header: str) -> \
Tuple[datetime, List[Tuple[str, str]]]:
"""Return a tuple where the first element is a datetime for the offering
being registered for. The second element is a list of tuples where, for each
tuple, the first element is the name of the client and the second element is
the name of the workout class the client is registering for.
Precondition: header has the format 'Registrations <Date and Time>', where
the date and time are in the following format: %Y-%m-%d %H:%M
"""
date_time = header.replace('Registrations', '').strip()
when = datetime.strptime(date_time, '%Y-%m-%d %H:%M')
registrations = []
line = file.readline().strip()
while line != '':
elements = line.split(sep=',')
name = elements[0].strip()
workout_class = elements[1].strip()
registrations.append((name, workout_class))
line = file.readline().strip()
return when, registrations
def load_data(file_name: str, gym_name: str) -> Gym:
"""Return a new Gym based on the contents of the file being read.
Precondition: Assumes that the file <file_name> exists and can be read.
"""
new_gym = Gym(gym_name)
with open(file_name, 'r') as f:
line = f.readline().strip()
while line != '':
if line.startswith('Instructor'):
instr = parse_instructor(f, line)
new_gym.add_instructor(instr)
elif line.startswith('Class'):
workout_class = parse_workout_class(f, line)
new_gym.add_workout_class(workout_class)
elif line.startswith('Room'):
room_name, room_capacity = parse_room(f, line)
new_gym.add_room(room_name, room_capacity)
# ignore the next line
f.readline()
elif line.startswith('Offerings'):
when, offerings = parse_offerings(f, line)
for o in offerings:
new_gym.schedule_workout_class(when, o[2], o[1], o[0])
elif line.startswith('Registrations'):
when, registrations = parse_registrations(f, line)
for r in registrations:
new_gym.register(when, r[0], r[1])
line = f.readline().strip()
return new_gym
if __name__ == '__main__':
import python_ta
python_ta.check_all(config={
'allowed-io': ['load_data'],
'allowed-import-modules': ['doctest', 'python_ta', 'typing',
'datetime'],
'max-attributes': 15,
})
import doctest
doctest.testmod()
# Example: reading data about a Gym from a file.
# ac = load_data('athletic-centre.txt', 'Athletic Centre')