Skip to content

Commit 2a9c5c5

Browse files
update oo intro exercise
1 parent 0450712 commit 2a9c5c5

4 files changed

Lines changed: 72 additions & 15 deletions

File tree

‎source/class_schedule/lesson07.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,6 @@ Exercises:
6969
.. toctree::
7070
:maxdepth: 1
7171

72-
../exercises/oo_intro.rst
72+
../exercises/oo_intro/oo_intro.rst
7373
../exercises/html_renderer.rst
7474
../exercises/html_renderer_tutorial.rst

‎source/exercises/oo_intro/oo_intro.rst‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
.. _oo_intro:
22

3-
########################
3+
#######################
44
OO Intro - Report Class
5-
########################
5+
#######################
66

77
This assignment uses Object Oriented Programming to design a class that can be used to manage data reporting.
88

@@ -14,18 +14,28 @@ We will explore here how one can utilize OO to improve and enhance reporting cap
1414
Procedure
1515
=========
1616

17-
You will use :download:`report.py` and :download:`test_report.py` file as a starting point for your code.
17+
Use the :download:`report.py` and :download:`test_report.py` files as a starting point for your code.
1818

19-
You will notice that ``Report`` class will have attributes and methods defined for you, including input parameters (and their types) as well as expected output.
20-
You will need to fill out the code for each defined method and docstrings containing additional information on what is expected.
19+
Notice that the ``Report`` class has attributes and methods defined for you, including input parameters as well as expected output.
20+
21+
You will need to fill out the code and docstrings for each defined method containing additional information on what is expected.
22+
23+
There is also some example code in the ``__main__`` block, to see how it's used.
24+
25+
Use TDD: write a test for each method before you add that functionality. There is a test file started for you -- it has one (failing) test for one of the methods.
26+
27+
The Row class
28+
-------------
29+
30+
The ``Report`` class uses another class, ``Row``, that is fully defined (and tested) for you. This class represents a single row in your report, and the report class will hold a list of the row instances.
31+
32+
There are big advantages to using a class like ``Row`` rather than a simple dictionary. This design creates a clear contract about what is part of a row, whereas with a dictionary you can put anything in it, so it is harder to catch when a key is misspelled or missing.
2133

22-
The ``Report`` class uses another class that is fully defined for you,
23-
the ``Row`` class. This class represents a single row in your report, and the report class will hold a list of the row instances.
24-
There are big advantages to using a class like ``Row`` in contrast to a simple dictionary, this design creates a clear contract on what's expected to be as part of a row, where with a dictionary it is easy to misspell or miss a key.
2534
A class like ``Row`` is often called a "data class" (since it only holds "data" and doesn't actually have any logic to it).
2635

27-
This is such a popular pattern that python introduced ``dataclasses`` in
28-
`version 3.7 <https://www.python.org/dev/peps/pep-0557>`_ to make this even simpler. You do not use them in this assignment but you should know that they exist and why.
36+
This is such a popular pattern that Python introduced ``dataclasses`` in
37+
`version 3.7 <https://www.python.org/dev/peps/pep-0557>`_ to make this even simpler.
38+
You do not use them in this assignment but you should know that they exist and why.
2939

3040
You can read more about ``dataclasses`` in the Python docs here:
3141

‎source/exercises/oo_intro/report.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Row:
1515
with ID, first name, last name and state attributes
1616
"""
1717
def __init__(self, fname, lname, state):
18-
self.row_id = str(uuid4()) # randomly generated unique ID
18+
self.id = str(uuid4()) # randomly generated unique ID
1919
self.fname = fname
2020
self.lname = lname
2121
self.state = state
@@ -30,11 +30,11 @@ def __init__(self, limit: int):
3030
self.rows = []
3131

3232
def add_row(self, row):
33-
"""Add a row object to the list"""
33+
"""Add a row object to the report"""
3434
pass
3535

3636
def remove_row(self, row_id):
37-
"""Remove a row object by the row ID"""
37+
"""Remove a row object by the row's ID"""
3838
pass
3939

4040
def size(self):
Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,50 @@
11
"""
22
test code for the Report class(es)
3-
"""
3+
"""
4+
5+
from report import Row, Report
6+
7+
8+
def populate_report(report):
9+
"""
10+
utility function to populate a Report with some data
11+
12+
:param report: the report object to populate
13+
14+
The Report will be populated in place
15+
"""
16+
report.add_row(Row("Natasha", "Smith", "WA"))
17+
report.add_row(Row("Devin", "Lei", "WA"))
18+
report.add_row(Row("Bob", "Li", "CA"))
19+
report.add_row(Row("Tracy", "Jones", "OR"))
20+
report.add_row(Row("Johnny", "Jakes", "WA"))
21+
report.add_row(Row("Derek", "Wright", "WA"))
22+
report.add_row(Row("Jordan", "Cooper", "WA"))
23+
report.add_row(Row("Mike", "Wong", "WA"))
24+
25+
26+
def test_row_init():
27+
"""You can initialize a Row, and it stores the attributes"""
28+
29+
row1 = Row("Joe", "Camel", "WA")
30+
31+
assert row1.fname == "Joe"
32+
assert row1.lname == "Camel"
33+
assert row1.state == "WA"
34+
35+
36+
def test_row_id_unique():
37+
""" two Rows should have unique IDs """
38+
row1 = Row("Joe", "Camel", "WA")
39+
row2 = Row("Bob", "Camel", "WA")
40+
41+
assert row1.id != row2.id
42+
43+
44+
def test_report_length():
45+
report = Report(4)
46+
populate_report(report)
47+
48+
# the test data has 8 rows
49+
assert report.size() == 8
50+

0 commit comments

Comments
 (0)