Skip to content

Commit f4f20bb

Browse files
committed
Added error handling instruction append, added additional test cases, corrected JinJa2 template, and regenerated test cases.
1 parent 3a1ac3d commit f4f20bb

File tree

5 files changed

+37
-19
lines changed

5 files changed

+37
-19
lines changed
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1-
# Hints
1+
# Instructions append
22

3-
Your converter should validate its input and raise a ValueError with a meaningful message if it receives a string that isn't a valid OCR number.
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 `convert()` function receives a string that isn't a valid OCR number. 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 rows aren't multiples of 4
13+
raise ValueError("Number of input lines is not a multiple of four")
14+
15+
# when the columns aren't multiples of 3
16+
raise ValueError("Number of input columns is not a multiple of three")
17+
```

exercises/practice/ocr-numbers/.meta/example.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ def convert(input_grid):
2727

2828

2929
def convert_one_line(input_grid):
30-
if (len(input_grid) != NUM_ROWS or len(input_grid[0]) % NUM_COLS or
31-
any(len(r) != len(input_grid[0]) for r in input_grid)):
32-
raise ValueError('Wrong grid size.')
30+
if len(input_grid) != NUM_ROWS:
31+
raise ValueError("Number of input lines is not a multiple of four")
32+
33+
if len(input_grid[0]) % NUM_COLS:
34+
raise ValueError("Number of input columns is not a multiple of three")
35+
3336
numbers = split_ocr(input_grid)
3437
digits = ''
3538
for n in numbers:

exercises/practice/ocr-numbers/.meta/template.j2

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ class {{ exercise | camel_case }}Test(unittest.TestCase):
66
def test_{{ case["description"] | to_snake }}(self):
77
{% set candidate = case["input"]["rows"] -%}
88
{% set output = case["expected"] -%}
9-
{% if output is mapping -%}
10-
with self.assertRaisesWithMessage(ValueError):
9+
{% if case is error_case -%}
10+
with self.assertRaises(ValueError) as err:
1111
{{ case["property"] | to_snake }}({{ candidate }})
12+
self.assertEqual(type(err.exception), ValueError)
13+
self.assertEqual(err.exception.args[0], "{{ case["expected"]["error"] }}")
1214
{% else -%}
1315
{% set expected = case["expected"] -%}
1416
self.assertEqual({{ case["property"] | to_snake }}({{ candidate }}), "{{ expected }}")
1517
{% endif %}
1618

1719
{% endfor %}
18-
19-
{{ macros.footer(True) }}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
def convert(input_grid):
22
pass
3+

exercises/practice/ocr-numbers/ocr_numbers_test.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,22 @@ def test_unreadable_but_correctly_sized_inputs_return(self):
2020
def test_input_with_a_number_of_lines_that_is_not_a_multiple_of_four_raises_an_error(
2121
self,
2222
):
23-
with self.assertRaisesWithMessage(ValueError):
23+
with self.assertRaises(ValueError) as err:
2424
convert([" _ ", "| |", " "])
25+
self.assertEqual(type(err.exception), ValueError)
26+
self.assertEqual(
27+
err.exception.args[0], "Number of input lines is not a multiple of four"
28+
)
2529

2630
def test_input_with_a_number_of_columns_that_is_not_a_multiple_of_three_raises_an_error(
2731
self,
2832
):
29-
with self.assertRaisesWithMessage(ValueError):
33+
with self.assertRaises(ValueError) as err:
3034
convert([" ", " |", " |", " "])
35+
self.assertEqual(type(err.exception), ValueError)
36+
self.assertEqual(
37+
err.exception.args[0], "Number of input columns is not a multiple of three"
38+
)
3139

3240
def test_recognizes_110101100(self):
3341
self.assertEqual(
@@ -114,11 +122,3 @@ def test_numbers_separated_by_empty_lines_are_recognized_lines_are_joined_by_com
114122
),
115123
"123,456,789",
116124
)
117-
118-
# Utility functions
119-
def assertRaisesWithMessage(self, exception):
120-
return self.assertRaisesRegex(exception, r".+")
121-
122-
123-
if __name__ == "__main__":
124-
unittest.main()

0 commit comments

Comments
 (0)