Skip to content

Commit 2bfd21a

Browse files
committed
updated oo_intro example code
1 parent 01a8dcd commit 2bfd21a

File tree

2 files changed

+56
-31
lines changed

2 files changed

+56
-31
lines changed

source/exercises/oo_intro/report.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def __str__(self):
2525

2626

2727
class Report:
28-
def __init__(self, limit: int):
28+
def __init__(self, limit):
2929
self.limit = limit
3030
self.rows = []
3131

@@ -44,7 +44,8 @@ def size(self):
4444
def get_number_of_pages(self):
4545
"""
4646
Get how many pages the report has; this will be based on limit variable.
47-
If your limit=4 and rows list has 6 records then there are two pages: page1 has 4 records, page2 has 2 records
47+
If your limit=4 and rows list has 6 records then there are two pages:
48+
page1 has 4 records, page2 has 2 records
4849
hint: you'll want to round up
4950
"""
5051
pass
@@ -75,43 +76,43 @@ def get_paged_rows(self, sort_field, page):
7576
pass
7677

7778

78-
if __name__ == "__main__":
79-
80-
report = Report(4)
79+
def run_report(sort_field):
80+
print(f"... PAGED REPORT SORTED BY: '{sort_field}'...")
81+
page = 1
82+
while True:
83+
rows = report.get_paged_rows(sort_field, page=page)
8184

82-
report.add_row(Row("natasha", "smith", "WA"))
83-
report.add_row(Row("devin", "lei", "WA"))
84-
report.add_row(Row("bob", "li", "CA"))
85-
report.add_row(Row("tracy", "jones", "OR"))
86-
report.add_row(Row("johny", "jakes", "WA"))
87-
report.add_row(Row("derek", "wright", "WA"))
85+
if not rows:
86+
break
8887

88+
input(f"Press ENTER to see page {page}")
8989

90-
def run_report(sort_field):
91-
print(f"... PAGED REPORT SORTED BY: '{sort_field}'...")
92-
page = 1
93-
while True:
94-
rows = report.get_paged_rows(sort_field, page=page)
90+
print(f"PAGE: {page} of {report.get_number_of_pages()}")
91+
print("---------------------------------------------------------------")
9592

96-
if not rows:
97-
break
93+
for row in rows:
94+
print(row)
9895

99-
input(f"Press ENTER to see page {page}")
96+
print("---------------------------------------------------------------")
10097

101-
print(f"PAGE: {page} of {report.get_number_of_pages()}")
102-
print("---------------------------------------------------------------")
98+
page += 1
10399

104-
for row in rows:
105-
print(row)
106100

107-
print("---------------------------------------------------------------")
101+
if __name__ == "__main__":
108102

109-
page += 1
103+
report = Report(4)
110104

105+
report.add_row(Row("natasha", "smith", "WA"))
106+
report.add_row(Row("devin", "lei", "WA"))
107+
report.add_row(Row("bob", "li", "CA"))
108+
report.add_row(Row("tracy", "jones", "OR"))
109+
report.add_row(Row("johny", "jakes", "WA"))
110+
report.add_row(Row("derek", "wright", "WA"))
111111

112112
run_report("fname")
113113

114-
print(f"\n\nRemoving student: {report.rows[1].fname} [{report.rows[1].row_id}]... \n\n")
114+
print("\n\nRemoving student: "
115+
f"{report.rows[1].fname} [{report.rows[1].row_id}]... \n\n")
115116

116117
report.remove_row(report.rows[1].row_id)
117118

source/exercises/oo_intro/test_report.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,20 @@
55
from report import Row, Report
66

77

8+
def example_report(report):
9+
"""
10+
utility function to provide a fresh report to test with
11+
"""
12+
report = Report(limit=4)
13+
14+
populate_report(report)
15+
return report
16+
17+
818
def populate_report(report):
919
"""
10-
utility function to populate a Report with some data
20+
utility function to populate an existing Report with
21+
some additional data
1122
1223
:param report: the report object to populate
1324
@@ -24,8 +35,9 @@ def populate_report(report):
2435

2536

2637
def test_row_init():
27-
"""You can initialize a Row, and it stores the attributes"""
28-
38+
"""
39+
test that a new row has the proper attributes initialized
40+
"""
2941
row1 = Row("Joe", "Camel", "WA")
3042

3143
assert row1.fname == "Joe"
@@ -42,9 +54,21 @@ def test_row_id_unique():
4254

4355

4456
def test_report_length():
45-
report = Report(4)
46-
populate_report(report)
57+
"""
58+
test report size method
59+
"""
60+
report = example_report()
4761

4862
# the test data has 8 rows
4963
assert report.size() == 8
5064

65+
66+
def test_number_of_pages():
67+
"""
68+
check that the number of pages is correct
69+
"""
70+
report = example_report()
71+
72+
assert report.get_number_of_pages() == 2
73+
74+

0 commit comments

Comments
 (0)