Skip to content

Commit 3e40a4f

Browse files
committed
Added instructions append for error handling and updated exemplar, stub, and test files.
1 parent 0f797a8 commit 3e40a4f

File tree

4 files changed

+37
-23
lines changed

4 files changed

+37
-23
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Instructions append
2+
3+
## Exception messages
4+
5+
Sometimes it is necessary to [raise an exception](https://docs.python.org/3/tutorial/errors.html#raising-exceptions). When you do this, you should always include a **meaningful error message** to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. For situations where you know that the error source will be a certain type, you can choose to raise one of the [built in error types](https://docs.python.org/3/library/exceptions.html#base-classes), but should still include a meaningful message.
6+
7+
This particular exercise requires that you refactor how/where the [raise statement](https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement) is used to "throw" a `ValueError` for invalid tree input. The tests will only pass if the code both `raise`s the appropriate `exception` and includes an appropriate message with it.

exercises/practice/tree-building/.meta/example.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ def __init__(self, node_id):
1515

1616
def validateRecord(record):
1717
if record.equal_id() and record.record_id != 0:
18-
raise ValueError("Only root should have equal record and parent id")
19-
elif not record.equal_id() and record.parent_id >= record.record_id:
20-
raise ValueError("Node record_id should be smaller than its parent_id")
18+
raise ValueError("Only root should have equal record and parent id.")
19+
20+
if not record.equal_id() and record.parent_id >= record.record_id:
21+
raise ValueError("Node record_id should be smaller than it's parent_id.")
2122

2223

2324
def BuildTree(records):
2425
parent_dict = {}
2526
node_dict = {}
2627
ordered_id = sorted((i.record_id for i in records))
28+
2729
for record in records:
2830
validateRecord(record)
2931
parent_dict[record.record_id] = record.parent_id
@@ -34,7 +36,8 @@ def BuildTree(records):
3436

3537
for index, record_id in enumerate(ordered_id):
3638
if index != record_id:
37-
raise ValueError("Record id is invalid or out of order")
39+
raise ValueError("Record id is invalid or out of order.")
40+
3841
if record_id == root_id:
3942
root = node_dict[record_id]
4043
else:

exercises/practice/tree-building/tree_building.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@ def BuildTree(records):
1616
ordered_id = [i.record_id for i in records]
1717
if records:
1818
if ordered_id[-1] != len(ordered_id) - 1:
19-
raise ValueError('Tree must be continuous')
19+
raise ValueError('broken tree')
2020
if ordered_id[0] != 0:
21-
raise ValueError('Tree must start with id 0')
21+
raise ValueError('invalid')
2222
trees = []
2323
parent = {}
2424
for i in range(len(ordered_id)):
2525
for j in records:
2626
if ordered_id[i] == j.record_id:
2727
if j.record_id == 0:
2828
if j.parent_id != 0:
29-
raise ValueError('Root node cannot have a parent')
29+
raise ValueError('error!')
3030
if j.record_id < j.parent_id:
31-
raise ValueError('Parent id must be lower than child id')
31+
raise ValueError('something went wrong!')
3232
if j.record_id == j.parent_id:
3333
if j.record_id != 0:
34-
raise ValueError('Tree is a cycle')
34+
raise ValueError('error!')
3535
trees.append(Node(ordered_id[i]))
3636
for i in range(len(ordered_id)):
3737
for j in trees:

exercises/practice/tree-building/tree_building_test.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,21 @@ def test_root_node_has_parent(self):
108108
Record(1, 0)
109109
]
110110
# Root parent_id should be equal to record_id(0)
111-
with self.assertRaisesWithMessage(ValueError):
111+
with self.assertRaises(ValueError) as err:
112112
BuildTree(records)
113+
self.assertEqual(type(err.exception), ValueError)
114+
self.assertEqual(err.exception.args[0], "Node record_id should be smaller than it's parent_id.")
113115

114116
def test_no_root_node(self):
115117
records = [
116118
Record(1, 0),
117119
Record(2, 0)
118120
]
119121
# Record with record_id 0 (root) is missing
120-
with self.assertRaisesWithMessage(ValueError):
122+
with self.assertRaises(ValueError) as err:
121123
BuildTree(records)
124+
self.assertEqual(type(err.exception), ValueError)
125+
self.assertEqual(err.exception.args[0], "Record id is invalid or out of order.")
122126

123127
def test_non_continuous(self):
124128
records = [
@@ -128,8 +132,10 @@ def test_non_continuous(self):
128132
Record(0, 0)
129133
]
130134
# Record with record_id 3 is missing
131-
with self.assertRaisesWithMessage(ValueError):
135+
with self.assertRaises(ValueError) as err:
132136
BuildTree(records)
137+
self.assertEqual(type(err.exception), ValueError)
138+
self.assertEqual(err.exception.args[0], "Record id is invalid or out of order.")
133139

134140
def test_cycle_directly(self):
135141
records = [
@@ -142,8 +148,10 @@ def test_cycle_directly(self):
142148
Record(6, 3)
143149
]
144150
# Cycle caused by Record 2 with parent_id pointing to itself
145-
with self.assertRaisesWithMessage(ValueError):
151+
with self.assertRaises(ValueError) as err:
146152
BuildTree(records)
153+
self.assertEqual(type(err.exception), ValueError)
154+
self.assertEqual(err.exception.args[0], "Only root should have equal record and parent id.")
147155

148156
def test_cycle_indirectly(self):
149157
records = [
@@ -156,8 +164,10 @@ def test_cycle_indirectly(self):
156164
Record(6, 3)
157165
]
158166
# Cycle caused by Record 2 with parent_id(6) greater than record_id(2)
159-
with self.assertRaisesWithMessage(ValueError):
167+
with self.assertRaises(ValueError) as err:
160168
BuildTree(records)
169+
self.assertEqual(type(err.exception), ValueError)
170+
self.assertEqual(err.exception.args[0], "Node record_id should be smaller than it's parent_id.")
161171

162172
def test_higher_id_parent_of_lower_id(self):
163173
records = [
@@ -166,8 +176,10 @@ def test_higher_id_parent_of_lower_id(self):
166176
Record(1, 2)
167177
]
168178
# Record 1 have parent_id(2) greater than record_id(1)
169-
with self.assertRaisesWithMessage(ValueError):
179+
with self.assertRaises(ValueError) as err:
170180
BuildTree(records)
181+
self.assertEqual(type(err.exception), ValueError)
182+
self.assertEqual(err.exception.args[0], "Node record_id should be smaller than it's parent_id.")
171183

172184
def assert_node_is_branch(self, node, node_id, children_count):
173185
self.assertEqual(node.node_id, node_id)
@@ -177,11 +189,3 @@ def assert_node_is_branch(self, node, node_id, children_count):
177189
def assert_node_is_leaf(self, node, node_id):
178190
self.assertEqual(node.node_id, node_id)
179191
self.assertEqual(len(node.children), 0)
180-
181-
# Utility functions
182-
def assertRaisesWithMessage(self, exception):
183-
return self.assertRaisesRegex(exception, r".+")
184-
185-
186-
if __name__ == '__main__':
187-
unittest.main()

0 commit comments

Comments
 (0)