Skip to content

Commit f9ac0b4

Browse files
committed
Added instructions append for error handling, updated JinJa2 template and example, and regenerated test files.
1 parent 1090a72 commit f9ac0b4

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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 use the [raise statement](https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement) to "throw" a `ValueError` when the strands being checked are not the same length. The tests will only pass if you both `raise` the `exception` and include a message with it.
8+
9+
To raise a `ValueError` with a message, write the message as an argument to the `exception` type:
10+
11+
```python
12+
# When the sequences being passed are not the same length.
13+
raise ValueError("Strands must be of equal length.")
14+
```
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
def distance(s1, s2):
22
if len(s1) != len(s2):
3-
raise ValueError("Sequences not of equal length.")
3+
raise ValueError("Strands must be of equal length.")
44

55
return sum(a != b for a, b in zip(s1, s2))

exercises/practice/hamming/.meta/template.j2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ class {{ exercise | camel_case }}Test(unittest.TestCase):
1212
{% for case in cases -%}
1313
def test_{{ case["description"] | to_snake }}(self):
1414
{%- if case is error_case %}
15-
with self.assertRaisesWithMessage(ValueError):
15+
with self.assertRaises(ValueError) as err:
1616
{{- test_call(case) }}
17+
self.assertEqual(type(err.exception), ValueError)
18+
self.assertEqual(err.exception.args[0], "Strands must be of equal length.")
1719
{%- else %}
1820
self.assertEqual(
1921
{{- test_call(case) }},
2022
{{ case["expected"] }}
2123
)
2224
{%- endif %}
2325
{% endfor %}
24-
25-
{{ macros.footer() }}

exercises/practice/hamming/hamming_test.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,29 @@ def test_long_different_strands(self):
2424
self.assertEqual(distance("GGACGGATTCTG", "AGGACGGATTCT"), 9)
2525

2626
def test_disallow_first_strand_longer(self):
27-
with self.assertRaisesWithMessage(ValueError):
27+
with self.assertRaises(ValueError) as err:
2828
distance("AATG", "AAA")
2929

30+
self.assertEqual(type(err.exception), ValueError)
31+
self.assertEqual(err.exception.args[0], "Strands must be of equal length.")
32+
3033
def test_disallow_second_strand_longer(self):
31-
with self.assertRaisesWithMessage(ValueError):
34+
with self.assertRaises(ValueError) as err:
3235
distance("ATA", "AGTG")
3336

37+
self.assertEqual(type(err.exception), ValueError)
38+
self.assertEqual(err.exception.args[0], "Strands must be of equal length.")
39+
3440
def test_disallow_left_empty_strand(self):
35-
with self.assertRaisesWithMessage(ValueError):
41+
with self.assertRaises(ValueError) as err:
3642
distance("", "G")
3743

44+
self.assertEqual(type(err.exception), ValueError)
45+
self.assertEqual(err.exception.args[0], "Strands must be of equal length.")
46+
3847
def test_disallow_right_empty_strand(self):
39-
with self.assertRaisesWithMessage(ValueError):
48+
with self.assertRaises(ValueError) as err:
4049
distance("G", "")
4150

42-
# Utility functions
43-
def assertRaisesWithMessage(self, exception):
44-
return self.assertRaisesRegex(exception, r".+")
45-
46-
47-
if __name__ == "__main__":
48-
unittest.main()
51+
self.assertEqual(type(err.exception), ValueError)
52+
self.assertEqual(err.exception.args[0], "Strands must be of equal length.")

0 commit comments

Comments
 (0)