2023/2/6 20:15 Assignment 0: CSC148H1S 20231 (All Sections): Introduction to Computer Science
https://q.utoronto.ca/courses/292974/pages/assignment-0 1/6
Assignment 0
Assignment 0: The Gym
Due date: Tuesday, February 7th, 2023 before 1pm sharp (not 1:10)
You must complete this assignment individually.
Learning goals
This assignment is called “Assignment 0” because it is considerably smaller than Assignments 1
and 2. Its purpose is to make sure that you have understood the basics of object-oriented
programming in Python and are ready to move on with the more advanced topics in the course.
By the end of this assignment you should be able to:
Implement a class in Python from a provided interface, including:
Using instance attributes that enable the class methods to provide their services to client
code,
Enforcing representation invariants that record important facts about implementation
decisions, and
Implementing initializers and other methods
Choose unit tests that can convincingly demonstrate that a method works according to its
docstring for all possible inputs
Implement those test cases in pytest
Interpret the results of doctests and unit tests to assess the correctness of methods
You should also have developed these habits:
Implementing tests for a method before implementing the method
Running your test suite throughout the development of your code
The domain
In this assignment, you will work with three Python classes that manage the schedule of a gym. A
Gym consists of many rooms and each room has a maximum capacity. The gym follows a
Schedule where, at different times in the day, a room may have a Workout Class scheduled in it.
A workout class is taught by an Instructor provided they have the proper qualifications (in the
form of certificates) to teach it. A gym offering is a workout class taking place in a specific room,
at a specific time, and taught by a specific instructor. Clients can register for these offerings
provided there is enough space in the room.
We will assume that all offerings begin on the hour and last for 1 hour. We will also assume that
all rooms have a fixed capacity (i.e., the room capacity does not change for different workout
classes).
2023/2/6 20:15 Assignment 0: CSC148H1S 20231 (All Sections): Introduction to Computer Science
https://q.utoronto.ca/courses/292974/pages/assignment-0 2/6
The program
The code consists of three Python classes:
An instance of WorkoutClass represents a type of workout class that can be offered at a gym,
such as advanced pilates . It is worthy of being a Python class rather than just a string because
it stores information about the qualifications required for teaching that type of workout class.
An instance of Instructor represents an instructor who may teach classes at a gym.
An instance of Gym represents a gym, with all of its instructors and its schedule of workout
classes. It contains instances of the other classes.
The code for WorkoutClass has been completed for you, but many methods for the Gym class are
missing their implementation, and you will need to complete the Instructor class to ensure that it
is compatible with the rest of the provided code. The only public attribute of the Instructor class
should be its name attribute; any other attributes you choose to add must be made private.
Representing the data
We have already decided how data will be represented for you in this assignment. For example,
clients are represented as strings and instructors are represented as a custom Python class. You
should read the provided starter code carefully to see how all the data is stored.
The most complex data structure is referred to by the _schedule attribute in the Gym class. The
diagram below should help you visualize the data structure:
Input to the program
2023/2/6 20:15 Assignment 0: CSC148H1S 20231 (All Sections): Introduction to Computer Science
https://q.utoronto.ca/courses/292974/pages/assignment-0 3/6
Input to the program comes from a file in a format called yaml. Yaml (rhymes with “camel”) is a
syntax for storing data for programs to use, and is designed to be easy for people to read. You
can open the provided file athletic-centre.yaml to see what a yaml file looks like, and read the
docstring of function gym_from_yaml in gym.py to find out how we used yaml to store data for a
gym.
We have provided the code for reading the data into the program from a yaml file, but we
encourage you to take the time to familiarize yourself with the basic structure of the code.
Getting ready
Save a0.zip to your computer and extract its contents. Copy all the extracted files and folders into
your csc148/assignments/a0 folder. Specifically, a0.zip includes:
gym.py : Starter code that you will complete and submit.
gym_utilities.py : Some provided helper functions.
gym_sample_tests.py : Some basic tests cases that you will want to add to.
athletic-centre.yaml : An example data file that you can use to run your program.
design_recipes : A folder containing the function design recipe used in csc108, the class design
recipe that we use in this course, and the example code from the class design recipe
document. This is for handy reference.
Requirements.txt : The starter code uses a package called pyYAML to process yaml files, and
you’ll need to install that. Open Requirements.txt and follow the same process you used in
Step 6 of the Software Setup guide (https://q.utoronto.ca/courses/292974/pages/installation-
instructions) to install this one additional package.
1. Open up the appropriate window:
Windows: Click on Ctrl+Alt+S on your keyboard, or select File > Settings to access the
Settings window.
macOS: Click ⌘+, on your keyboard, or select PyCharm > Preferences to access the
Preferences window.
Linux: Click on Ctrl+Alt+S on your keyboard, or select PyCharm > Settings to access the
Settings window.
2. Navigate to Project: csc148 > Python Interpreter from the sidebar.
3. Press the + icon and type the package name pyYAML .
4. Select the package and click Install Package.
Your tasks
Your tasks are to:
1. Write the Instructor class.
You must define the class so that it provides the public methods needed by the provided
function gym_from_yaml , by the methods in the Gym class, and by the tests in
gym_sample_tests.py .
2023/2/6 20:15 Assignment 0: CSC148H1S 20231 (All Sections): Introduction to Computer Science
https://q.utoronto.ca/courses/292974/pages/assignment-0 4/6
You may find it helpful to add more public methods to your Instructor class later. For
example, when implementing some of the methods in class Gym , you may find you require
certain information that is stored in instances of your Instructor class.
You must include one public attribute called name to hold the instructor’s name, and that
must be the only public attribute. You get to decide what private attributes to use to store
additional information.
2. Implement the methods defined in class Gym . You must use the attributes that we defined in
the class docstring, and must ensure that the representation invariants hold at all times in
between method calls.
Do not add any new attributes or public methods to this class.
Class Gym will use instances of class Instructor and WorkoutClass heavily, but it must not
use any of their private attributes.
If a docstring does not specify what to do in a particular scenario, you may decide what the
method will do (we won’t test that scenario). You are welcome, and encouraged, to add private
helper methods as you see fit. Include a docstring and doctest examples for any private methods
you write.
The main block in gym.py calls a function that provides a small demo showing how to read the
data for a gym from a yaml file, and use some of the services provided by your classes. It also
calls a function we provided to make and display a webpage showing the whole schedule for a
gym. Once your code is complete, try running the main block to see how your code supports this!
Helpful tips
Dealing with a very complex object. The trickiest part of this assignment is the nested structure
in class Gym . It will help immensely if you can keep straight which data (and type) you are
referring to at each moment in the code.
Local variables inside your methods aren’t required to have a type annotation, but in PyCharm
you can hover over a variable name and it will indicate what type of object it refers to! While this is
helpful, it is more helpful if you give your local variables good names, so that you can easily keep
track of what information that are storing.
Pulling out the pieces as you iterate. When you are iterating over a compound object (e.g., a
list of tuples, a dictionary), you can extract its pieces in the for statement. Here are some
examples:
# Example 1 - List of Tuples
world = [('AFG', 'Afghanistan', 22720000),
('CAN', 'Canada', 31147000),
('ARG', 'Argentina', 37032000),
('GHA', 'Ghana', 20212000)]
for code, country_name, population in world:
# Loop body omitted.
# Example 2 - Dictionary
code_to_continent = {'AFG': 'Asia',
2023/2/6 20:15 Assignment 0: CSC148H1S 20231 (All Sections): Introduction to Computer Science
https://q.utoronto.ca/courses/292974/pages/assignment-0 5/6
'CAN': 'North America',
'ARG': 'South America',
'GHA': 'Africa'
}
for code, continent in code_to_continent.items():
# Loop body omitted.
A list of tuples can be sorted. If you call the sort method on a list of tuples it will, by default,
sort based on the first element of the tuple. (In the case of a tie, it then sorts on the next element
and so on.) For example,
>>> world = [('AFG', 'Afghanistan', 22720000),
('CAN', 'Canada', 31147000),
('ARG', 'Argentina', 37032000),
('GHA', 'Ghana', 20212000)]
>>> world.sort()
>>> print(world)
[('AFG', 'Afghanistan', 22720000), ('ARG', 'Argentina', 37032000),
('CAN', 'Canada', 31147000), ('GHA', 'Ghana', 20212000)]
Formating datetime objects. You’ll find you need to format some dates stored in datetime
objects in part of this assignment. The example below demonstrates formatting a datetime object.
>>> from datetime import datetime
>>> date = datetime(year=2023,month=1, day=27, hour=17)
>>> date.strftime("%A, %Y-%m-%d")
'Friday, 2023-01-27'
>>> date.strftime('%H:%M')
'17:00'
Beware that the notation for specifying a format is case-sensitive. E.g., the datetime library
represents a month with %m and a minute with %M . For anyone interested, you can read more
about these format strings on the python documentation page for datatime
(https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior) .
About testing
Be sure to test your code thoroughly. - You may use our doctests and our tests in
gym_sample_tests.py as a starting point, but you should augment these with much more thorough
testing of your own to ensure your code is fully correct. - For any helper methods that you define,
you will be starting from scratch with the testing. - Your assignment grade will be based on the
autotesting that we do, and you can be sure we’ll try to break your code as hard as we can,
so you should also!
The most efficient way to produce code that works is to create and run your test cases as
early as possible, and to re-run them after every change to your code.
Polish!
Take some time to polish up. This step will improve your mark, but it also feels so good. Here are
some things you can do:
2023/2/6 20:15 Assignment 0: CSC148H1S 20231 (All Sections): Introduction to Computer Science
https://q.utoronto.ca/courses/292974/pages/assignment-0 6/6
Pay attention to any violations of the Python style guidelines that PyCharm points out. Fix
them!
Run the provided python_ta.check_all() code to check for errors. Fix them!
Check your docstrings for any helper methods you wrote to make sure they are precise and
complete, and that they follow the conventions of the function design recipe
(https://www.teach.cs.toronto.edu/~csc148h/winter/notes/python-
recap/function_design_recipe.pdf) and the class design recipe
(https://www.teach.cs.toronto.edu/~csc148h/winter/notes/object-oriented-
programming/class_design_recipe.pdf) .
Read through and polish your internal comments.
Remove any code you added just for debugging, such as calls to the print function.
Remove the word “TODO” wherever/whenever you have completed the task.
Take pride in your gorgeous code!
Submission instructions
Submit your file gym.py on MarkUs. No other files need to be submitted. We strongly recommend
that you submit early and often. We will grade the latest version you submit within the permitted
submission period. Be sure to run the tests we’ve provided within MarkUs one last time
before the due date. This will make sure that you didn’t accidentally submit the wrong version of
your code, or worse yet, the starter code!
Congratulations, you are finished with your first assignment in CSC148! Go have some chocolate
or do a cartwheel. :)