diff --git a/docs/EXERCISE_README_INSERT.md b/docs/EXERCISE_README_INSERT.md index 1e4f9a0d9b..dbf5c48bb0 100644 --- a/docs/EXERCISE_README_INSERT.md +++ b/docs/EXERCISE_README_INSERT.md @@ -1,3 +1,18 @@ +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/accumulate/README.md b/exercises/accumulate/README.md index d801cda1d1..3397a59bc6 100644 --- a/exercises/accumulate/README.md +++ b/exercises/accumulate/README.md @@ -25,6 +25,21 @@ Keep your hands off that collect/map/fmap/whatchamacallit functionality provided by your standard library! Solve this one yourself using other basic tools instead. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/acronym/README.md b/exercises/acronym/README.md index d172c2c75d..a11137c1ec 100644 --- a/exercises/acronym/README.md +++ b/exercises/acronym/README.md @@ -7,6 +7,21 @@ Techies love their TLA (Three Letter Acronyms)! Help generate some jargon by writing a program that converts a long name like Portable Network Graphics to its acronym (PNG). +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/all-your-base/README.md b/exercises/all-your-base/README.md index 5d4a43a637..355ce58aa8 100644 --- a/exercises/all-your-base/README.md +++ b/exercises/all-your-base/README.md @@ -31,6 +31,21 @@ I think you got the idea! *Yes. Those three numbers above are exactly the same. Congratulations!* +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/all-your-base/all_your_base_test.py b/exercises/all-your-base/all_your_base_test.py index b8166e40cb..c5fc95536a 100644 --- a/exercises/all-your-base/all_your_base_test.py +++ b/exercises/all-your-base/all_your_base_test.py @@ -44,41 +44,51 @@ def test_leading_zeros(self): self.assertEqual(rebase(7, [0, 6, 0], 10), [4, 2]) def test_first_base_is_one(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): rebase(1, [], 10) def test_first_base_is_zero(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): rebase(0, [], 10) def test_first_base_is_negative(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): rebase(-2, [1], 10) def test_negative_digit(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): rebase(2, [1, -1, 1, 0, 1, 0], 10) def test_invalid_positive_digit(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): rebase(2, [1, 2, 1, 0, 1, 0], 10) def test_second_base_is_one(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): rebase(2, [1, 0, 1, 0, 1, 0], 1) def test_second_base_is_zero(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): rebase(10, [7], 0) def test_second_base_is_negative(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): rebase(2, [1], -7) def test_both_bases_are_negative(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): rebase(-2, [1], -7) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/allergies/README.md b/exercises/allergies/README.md index bc0814567f..78ef4ff75f 100644 --- a/exercises/allergies/README.md +++ b/exercises/allergies/README.md @@ -29,6 +29,21 @@ allergens that score 256, 512, 1024, etc.). Your program should ignore those components of the score. For example, if the allergy score is 257, your program should only report the eggs (1) allergy. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/alphametics/README.md b/exercises/alphametics/README.md index 96ba3a2b84..26c778f24b 100644 --- a/exercises/alphametics/README.md +++ b/exercises/alphametics/README.md @@ -31,6 +31,21 @@ a multi-digit number must not be zero. Write a function to solve alphametics puzzles. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/anagram/README.md b/exercises/anagram/README.md index 35f96af1b8..0c3f5d318d 100644 --- a/exercises/anagram/README.md +++ b/exercises/anagram/README.md @@ -6,6 +6,21 @@ Given `"listen"` and a list of candidates like `"enlists" "google" "inlets" "banana"` the program should return a list containing `"inlets"`. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/atbash-cipher/README.md b/exercises/atbash-cipher/README.md index 40f0d0f76c..4180c76de5 100644 --- a/exercises/atbash-cipher/README.md +++ b/exercises/atbash-cipher/README.md @@ -28,6 +28,21 @@ things based on word boundaries. - Decoding `gvhg` gives `test` - Decoding `gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt` gives `thequickbrownfoxjumpsoverthelazydog` +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/beer-song/README.md b/exercises/beer-song/README.md index 6cbb8668ad..d152cbbc02 100644 --- a/exercises/beer-song/README.md +++ b/exercises/beer-song/README.md @@ -1,6 +1,6 @@ # Beer Song -Produce the lyrics to that beloved classic, that field-trip favorite: 99 Bottles of Beer on the Wall. +Recite the lyrics to that beloved classic, that field-trip favorite: 99 Bottles of Beer on the Wall. Note that not all verses are identical. @@ -320,6 +320,21 @@ are some additional things you could try: Then please share your thoughts in a comment on the submission. Did this experiment make the code better? Worse? Did you learn anything from it? +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/binary-search-tree/README.md b/exercises/binary-search-tree/README.md index cbf5c0fa56..408e7fd3e8 100644 --- a/exercises/binary-search-tree/README.md +++ b/exercises/binary-search-tree/README.md @@ -53,6 +53,20 @@ And if we then added 1, 5, and 7, it would look like this / \ / \ 1 3 5 7 +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + ## Submitting Exercises @@ -65,7 +79,7 @@ please see the [help page](http://exercism.io/languages/python). ## Source -Wikipedia [https://en.wikipedia.org/wiki/Binary_search_tree](https://en.wikipedia.org/wiki/Binary_search_tree) +Josh Cheek [https://twitter.com/josh_cheek](https://twitter.com/josh_cheek) ## Submitting Incomplete Solutions -It's possible to submit an incomplete solution so you can see how others have completed the exercise. \ No newline at end of file +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/exercises/binary-search/README.md b/exercises/binary-search/README.md index eae22e71b5..814177ee94 100644 --- a/exercises/binary-search/README.md +++ b/exercises/binary-search/README.md @@ -34,6 +34,21 @@ A binary search halves the number of items to check with each iteration, so locating an item (or determining its absence) takes logarithmic time. A binary search is a dichotomic divide and conquer search algorithm. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/binary-search/binary_search_test.py b/exercises/binary-search/binary_search_test.py index 581e2fbc48..eb3f8174a4 100644 --- a/exercises/binary-search/binary_search_test.py +++ b/exercises/binary-search/binary_search_test.py @@ -29,21 +29,31 @@ def test_finds_value_in_array_of_even_length(self): 5) def test_identifies_value_missing(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): binary_search([1, 3, 4, 6, 8, 9, 11], 7) def test_value_smaller_than_arrays_minimum(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): binary_search([1, 3, 4, 6, 8, 9, 11], 0) def test_value_larger_than_arrays_maximum(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): binary_search([1, 3, 4, 6, 8, 9, 11], 13) def test_empty_array(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): binary_search([], 1) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/binary/README.md b/exercises/binary/README.md index 9643543cba..628adde4ca 100644 --- a/exercises/binary/README.md +++ b/exercises/binary/README.md @@ -30,6 +30,21 @@ Binary is similar, but uses powers of 2 rather than powers of 10. So: `101 => 1*2^2 + 0*2^1 + 1*2^0 => 1*4 + 0*2 + 1*1 => 4 + 1 => 5 base 10`. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/binary/binary_test.py b/exercises/binary/binary_test.py index 28ce8fcdc0..510cdc5201 100644 --- a/exercises/binary/binary_test.py +++ b/exercises/binary/binary_test.py @@ -32,21 +32,31 @@ def test_binary_10001101000_is_decimal_1128(self): self.assertEqual(parse_binary("10001101000"), 1128) def test_invalid_binary_text_only(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): parse_binary("carrot") def test_invalid_binary_number_not_base2(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): parse_binary("102011") def test_invalid_binary_numbers_with_text(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): parse_binary("10nope") def test_invalid_binary_text_with_numbers(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): parse_binary("nope10") + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/bob/README.md b/exercises/bob/README.md index 181f8a6f72..e62455f9cf 100644 --- a/exercises/bob/README.md +++ b/exercises/bob/README.md @@ -11,6 +11,21 @@ anything. He answers 'Whatever.' to anything else. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/book-store/README.md b/exercises/book-store/README.md index 7672c9d101..81c6d42553 100644 --- a/exercises/book-store/README.md +++ b/exercises/book-store/README.md @@ -67,6 +67,21 @@ For a total of $51.20 And $51.20 is the price with the biggest discount. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/bracket-push/README.md b/exercises/bracket-push/README.md index a1afd7efd2..3f44962fb9 100644 --- a/exercises/bracket-push/README.md +++ b/exercises/bracket-push/README.md @@ -3,6 +3,21 @@ Given a string containing brackets `[]`, braces `{}` and parentheses `()`, verify that all the pairs are matched and nested correctly. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/change/README.md b/exercises/change/README.md index 9375ea3045..4fcf70451a 100644 --- a/exercises/change/README.md +++ b/exercises/change/README.md @@ -16,6 +16,21 @@ that the sum of the coins' value would equal the correct amount of change. - Can you ask for negative change? - Can you ask for a change value smaller than the smallest coin value? +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/circular-buffer/README.md b/exercises/circular-buffer/README.md index 5f27d2d6b0..f833d7b50e 100644 --- a/exercises/circular-buffer/README.md +++ b/exercises/circular-buffer/README.md @@ -50,6 +50,21 @@ the buffer is once again full. [D][7][8][9][A][B][C] +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/circular-buffer/circular_buffer_test.py b/exercises/circular-buffer/circular_buffer_test.py index 54cc4a7f03..27382cda6f 100644 --- a/exercises/circular-buffer/circular_buffer_test.py +++ b/exercises/circular-buffer/circular_buffer_test.py @@ -12,7 +12,7 @@ class CircularBufferTest(unittest.TestCase): def test_read_empty_buffer(self): buf = CircularBuffer(1) - with self.assertRaises(BufferEmptyException): + with self.assertRaisesWithMessage(BufferEmptyException): buf.read() def test_read_just_written_item(self): @@ -24,7 +24,7 @@ def test_write_and_read_back_one_item(self): buf = CircularBuffer(1) buf.write('1') self.assertEqual(buf.read(), '1') - with self.assertRaises(BufferEmptyException): + with self.assertRaisesWithMessage(BufferEmptyException): buf.read() def test_write_and_read_back_multiple_items_ordered(self): @@ -37,7 +37,7 @@ def test_write_and_read_back_multiple_items_ordered(self): def test_full_buffer_cant_written(self): buf = CircularBuffer(1) buf.write('1') - with self.assertRaises(BufferFullException): + with self.assertRaisesWithMessage(BufferFullException): buf.write('2') def test_alternate_write_and_read(self): @@ -60,7 +60,7 @@ def test_clearing_buffer(self): buf = CircularBuffer(1) buf.write('1') buf.clear() - with self.assertRaises(BufferEmptyException): + with self.assertRaisesWithMessage(BufferEmptyException): buf.read() def test_clear_free_buffer_for_write(self): @@ -95,7 +95,7 @@ def test_write_full_buffer(self): buf = CircularBuffer(2) buf.write('1') buf.write('2') - with self.assertRaises(BufferFullException): + with self.assertRaisesWithMessage(BufferFullException): buf.write('A') def test_over_write_replaces_oldest_remaning_item(self): @@ -110,6 +110,16 @@ def test_over_write_replaces_oldest_remaning_item(self): self.assertEqual(buf.read(), '4') self.assertEqual(buf.read(), '5') + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/circular-buffer/example.py b/exercises/circular-buffer/example.py index 857daf8538..a5cfaff542 100644 --- a/exercises/circular-buffer/example.py +++ b/exercises/circular-buffer/example.py @@ -25,7 +25,7 @@ def clear(self): def write(self, data): if all(self.buffer): - raise BufferFullException + raise BufferFullException("Circular buffer is full") self._update_buffer(data) self.write_point = (self.write_point + 1) % len(self.buffer) @@ -37,7 +37,7 @@ def overwrite(self, data): def read(self): if not any(self.buffer): - raise BufferEmptyException + raise BufferEmptyException("Circular buffer is empty") data = chr(self.buffer[self.read_point]) self.buffer[self.read_point] = 0 self.read_point = (self.read_point + 1) % len(self.buffer) diff --git a/exercises/clock/README.md b/exercises/clock/README.md index 6c945a0224..ff51b21338 100644 --- a/exercises/clock/README.md +++ b/exercises/clock/README.md @@ -6,6 +6,21 @@ You should be able to add and subtract minutes to it. Two clocks that represent the same time should be equal to each other. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/collatz-conjecture/.meta/hints.md b/exercises/collatz-conjecture/.meta/hints.md index 47ebd98f6d..51b5fde426 100644 --- a/exercises/collatz-conjecture/.meta/hints.md +++ b/exercises/collatz-conjecture/.meta/hints.md @@ -1,3 +1,3 @@ ## Notes -The Collatz Conjecture is only concerned with strictly positive integers, so your solution should return `None` if given 0 or a negative integer. +The Collatz Conjecture is only concerned with strictly positive integers, so your solution should raise a `ValueError` with a meaningful message if given 0 or a negative integer. diff --git a/exercises/collatz-conjecture/README.md b/exercises/collatz-conjecture/README.md index a4f625424a..b31582485a 100644 --- a/exercises/collatz-conjecture/README.md +++ b/exercises/collatz-conjecture/README.md @@ -31,6 +31,21 @@ Resulting in 9 steps. So for input n = 12, the return value would be 9. The Collatz Conjecture is only concerned with strictly positive integers, so your solution should raise a `ValueError` with a meaningful message if given 0 or a negative integer. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/complex-numbers/README.md b/exercises/complex-numbers/README.md index 63999ab85d..7e3c9a2fd8 100644 --- a/exercises/complex-numbers/README.md +++ b/exercises/complex-numbers/README.md @@ -31,6 +31,21 @@ Implement the following operations: Assume the programming language you are using does not have an implementation of complex numbers. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/connect/README.md b/exercises/connect/README.md index 4eb6d1a9d5..56b5d4da4d 100644 --- a/exercises/connect/README.md +++ b/exercises/connect/README.md @@ -18,7 +18,7 @@ computes the winner (or lack thereof). Note that all games need not be "fair". The boards look like this (with spaces added for readability, which won't be in the representation passed to your code): -``` +```text . O . X . . X X O . O O O X . @@ -30,15 +30,30 @@ the representation passed to your code): the above example `O` has made a connection from left to right but nobody has won since `O` didn't connect top and bottom. -### Submitting Exercises +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + +## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. For example, if you're submitting `bob.py` for the Bob exercise, the submit command would be something like `exercism submit /python/bob/bob.py`. - For more detailed information about running tests, code style and linting, please see the [help page](http://exercism.io/languages/python). + ## Submitting Incomplete Solutions It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/exercises/crypto-square/README.md b/exercises/crypto-square/README.md index 669cb8762d..cd8b09155e 100644 --- a/exercises/crypto-square/README.md +++ b/exercises/crypto-square/README.md @@ -69,6 +69,21 @@ aohghn sseoau ``` +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/diamond/README.md b/exercises/diamond/README.md index d79e998476..7fea061bfe 100644 --- a/exercises/diamond/README.md +++ b/exercises/diamond/README.md @@ -52,6 +52,21 @@ E·······E ····A···· ``` +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/difference-of-squares/README.md b/exercises/difference-of-squares/README.md index 84015f4cc3..2ee2349fa2 100644 --- a/exercises/difference-of-squares/README.md +++ b/exercises/difference-of-squares/README.md @@ -12,6 +12,21 @@ Hence the difference between the square of the sum of the first ten natural numbers and the sum of the squares of the first ten natural numbers is 3025 - 385 = 2640. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/diffie-hellman/README.md b/exercises/diffie-hellman/README.md index 5db2f7404a..6869a0721a 100644 --- a/exercises/diffie-hellman/README.md +++ b/exercises/diffie-hellman/README.md @@ -54,6 +54,20 @@ cryptographically strong random numbers that provide the greater security requir Since this is only an exercise, `random` is fine to use, but note that **it would be very insecure if actually used for cryptography.** +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + ## Submitting Exercises diff --git a/exercises/dominoes/README.md b/exercises/dominoes/README.md index a858a78c0a..24d40d8638 100644 --- a/exercises/dominoes/README.md +++ b/exercises/dominoes/README.md @@ -14,15 +14,30 @@ For stones `[1|2]`, `[4|1]` and `[2|3]` the resulting chain is not valid: `[4|1] Some test cases may use duplicate stones in a chain solution, assume that multiple Domino sets are being used. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. For example, if you're submitting `bob.py` for the Bob exercise, the submit command would be something like `exercism submit /python/bob/bob.py`. - For more detailed information about running tests, code style and linting, please see the [help page](http://exercism.io/languages/python). + ## Submitting Incomplete Solutions It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/exercises/error-handling/README.md b/exercises/error-handling/README.md index ab7f3f0621..e3505dd2ed 100644 --- a/exercises/error-handling/README.md +++ b/exercises/error-handling/README.md @@ -18,6 +18,22 @@ class implements the following methods: - `__enter__` and `__exit__`, for implicit opening and closing. - `do_something`, which may or may not throw an `Exception`. + +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. @@ -27,5 +43,6 @@ For example, if you're submitting `bob.py` for the Bob exercise, the submit comm For more detailed information about running tests, code style and linting, please see the [help page](http://exercism.io/languages/python). + ## Submitting Incomplete Solutions It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/exercises/error-handling/error_handling_test.py b/exercises/error-handling/error_handling_test.py index 2da09df96d..a7a76f7fa7 100644 --- a/exercises/error-handling/error_handling_test.py +++ b/exercises/error-handling/error_handling_test.py @@ -28,12 +28,12 @@ def __exit__(self, *args): def do_something(self): self.did_something = True if self.fail_something: - raise Exception() + raise Exception("Failed while doing something") class ErrorHandlingTest(unittest.TestCase): def test_throw_exception(self): - with self.assertRaises(Exception): + with self.assertRaisesWithMessage(Exception): er.handle_error_by_throwing_exception() def test_return_none(self): @@ -54,7 +54,7 @@ def test_return_tuple(self): def test_filelike_objects_are_closed_on_exception(self): filelike_object = FileLike(fail_something=True) - with self.assertRaises(Exception): + with self.assertRaisesWithMessage(Exception): er.filelike_objects_are_closed_on_exception(filelike_object) self.assertIs(filelike_object.is_open, False, 'filelike_object should be closed') @@ -73,6 +73,16 @@ def test_filelike_objects_are_closed_without_exception(self): self.assertIs(filelike_object.did_something, True, 'filelike_object should call do_something()') + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/error-handling/example.py b/exercises/error-handling/example.py index 8544114809..b7886de168 100644 --- a/exercises/error-handling/example.py +++ b/exercises/error-handling/example.py @@ -1,5 +1,5 @@ def handle_error_by_throwing_exception(): - raise Exception() + raise Exception("Meaningful message describing the source of the error") def handle_error_by_returning_none(input_data): diff --git a/exercises/etl/README.md b/exercises/etl/README.md index 498596d4fc..a4ad4a69d3 100644 --- a/exercises/etl/README.md +++ b/exercises/etl/README.md @@ -1,4 +1,4 @@ -# Etl +# ETL We are going to do the `Transform` step of an Extract-Transform-Load. @@ -46,6 +46,21 @@ variety of languages, each with its own unique scoring table. For example, an "E" is scored at 2 in the Māori-language version of the game while being scored at 4 in the Hawaiian-language version. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/flatten-array/README.md b/exercises/flatten-array/README.md index 96540ebdf3..d624f3c212 100644 --- a/exercises/flatten-array/README.md +++ b/exercises/flatten-array/README.md @@ -10,6 +10,21 @@ input: [1,[2,3,null,4],[null],5] output: [1,2,3,4,5] +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/food-chain/README.md b/exercises/food-chain/README.md index 22c711a711..8e6df89fc9 100644 --- a/exercises/food-chain/README.md +++ b/exercises/food-chain/README.md @@ -63,6 +63,21 @@ I know an old lady who swallowed a horse. She's dead, of course! ``` +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/forth/README.md b/exercises/forth/README.md index 2020976f71..d731177eb2 100644 --- a/exercises/forth/README.md +++ b/exercises/forth/README.md @@ -25,6 +25,21 @@ enough.) Words are case-insensitive. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/forth/example.py b/exercises/forth/example.py index 39936283ee..850ffd43a9 100644 --- a/exercises/forth/example.py +++ b/exercises/forth/example.py @@ -20,7 +20,7 @@ def evaluate(input_data): values.pop(0) key = values.pop(0) if is_integer(key): - raise ValueError() + raise ValueError("Integers cannot be redefined") defines[key] = values stack = [] input_data = input_data[-1].split() @@ -38,10 +38,10 @@ def evaluate(input_data): elif word == '*': stack.append(stack.pop() * stack.pop()) elif word == '/': - divider = stack.pop() - if divider == 0: - raise ZeroDivisionError() - stack.append(int(stack.pop() / divider)) + divisor = stack.pop() + if divisor == 0: + raise ZeroDivisionError("Attempted to divide by zero") + stack.append(int(stack.pop() / divisor)) elif word == 'dup': stack.append(stack[-1]) elif word == 'drop': @@ -52,9 +52,7 @@ def evaluate(input_data): elif word == 'over': stack.append(stack[-2]) else: - raise ValueError() - except ZeroDivisionError: - raise + raise ValueError("{} has not been defined".format(word)) except IndexError: - raise StackUnderflowError() + raise StackUnderflowError("Insufficient number of items in stack") return stack diff --git a/exercises/forth/forth_test.py b/exercises/forth/forth_test.py index 0cc7563b03..8ce629f74d 100644 --- a/exercises/forth/forth_test.py +++ b/exercises/forth/forth_test.py @@ -31,14 +31,24 @@ def test_can_add_two_numbers(self): def test_errors_if_there_is_nothing_on_the_stack(self): input_data = ["+"] - with self.assertRaises(StackUnderflowError): + with self.assertRaisesWithMessage(StackUnderflowError): evaluate(input_data) def test_errors_if_there_is_only_one_value_on_the_stack(self): input_data = ["1 +"] - with self.assertRaises(StackUnderflowError): + with self.assertRaisesWithMessage(StackUnderflowError): evaluate(input_data) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + class ForthSubtractionTest(unittest.TestCase): def test_can_subtract_two_numbers(self): @@ -48,14 +58,24 @@ def test_can_subtract_two_numbers(self): def test_errors_if_there_is_nothing_on_the_stack(self): input_data = ["-"] - with self.assertRaises(StackUnderflowError): + with self.assertRaisesWithMessage(StackUnderflowError): evaluate(input_data) def test_errors_if_there_is_only_one_value_on_the_stack(self): input_data = ["1 -"] - with self.assertRaises(StackUnderflowError): + with self.assertRaisesWithMessage(StackUnderflowError): evaluate(input_data) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + class ForthMultiplicationTest(unittest.TestCase): def test_can_multiply_two_numbers(self): @@ -65,14 +85,24 @@ def test_can_multiply_two_numbers(self): def test_errors_if_there_is_nothing_on_the_stack(self): input_data = ["*"] - with self.assertRaises(StackUnderflowError): + with self.assertRaisesWithMessage(StackUnderflowError): evaluate(input_data) def test_errors_if_there_is_only_one_value_on_the_stack(self): input_data = ["1 *"] - with self.assertRaises(StackUnderflowError): + with self.assertRaisesWithMessage(StackUnderflowError): evaluate(input_data) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + class ForthDivisionTest(unittest.TestCase): def test_can_divide_two_numbers(self): @@ -87,19 +117,29 @@ def test_performs_integer_division(self): def test_errors_if_dividing_by_zero(self): input_data = ["4 0 /"] - with self.assertRaises(ZeroDivisionError): + with self.assertRaisesWithMessage(ZeroDivisionError): evaluate(input_data) def test_errors_if_there_is_nothing_on_the_stack(self): input_data = ["/"] - with self.assertRaises(StackUnderflowError): + with self.assertRaisesWithMessage(StackUnderflowError): evaluate(input_data) def test_errors_if_there_is_only_one_value_on_the_stack(self): input_data = ["1 /"] - with self.assertRaises(StackUnderflowError): + with self.assertRaisesWithMessage(StackUnderflowError): evaluate(input_data) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + class ForthCombinedArithmeticTest(unittest.TestCase): def test_addition_and_subtraction(self): @@ -126,9 +166,19 @@ def test_is_case_insensitive(self): def test_errors_if_there_is_nothing_on_the_stack(self): input_data = ["dup"] - with self.assertRaises(StackUnderflowError): + with self.assertRaisesWithMessage(StackUnderflowError): evaluate(input_data) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + class ForthDropTest(unittest.TestCase): def test_removes_the_top_value_on_the_stack_if_it_is_the_only_one(self): @@ -148,9 +198,19 @@ def test_is_case_insensitive(self): def test_errors_if_there_is_nothing_on_the_stack(self): input_data = ["drop"] - with self.assertRaises(StackUnderflowError): + with self.assertRaisesWithMessage(StackUnderflowError): evaluate(input_data) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + class ForthSwapTest(unittest.TestCase): def test_swaps_only_two_values_on_stack(self): @@ -170,14 +230,24 @@ def test_is_case_insensitive(self): def test_errors_if_there_is_nothing_on_the_stack(self): input_data = ["swap"] - with self.assertRaises(StackUnderflowError): + with self.assertRaisesWithMessage(StackUnderflowError): evaluate(input_data) def test_errors_if_there_is_only_one_value_on_the_stack(self): input_data = ["1 swap"] - with self.assertRaises(StackUnderflowError): + with self.assertRaisesWithMessage(StackUnderflowError): evaluate(input_data) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + class ForthOverTest(unittest.TestCase): def test_copies_the_second_element_if_there_are_only_two(self): @@ -197,14 +267,24 @@ def test_is_case_insensitive(self): def test_errors_if_there_is_nothing_on_the_stack(self): input_data = ["over"] - with self.assertRaises(StackUnderflowError): + with self.assertRaisesWithMessage(StackUnderflowError): evaluate(input_data) def test_errors_if_there_is_only_one_value_on_the_stack(self): input_data = ["1 over"] - with self.assertRaises(StackUnderflowError): + with self.assertRaisesWithMessage(StackUnderflowError): evaluate(input_data) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + class ForthUserDefinedWordsTest(unittest.TestCase): def test_can_consist_of_built_in_words(self): @@ -258,14 +338,24 @@ def test_is_case_insensitive(self): def test_cannot_redefine_numbers(self): input_data = [": 1 2 ;"] - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): evaluate(input_data) def test_errors_if_executing_a_non_existent_word(self): input_data = ["foo"] - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): evaluate(input_data) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/gigasecond/README.md b/exercises/gigasecond/README.md index 5add08a5ee..7054afef7b 100644 --- a/exercises/gigasecond/README.md +++ b/exercises/gigasecond/README.md @@ -4,6 +4,21 @@ Calculate the moment when someone has lived for 10^9 seconds. A gigasecond is 10^9 (1,000,000,000) seconds. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/go-counting/README.md b/exercises/go-counting/README.md index 0d21809476..bfd4b3fa59 100644 --- a/exercises/go-counting/README.md +++ b/exercises/go-counting/README.md @@ -16,7 +16,7 @@ only horizontal and vertical neighbours count. In the following diagram the stones which matter are marked "O" and the stones that don't are marked "I" (ignored). Empty spaces represent empty intersections. -``` +```text +----+ |IOOI| |O O| @@ -33,12 +33,30 @@ For more information see [wikipedia](https://en.wikipedia.org/wiki/Go_%28game%29) or [Sensei's Library](http://senseis.xmp.net/). +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. For example, if you're submitting `bob.py` for the Bob exercise, the submit command would be something like `exercism submit /python/bob/bob.py`. +For more detailed information about running tests, code style and linting, +please see the [help page](http://exercism.io/languages/python). + + ## Submitting Incomplete Solutions -It's possible to submit an incomplete solution so you can see how others have completed the -exercise. +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/exercises/grade-school/README.md b/exercises/grade-school/README.md index 7df5b67b29..83138d7f91 100644 --- a/exercises/grade-school/README.md +++ b/exercises/grade-school/README.md @@ -34,6 +34,21 @@ are some additional things you could try: Then please share your thoughts in a comment on the submission. Did this experiment make the code better? Worse? Did you learn anything from it? +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/grains/README.md b/exercises/grains/README.md index 30a5b1d142..57660762b9 100644 --- a/exercises/grains/README.md +++ b/exercises/grains/README.md @@ -26,6 +26,21 @@ are some additional things you could try: Then please share your thoughts in a comment on the submission. Did this experiment make the code better? Worse? Did you learn anything from it? +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/grains/grains_test.py b/exercises/grains/grains_test.py index 5ce4e1e76a..9e171742e1 100644 --- a/exercises/grains/grains_test.py +++ b/exercises/grains/grains_test.py @@ -31,26 +31,36 @@ def test_square_64(self): self.assertEqual(on_square(64), 9223372036854775808) def test_square_0_raises_exception(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): on_square(0) - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): total_after(0) def test_square_negative_raises_exception(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): on_square(-1) - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): total_after(-1) def test_square_gt_64_raises_exception(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): on_square(65) - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): total_after(65) def test_total(self): self.assertEqual(total_after(64), 18446744073709551615) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/grep/README.md b/exercises/grep/README.md index a0acd728df..9ca32cbe02 100644 --- a/exercises/grep/README.md +++ b/exercises/grep/README.md @@ -64,6 +64,21 @@ The `grep` command should support multiple flags at once. For example, running `grep -l -v "hello" file1.txt file2.txt` should print the names of files that do not contain the string "hello". +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/hamming/README.md b/exercises/hamming/README.md index b5c68ad05a..203d34308d 100644 --- a/exercises/hamming/README.md +++ b/exercises/hamming/README.md @@ -35,6 +35,21 @@ The Hamming distance is only defined for sequences of equal length. This means that based on the definition, each language could deal with getting sequences of equal length differently. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/hamming/hamming_test.py b/exercises/hamming/hamming_test.py index 77dc6823cf..757d9df261 100644 --- a/exercises/hamming/hamming_test.py +++ b/exercises/hamming/hamming_test.py @@ -47,13 +47,23 @@ def test_large_distance_in_off_by_one_strand(self): self.assertEqual(hamming.distance("GGACGGATTCTG", "AGGACGGATTCT"), 9) def test_disallow_first_strand_longer(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): hamming.distance("AATG", "AAA") def test_disallow_second_strand_longer(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): hamming.distance("ATA", "AGTG") + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/hello-world/README.md b/exercises/hello-world/README.md index d6e94bf7e0..0db0f60a5f 100644 --- a/exercises/hello-world/README.md +++ b/exercises/hello-world/README.md @@ -14,6 +14,21 @@ The objectives are simple: If everything goes well, you will be ready to fetch your first real exercise. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/hexadecimal/README.md b/exercises/hexadecimal/README.md index 0ac11a3861..b4bbcb8e22 100644 --- a/exercises/hexadecimal/README.md +++ b/exercises/hexadecimal/README.md @@ -7,6 +7,21 @@ teal: 008080, navy: 000080). The program should handle invalid hexadecimal strings. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/hexadecimal/hexadecimal_test.py b/exercises/hexadecimal/hexadecimal_test.py index c805b32a66..705fe98c0b 100644 --- a/exercises/hexadecimal/hexadecimal_test.py +++ b/exercises/hexadecimal/hexadecimal_test.py @@ -35,9 +35,19 @@ def test_valid_hexa9(self): self.assertEqual(hexa('00fff0'), 65520) def test_invalid_hexa(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): hexa('carrot') + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/house/README.md b/exercises/house/README.md index 3661dd19d9..7ed58e642e 100644 --- a/exercises/house/README.md +++ b/exercises/house/README.md @@ -1,6 +1,6 @@ # House -Output the nursery rhyme 'This is the House that Jack Built'. +Recite the nursery rhyme 'This is the House that Jack Built'. > [The] process of placing a phrase of clause within another phrase of > clause is called embedding. It is through the processes of recursion @@ -105,6 +105,21 @@ that ate the malt that lay in the house that Jack built. ``` +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/isogram/README.md b/exercises/isogram/README.md index 58c3482063..236c25be50 100644 --- a/exercises/isogram/README.md +++ b/exercises/isogram/README.md @@ -13,6 +13,21 @@ Examples of isograms: The word *isograms*, however, is not an isogram, because the s repeats. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/kindergarten-garden/README.md b/exercises/kindergarten-garden/README.md index c8ddd3713a..7326fbf1c1 100644 --- a/exercises/kindergarten-garden/README.md +++ b/exercises/kindergarten-garden/README.md @@ -9,8 +9,8 @@ actual dirt, and grow actual plants. They've chosen to grow grass, clover, radishes, and violets. -To this end, the children have put little cups along the window sills, and -planted one type of plant in each cup, choosing randomly from the available +To this end, the children have put little cups along the window sills, and +planted one type of plant in each cup, choosing randomly from the available types of seeds. ```text @@ -25,7 +25,7 @@ There are 12 children in the class: - Eve, Fred, Ginny, Harriet, - Ileana, Joseph, Kincaid, and Larry. -Each child gets 4 cups, two on each row. Their teacher assigns cups to +Each child gets 4 cups, two on each row. Their teacher assigns cups to the children alphabetically by their names. The following diagram represents Alice's plants: @@ -59,6 +59,21 @@ While asking for Bob's plants would yield: - Clover, grass, clover, clover +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/largest-series-product/README.md b/exercises/largest-series-product/README.md index 63032f7c2c..99019e4568 100644 --- a/exercises/largest-series-product/README.md +++ b/exercises/largest-series-product/README.md @@ -13,6 +13,21 @@ in the input; the digits need not be *numerically consecutive*. For the input `'73167176531330624919225119674426574742355349194934'`, the largest product for a series of 6 digits is 23520. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/largest-series-product/largest_series_product_test.py b/exercises/largest-series-product/largest_series_product_test.py index 62307ea532..3ad3676a44 100644 --- a/exercises/largest-series-product/largest_series_product_test.py +++ b/exercises/largest-series-product/largest_series_product_test.py @@ -45,7 +45,7 @@ def test_reports_zero_if_all_spans_include_zero(self): self.assertEqual(largest_product("99099", 3), 0) def test_rejects_span_longer_than_string_length(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): largest_product("123", 4) def test_reports_1_for_empty_string_and_empty_product_0_span(self): @@ -55,15 +55,15 @@ def test_reports_1_for_nonempty_string_and_empty_product_0_span(self): self.assertEqual(largest_product("123", 0), 1) def test_rejects_empty_string_and_nonzero_span(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): largest_product("", 1) def test_rejects_invalid_character_in_digits(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): largest_product("1234a5", 2) def test_rejects_negative_span(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): largest_product("12345", -1) @unittest.skip("extra-credit") @@ -87,6 +87,16 @@ def test_project_euler_big_number(self): "3600823257530420752963450") self.assertEqual(largest_product(series, 13), 23514624000) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/leap/README.md b/exercises/leap/README.md index 91c9a51cb2..c74c3d6c43 100644 --- a/exercises/leap/README.md +++ b/exercises/leap/README.md @@ -26,6 +26,21 @@ phenomenon, go watch [this youtube video][video]. [video]: http://www.youtube.com/watch?v=xX96xng7sAE +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/linked-list/README.md b/exercises/linked-list/README.md index 12c85fab2e..ba7f0117a7 100644 --- a/exercises/linked-list/README.md +++ b/exercises/linked-list/README.md @@ -27,6 +27,21 @@ empty list. If you want to know more about linked lists, check [Wikipedia](https://en.wikipedia.org/wiki/Linked_list). +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/list-ops/README.md b/exercises/list-ops/README.md index 8b85aef8bb..3cd7c5e340 100644 --- a/exercises/list-ops/README.md +++ b/exercises/list-ops/README.md @@ -6,6 +6,21 @@ In functional languages list operations like `length`, `map`, and `reduce` are very common. Implement a series of basic list operations, without using existing functions. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/luhn/README.md b/exercises/luhn/README.md index 4f60eee727..1b2c1544ef 100644 --- a/exercises/luhn/README.md +++ b/exercises/luhn/README.md @@ -64,6 +64,21 @@ Sum the digits 57 is not evenly divisible by 10, so this number is not valid. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/markdown/README.md b/exercises/markdown/README.md index e9db4ea49f..8364521eb7 100644 --- a/exercises/markdown/README.md +++ b/exercises/markdown/README.md @@ -1,5 +1,7 @@ # Markdown +Refactor a Markdown parser. + The markdown exercise is a refactoring exercise. There is code that parses a given string with [Markdown syntax](https://guides.github.com/features/mastering-markdown/) and returns the @@ -12,6 +14,21 @@ It would be helpful if you made notes of what you did in your refactoring in comments so reviewers can see that, but it isn't strictly necessary. The most important thing is to make the code better! +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. @@ -21,5 +38,6 @@ For example, if you're submitting `bob.py` for the Bob exercise, the submit comm For more detailed information about running tests, code style and linting, please see the [help page](http://exercism.io/languages/python). + ## Submitting Incomplete Solutions It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/exercises/matrix/README.md b/exercises/matrix/README.md index faf5cca5ed..8851661c53 100644 --- a/exercises/matrix/README.md +++ b/exercises/matrix/README.md @@ -40,6 +40,21 @@ And its columns: - 8, 3, 6 - 7, 2, 7 +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/meetup/README.md b/exercises/meetup/README.md index 98ba81bda7..a1b35b12e0 100644 --- a/exercises/meetup/README.md +++ b/exercises/meetup/README.md @@ -2,25 +2,44 @@ Calculate the date of meetups. -Typically meetups happen on the same day of the week. In this exercise, you will take -a description of a meetup date, and return the actual meetup date. +Typically meetups happen on the same day of the week. In this exercise, you +will take a description of a meetup date, and return the actual meetup date. Examples of general descriptions are: -- the first Monday of January 2017 -- the third Tuesday of January 2017 -- the Wednesteenth of January 2017 -- the last Thursday of January 2017 +- The first Monday of January 2017 +- The third Tuesday of January 2017 +- The wednesteenth of January 2017 +- The last Thursday of January 2017 -Note that "Monteenth", "Tuesteenth", etc are all made up words. There -was a meetup whose members realized that there are exactly 7 numbered days in a month that -end in '-teenth'. Therefore, one is guaranteed that each day of the week +The descriptors you are expected to parse are: +first, second, third, fourth, fifth, last, monteenth, tuesteenth, wednesteenth, +thursteenth, friteenth, saturteenth, sunteenth + +Note that "monteenth", "tuesteenth", etc are all made up words. There was a +meetup whose members realized that there are exactly 7 numbered days in a month +that end in '-teenth'. Therefore, one is guaranteed that each day of the week (Monday, Tuesday, ...) will have exactly one date that is named with '-teenth' in every month. -Given examples of a meetup dates, each containing a month, day, year, and descriptor -(first, second, teenth, etc), calculate the date of the actual meetup. -For example, if given "First Monday of January 2017", the correct meetup date is 2017/1/2 +Given examples of a meetup dates, each containing a month, day, year, and +descriptor calculate the date of the actual meetup. For example, if given +"The first Monday of January 2017", the correct meetup date is 2017/1/2. + +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + ## Submitting Exercises diff --git a/exercises/meetup/meetup_test.py b/exercises/meetup/meetup_test.py index 4455bb8fbd..06c9856cdf 100644 --- a/exercises/meetup/meetup_test.py +++ b/exercises/meetup/meetup_test.py @@ -399,9 +399,19 @@ def test_fifth_monday_of_march_2015(self): meetup_day(2015, 3, 'Monday', '5th'), date(2015, 3, 30)) def test_nonexistent_fifth_monday_of_february_2015(self): - with self.assertRaises(MeetupDayException): + with self.assertRaisesWithMessage(MeetupDayException): meetup_day(2015, 2, 'Monday', '5th') + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/minesweeper/README.md b/exercises/minesweeper/README.md index 7aeca7a9e4..414e02deab 100644 --- a/exercises/minesweeper/README.md +++ b/exercises/minesweeper/README.md @@ -26,6 +26,21 @@ into this: | 111 | +-----+ +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/minesweeper/minesweeper_test.py b/exercises/minesweeper/minesweeper_test.py index 839f67a47b..bd33781180 100644 --- a/exercises/minesweeper/minesweeper_test.py +++ b/exercises/minesweeper/minesweeper_test.py @@ -139,14 +139,24 @@ def test_different_len(self): inp = [" ", "* ", " "] - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): board(inp) def test_invalid_char(self): inp = ["X * "] - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): board(inp) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/nth-prime/README.md b/exercises/nth-prime/README.md index 77a968f044..08f10312cb 100644 --- a/exercises/nth-prime/README.md +++ b/exercises/nth-prime/README.md @@ -8,6 +8,21 @@ the 6th prime is 13. If your language provides methods in the standard library to deal with prime numbers, pretend they don't exist and implement them yourself. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/nth-prime/nth_prime_test.py b/exercises/nth-prime/nth_prime_test.py index bf1322d4ca..0dff8bc1c2 100644 --- a/exercises/nth-prime/nth_prime_test.py +++ b/exercises/nth-prime/nth_prime_test.py @@ -19,7 +19,7 @@ def test_big_prime(self): self.assertEqual(nth_prime(10001), 104743) def test_there_is_no_zeroth_prime(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): nth_prime(0) # additional track specific test @@ -28,6 +28,16 @@ def test_first_twenty_primes(self): 37, 41, 43, 47, 53, 59, 61, 67, 71], [nth_prime(n) for n in range(1, 21)]) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/nucleotide-count/README.md b/exercises/nucleotide-count/README.md index 9d9251a3aa..827eafaf98 100644 --- a/exercises/nucleotide-count/README.md +++ b/exercises/nucleotide-count/README.md @@ -2,8 +2,8 @@ Given a single stranded DNA string, compute how many times each nucleotide occurs in the string. -The genetic language of every living thing on the planet is DNA. -DNA is a large molecule that is built from an extremely long sequence of individual elements called nucleotides. +The genetic language of every living thing on the planet is DNA. +DNA is a large molecule that is built from an extremely long sequence of individual elements called nucleotides. 4 types exist in DNA and these differ only slightly and can be represented as the following symbols: 'A' for adenine, 'C' for cytosine, 'G' for guanine, and 'T' thymine. Here is an analogy: @@ -12,6 +12,21 @@ Here is an analogy: - legos are to lego houses as - words are to sentences as... +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/nucleotide-count/nucleotide_count_test.py b/exercises/nucleotide-count/nucleotide_count_test.py index dd59e2be3e..24bb7c6765 100644 --- a/exercises/nucleotide-count/nucleotide_count_test.py +++ b/exercises/nucleotide-count/nucleotide_count_test.py @@ -28,7 +28,7 @@ def test_counts_only_thymidine(self): self.assertEqual(count('GGGGGTAACCCGG', 'T'), 1) def test_validates_nucleotides(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): count("GACT", 'X') def test_counts_all_nucleotides(self): @@ -37,6 +37,16 @@ def test_counts_all_nucleotides(self): expected = {'A': 20, 'T': 21, 'G': 17, 'C': 12} self.assertEqual(nucleotide_counts(dna), expected) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/ocr-numbers/README.md b/exercises/ocr-numbers/README.md index 16d798ce3d..9444fc92da 100644 --- a/exercises/ocr-numbers/README.md +++ b/exercises/ocr-numbers/README.md @@ -1,4 +1,4 @@ -# Ocr Numbers +# OCR Numbers Given a 3 x 4 grid of pipes, underscores, and spaces, determine which number is represented, or whether it is garbled. @@ -78,6 +78,21 @@ Update your program to handle multiple numbers, one per line. When converting se Is converted to "123,456,789" +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/ocr-numbers/ocr_numbers_test.py b/exercises/ocr-numbers/ocr_numbers_test.py index dd4064aca4..16da8bb16c 100644 --- a/exercises/ocr-numbers/ocr_numbers_test.py +++ b/exercises/ocr-numbers/ocr_numbers_test.py @@ -33,13 +33,13 @@ def test_unreadable(self): " "]), '?') def test_line_number_not_multiple_of_four(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): convert([" _ ", "| |", " "]) def test_col_number_not_multiple_of_three(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): convert([" ", " |", " |", @@ -137,6 +137,16 @@ def test_recognizes_numbers_separated_by_empty_lines(self): ] self.assertEqual(convert(input_grid), "123,456,789") + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/octal/README.md b/exercises/octal/README.md index a9e096b47e..6b1130ebf4 100644 --- a/exercises/octal/README.md +++ b/exercises/octal/README.md @@ -46,6 +46,21 @@ So: = 155 ``` +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/octal/octal_test.py b/exercises/octal/octal_test.py index 43cdf1037e..4a83299194 100644 --- a/exercises/octal/octal_test.py +++ b/exercises/octal/octal_test.py @@ -29,17 +29,30 @@ def test_octal_1234567_is_decimal_342391(self): self.assertEqual(parse_octal("1234567"), 342391) def test_8_is_seen_as_invalid(self): - self.assertRaises(ValueError, parse_octal, "8") + with self.assertRaisesWithMessage(ValueError): + parse_octal("8") def test_invalid_octal_is_recognized(self): - self.assertRaises(ValueError, parse_octal, "carrot") + with self.assertRaisesWithMessage(ValueError): + parse_octal("carrot") def test_6789_is_seen_as_invalid(self): - self.assertRaises(ValueError, parse_octal, "6789") + with self.assertRaisesWithMessage(ValueError): + parse_octal("6789") def test_valid_octal_formatted_string_011_is_decimal_9(self): self.assertEqual(parse_octal("011"), 9) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/palindrome-products/README.md b/exercises/palindrome-products/README.md index 0c85e680e0..687bbaf342 100644 --- a/exercises/palindrome-products/README.md +++ b/exercises/palindrome-products/README.md @@ -9,7 +9,7 @@ Given a range of numbers, find the largest and smallest palindromes which are products of numbers within that range. Your solution should return the largest and smallest palindromes, along with the -factors of each within the range. If the largest or smallest palindrome has more +factors of each within the range. If the largest or smallest palindrome has more than one pair of factors within the range, then return all the pairs. ## Example 1 @@ -32,6 +32,21 @@ Given the range `[10, 99]` (both inclusive)... The smallest palindrome product is `121`. Its factors are `(11, 11)`. The largest palindrome product is `9009`. Its factors are `(91, 99)`. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/pangram/README.md b/exercises/pangram/README.md index 7a1541db79..477ca68db1 100644 --- a/exercises/pangram/README.md +++ b/exercises/pangram/README.md @@ -8,6 +8,21 @@ The best known English pangram is: The alphabet used consists of ASCII letters `a` to `z`, inclusive, and is case insensitive. Input will not contain non-ASCII symbols. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/pascals-triangle/README.md b/exercises/pascals-triangle/README.md index 8c445bf997..f9e52bcc21 100644 --- a/exercises/pascals-triangle/README.md +++ b/exercises/pascals-triangle/README.md @@ -1,4 +1,4 @@ -# Pascals Triangle +# Pascal's Triangle Compute Pascal's triangle up to a given number of rows. @@ -14,6 +14,21 @@ the right and left of the current position in the previous row. # ... etc ``` +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/perfect-numbers/README.md b/exercises/perfect-numbers/README.md index ad82791f47..fec5fbaaad 100644 --- a/exercises/perfect-numbers/README.md +++ b/exercises/perfect-numbers/README.md @@ -17,6 +17,21 @@ The Greek mathematician [Nicomachus](https://en.wikipedia.org/wiki/Nicomachus) d Implement a way to determine whether a given number is **perfect**. Depending on your language track, you may also need to implement a way to determine whether a given number is **abundant** or **deficient**. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/perfect-numbers/perfect_numbers_test.py b/exercises/perfect-numbers/perfect_numbers_test.py index dd350818a6..9f4259c533 100644 --- a/exercises/perfect-numbers/perfect_numbers_test.py +++ b/exercises/perfect-numbers/perfect_numbers_test.py @@ -2,8 +2,8 @@ from perfect_numbers import classify -# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.1 +# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.1 class PerfectNumbersTest(unittest.TestCase): def test_smallest_perfect_number(self): @@ -69,13 +69,23 @@ def test_edge_case(self): class InvalidInputsTest(unittest.TestCase): def test_zero(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): classify(0) def test_negative(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): classify(-1) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/phone-number/README.md b/exercises/phone-number/README.md index 9f93f439d2..e22b8f23c3 100644 --- a/exercises/phone-number/README.md +++ b/exercises/phone-number/README.md @@ -14,8 +14,7 @@ The format is usually represented as where `N` is any digit from 2 through 9 and `X` is any digit from 0 through 9. -Your task is to clean up differently formatted telephone numbers by removing punctuation and the -country code (1) if present. +Your task is to clean up differently formatted telephone numbers by removing punctuation and the country code (1) if present. For example, the inputs - `+1 (613)-995-0253` @@ -29,6 +28,21 @@ should all produce the output **Note:** As this exercise only deals with telephone numbers used in NANP-countries, only 1 is considered a valid country code. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/phone-number/example.py b/exercises/phone-number/example.py index 239760a6d7..c9a53f64fe 100644 --- a/exercises/phone-number/example.py +++ b/exercises/phone-number/example.py @@ -29,4 +29,4 @@ def _normalize(self, number): if valid: return number[-10:] else: - raise ValueError() + raise ValueError("{} is not a valid phone number".format(number)) diff --git a/exercises/phone-number/phone_number_test.py b/exercises/phone-number/phone_number_test.py index 758e2e2590..638cf1de1f 100644 --- a/exercises/phone-number/phone_number_test.py +++ b/exercises/phone-number/phone_number_test.py @@ -19,11 +19,11 @@ def test_cleans_number_with_multiple_spaces(self): self.assertEqual(number, "2234567890") def test_invalid_when_9_digits(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): Phone("123456789") def test_invalid_when_11_digits_and_first_not_1(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): Phone("22234567890") def test_valid_when_11_digits_and_first_is_1(self): @@ -35,23 +35,23 @@ def test_valid_when_11_digits_and_first_is_1_with_punctuation(self): self.assertEqual(number, "2234567890") def test_invalid_when_more_than_11_digits(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): Phone("321234567890") def test_invalid_with_letters(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): Phone("123-abc-7890") def test_invalid_with_punctuation(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): Phone("123-@:!-7890") def test_invalid_area_code(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): Phone("(123) 456-7890") def test_invalid_exchange_code(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): Phone("(223) 056-7890") # Track specific tests @@ -67,6 +67,16 @@ def test_pretty_print_with_full_us_phone_number(self): number = Phone("12234567890") self.assertEqual(number.pretty(), "(223) 456-7890") + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/pig-latin/README.md b/exercises/pig-latin/README.md index d8d7d85cd3..fdafd0e5e7 100644 --- a/exercises/pig-latin/README.md +++ b/exercises/pig-latin/README.md @@ -17,6 +17,21 @@ variants too. See for more details. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/point-mutations/README.md b/exercises/point-mutations/README.md index e94a9f4af4..e5f1f91949 100644 --- a/exercises/point-mutations/README.md +++ b/exercises/point-mutations/README.md @@ -34,6 +34,21 @@ distance function. **Note: This problem is deprecated, replaced by the one called `hamming`.** +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/poker/README.md b/exercises/poker/README.md index d22f86e28b..ad1e356f59 100644 --- a/exercises/poker/README.md +++ b/exercises/poker/README.md @@ -5,6 +5,21 @@ Pick the best hand(s) from a list of poker hands. See [wikipedia](https://en.wikipedia.org/wiki/List_of_poker_hands) for an overview of poker hands. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/pov/README.md b/exercises/pov/README.md index d4cbfdd5a2..10c4386b93 100644 --- a/exercises/pov/README.md +++ b/exercises/pov/README.md @@ -2,7 +2,7 @@ Reparent a graph on a selected node. -### Tree Reparenting +# Tree Reparenting This exercise is all about re-orientating a graph to see things from a different point of view. For example family trees are usually presented from the @@ -39,15 +39,33 @@ a different leaf node) can be seen to follow the path 6-2-0-3-9 This exercise involves taking an input graph and re-orientating it from the point of view of one of the nodes. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. For example, if you're submitting `bob.py` for the Bob exercise, the submit command would be something like `exercism submit /python/bob/bob.py`. - For more detailed information about running tests, code style and linting, please see the [help page](http://exercism.io/languages/python). +## Source + +Adaptation of exercise from 4clojure [https://www.4clojure.com/](https://www.4clojure.com/) + ## Submitting Incomplete Solutions It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/exercises/pov/example.py b/exercises/pov/example.py index 462a18aaaa..8d80b43804 100644 --- a/exercises/pov/example.py +++ b/exercises/pov/example.py @@ -53,7 +53,7 @@ def fromPov(self, from_node): return tree for child in tree.children: stack.append(child.add(tree.remove(child.label))) - raise ValueError() + raise ValueError("Tree could not be reoriented") def pathTo(self, from_node, to_node): reordered = self.fromPov(from_node) @@ -63,7 +63,7 @@ def pathTo(self, from_node, to_node): try: tree = stack.pop() except IndexError: - raise ValueError() + raise ValueError("No path found") if to_node in tree: path.append(tree.label) stack = tree.children diff --git a/exercises/pov/pov_test.py b/exercises/pov/pov_test.py index dc9dbb5771..88fcd31f23 100644 --- a/exercises/pov/pov_test.py +++ b/exercises/pov/pov_test.py @@ -2,8 +2,8 @@ from pov import Tree -# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.1 +# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.1 class PovTest(unittest.TestCase): def assertTreeEquals(self, result, expected): @@ -110,7 +110,7 @@ def test_can_reroot_complex_tree_with_cousins(self): def test_errors_if_target_does_not_exist_in_singleton_tree(self): tree = Tree('x') - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): tree.fromPov('nonexistent') def test_errors_if_target_does_not_exist_in_large_tree(self): @@ -122,7 +122,7 @@ def test_errors_if_target_does_not_exist_in_large_tree(self): Tree('sibling-0'), Tree('sibling-1') ]) - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): tree.fromPov('nonexistent') def test_find_path_between_two_nodes(self): @@ -180,7 +180,7 @@ def test_errors_if_destination_does_not_exist(self): Tree('sibling-0'), Tree('sibling-1') ]) - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): tree.pathTo('x', 'nonexistent') def test_errors_if_source_does_not_exist(self): @@ -192,9 +192,19 @@ def test_errors_if_source_does_not_exist(self): Tree('sibling-0'), Tree('sibling-1') ]) - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): tree.pathTo('nonexistent', 'x') + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/prime-factors/README.md b/exercises/prime-factors/README.md index d625d41833..5ccb85d501 100644 --- a/exercises/prime-factors/README.md +++ b/exercises/prime-factors/README.md @@ -29,6 +29,21 @@ You can check this yourself: - = 60 - Success! +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/protein-translation/README.md b/exercises/protein-translation/README.md index 607ef842c8..c0ab273470 100644 --- a/exercises/protein-translation/README.md +++ b/exercises/protein-translation/README.md @@ -41,6 +41,21 @@ UAA, UAG, UGA | STOP Learn more about [protein translation on Wikipedia](http://en.wikipedia.org/wiki/Translation_(biology)) +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/protein-translation/protein_translation_test.py b/exercises/protein-translation/protein_translation_test.py index df3e78f72c..7429ad2c81 100644 --- a/exercises/protein-translation/protein_translation_test.py +++ b/exercises/protein-translation/protein_translation_test.py @@ -65,6 +65,16 @@ def test_stops_translation_if_stop_codon_in_middle_of_six_codon_sequence( expected = ['Tryptophan', 'Cysteine', 'Tyrosine'] self.assertEqual(expected, proteins(strand)) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/proverb/README.md b/exercises/proverb/README.md index e19a1f398e..1cf02312c6 100644 --- a/exercises/proverb/README.md +++ b/exercises/proverb/README.md @@ -16,6 +16,20 @@ And all for the want of a nail. Note that the list of inputs may vary; your solution should be able to handle lists of arbitrary length and content. No line of the output text should be a static, unchanging string; all should vary according to the input given. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/pythagorean-triplet/README.md b/exercises/pythagorean-triplet/README.md index c48b0fbad7..abb063b02f 100644 --- a/exercises/pythagorean-triplet/README.md +++ b/exercises/pythagorean-triplet/README.md @@ -17,6 +17,21 @@ There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product a * b * c. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/pythagorean-triplet/pythagorean_triplet_test.py b/exercises/pythagorean-triplet/pythagorean_triplet_test.py index 3c46fa5be3..9288e73305 100644 --- a/exercises/pythagorean-triplet/pythagorean_triplet_test.py +++ b/exercises/pythagorean-triplet/pythagorean_triplet_test.py @@ -80,9 +80,19 @@ def test_is_triplet3(self): self.assertIs(is_triplet((924, 43, 925)), True) def test_odd_number(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): primitive_triplets(5) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/queen-attack/README.md b/exercises/queen-attack/README.md index 1a6e94e975..4c99f5908d 100644 --- a/exercises/queen-attack/README.md +++ b/exercises/queen-attack/README.md @@ -26,6 +26,21 @@ You'd also be able to answer whether the queens can attack each other. In this case, that answer would be yes, they can, because both pieces share a diagonal. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/queen-attack/queen_attack_test.py b/exercises/queen-attack/queen_attack_test.py index 6d0d032b1a..35ad64a953 100644 --- a/exercises/queen-attack/queen_attack_test.py +++ b/exercises/queen-attack/queen_attack_test.py @@ -14,19 +14,19 @@ def test_queen_valid_position(self): self.fail("Unexpected Exception") def test_queen_negative_row(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): board((1, 1), (-2, 2)) def test_queen_invalid_row(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): board((1, 1), (8, 4)) def test_queen_negative_column(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): board((1, 1), (2, -2)) def test_queen_invalid_column(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): board((1, 1), (4, 8)) def test_attack_false(self): @@ -55,15 +55,15 @@ def test_attack_diagonal4(self): # If either board or can_attack are called with an invalid board position # they should raise a ValueError with a meaningful error message. def test_invalid_position_can_attack(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): can_attack((0, 0), (7, 8)) def test_queens_same_position_board(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): board((2, 2), (2, 2)) def test_queens_same_position_can_attack(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): can_attack((2, 2), (2, 2)) def test_board1(self): @@ -88,6 +88,16 @@ def test_board2(self): '________'] self.assertEqual(board((0, 6), (1, 7)), ans) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/rail-fence-cipher/README.md b/exercises/rail-fence-cipher/README.md index 7bdccff2d4..893857929b 100644 --- a/exercises/rail-fence-cipher/README.md +++ b/exercises/rail-fence-cipher/README.md @@ -58,6 +58,21 @@ W . . . E . . . C . . . R . . . L . . . T . . . E If you now read along the zig-zag shape you can read the original message. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/raindrops/README.md b/exercises/raindrops/README.md index 1fa327b67b..eb3e69fcc0 100644 --- a/exercises/raindrops/README.md +++ b/exercises/raindrops/README.md @@ -17,6 +17,21 @@ Convert a number to a string, the contents of which depend on the number's facto - 34 has four factors: 1, 2, 17, and 34. - In raindrop-speak, this would be "34". +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/react/README.md b/exercises/react/README.md index b67d7d076b..160966fec7 100644 --- a/exercises/react/README.md +++ b/exercises/react/README.md @@ -15,13 +15,27 @@ In addition, compute cells should allow for registering change notification callbacks. Call a cell’s callbacks when the cell’s value in a new stable state has changed from the previous stable state. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. For example, if you're submitting `bob.py` for the Bob exercise, the submit command would be something like `exercism submit /python/bob/bob.py`. - For more detailed information about running tests, code style and linting, please see the [help page](http://exercism.io/languages/python). diff --git a/exercises/rectangles/README.md b/exercises/rectangles/README.md index 75c549bbdb..2a7ecc108c 100644 --- a/exercises/rectangles/README.md +++ b/exercises/rectangles/README.md @@ -37,32 +37,47 @@ The above diagram contains 6 rectangles: ``` ```text - - + + +--+ | | +--+ ``` ```text - - + + +--+ | | +--+ ``` ```text - - ++ - ++ - - + + ++ + ++ + + ``` You may assume that the input is always a proper rectangle (i.e. the length of every line equals the length of the first line). +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/rna-transcription/.meta/hints.md b/exercises/rna-transcription/.meta/hints.md new file mode 100644 index 0000000000..29b397ab7b --- /dev/null +++ b/exercises/rna-transcription/.meta/hints.md @@ -0,0 +1 @@ +Your function will need to be able to handle invalid inputs by raising a `ValueError` with a meaningful message. diff --git a/exercises/rna-transcription/README.md b/exercises/rna-transcription/README.md index 641b0568cf..a9340e6240 100644 --- a/exercises/rna-transcription/README.md +++ b/exercises/rna-transcription/README.md @@ -1,4 +1,4 @@ -# Rna Transcription +# RNA Transcription Given a DNA strand, return its RNA complement (per RNA transcription). @@ -18,8 +18,23 @@ each nucleotide with its complement: * `T` -> `A` * `A` -> `U` -Your function will need to be able to handle invalid inputs by raising a -`ValueError` with a meaningful message. +Your function will need to be able to handle invalid inputs by raising a `ValueError` with a meaningful message. + + +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + ## Submitting Exercises @@ -34,7 +49,5 @@ please see the [help page](http://exercism.io/languages/python). Rosalind [http://rosalind.info/problems/rna](http://rosalind.info/problems/rna) -Note that in this problem we are finding the complement, so we also swap for the complementary bases - unlike the Rosalind problem which is just swapping T for U. - ## Submitting Incomplete Solutions It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/exercises/rna-transcription/rna_transcription_test.py b/exercises/rna-transcription/rna_transcription_test.py index ca0771005f..b2feb5186b 100644 --- a/exercises/rna-transcription/rna_transcription_test.py +++ b/exercises/rna-transcription/rna_transcription_test.py @@ -23,17 +23,27 @@ def test_transcribes_all_occurences(self): self.assertEqual(to_rna('ACGTGGTCTTAA'), 'UGCACCAGAAUU') def test_correctly_handles_single_invalid_input(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): to_rna('U') def test_correctly_handles_completely_invalid_input(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): to_rna('XXX') def test_correctly_handles_partially_invalid_input(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): to_rna('ACGTXXXCTTAA') + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/robot-name/README.md b/exercises/robot-name/README.md index a28ed95e20..ebd962dbaf 100644 --- a/exercises/robot-name/README.md +++ b/exercises/robot-name/README.md @@ -15,6 +15,21 @@ The names must be random: they should not follow a predictable sequence. Random names means a risk of collisions. Your solution must ensure that every existing robot has a unique name. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/robot-simulator/README.md b/exercises/robot-simulator/README.md index cc696556ac..cd49116ea8 100644 --- a/exercises/robot-simulator/README.md +++ b/exercises/robot-simulator/README.md @@ -27,6 +27,21 @@ direction it is pointing. - Say a robot starts at {7, 3} facing north. Then running this stream of instructions should leave it at {9, 4} facing west. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/roman-numerals/README.md b/exercises/roman-numerals/README.md index 3ce7b49432..0d960a8c21 100644 --- a/exercises/roman-numerals/README.md +++ b/exercises/roman-numerals/README.md @@ -42,6 +42,21 @@ In Roman numerals 1990 is MCMXC: See also: http://www.novaroma.org/via_romana/numbers.html +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/rotational-cipher/README.md b/exercises/rotational-cipher/README.md index 0dc9438251..7fb876d5b6 100644 --- a/exercises/rotational-cipher/README.md +++ b/exercises/rotational-cipher/README.md @@ -30,6 +30,21 @@ Ciphertext is written out in the same formatting as the input including spaces a - ROT13 `The quick brown fox jumps over the lazy dog.` gives `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.` - ROT13 `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.` gives `The quick brown fox jumps over the lazy dog.` +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/run-length-encoding/README.md b/exercises/run-length-encoding/README.md index da8623afe9..89fbfb7dd6 100644 --- a/exercises/run-length-encoding/README.md +++ b/exercises/run-length-encoding/README.md @@ -23,6 +23,21 @@ the letters A through Z (either lower or upper case) and whitespace. This way data to be encoded will never contain any numbers and numbers inside data to be decoded always represent the count for the following character. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/saddle-points/README.md b/exercises/saddle-points/README.md index b8602a77c2..fb7be3a2a6 100644 --- a/exercises/saddle-points/README.md +++ b/exercises/saddle-points/README.md @@ -26,6 +26,21 @@ saddle points for any given matrix. Note that you may find other definitions of matrix saddle points online, but the tests for this exercise follow the above unambiguous definition. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/saddle-points/saddle_points_test.py b/exercises/saddle-points/saddle_points_test.py index c893ccc0db..3a765a1fbc 100644 --- a/exercises/saddle-points/saddle_points_test.py +++ b/exercises/saddle-points/saddle_points_test.py @@ -38,9 +38,19 @@ def test_indentify_saddle_bottom_right_corner(self): def test_irregular_matrix(self): inp = [[3, 2, 1], [0, 1], [2, 1, 0]] - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): saddle_points(inp) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/say/README.md b/exercises/say/README.md index 8e975b8c15..aee6cf0341 100644 --- a/exercises/say/README.md +++ b/exercises/say/README.md @@ -62,6 +62,21 @@ Use _and_ (correctly) when spelling out the number in English: - 1002 becomes "one thousand and two". - 1323 becomes "one thousand three hundred and twenty-three". +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/say/say_test.py b/exercises/say/say_test.py index 89a055577f..56b0e586d6 100644 --- a/exercises/say/say_test.py +++ b/exercises/say/say_test.py @@ -64,13 +64,23 @@ def test_987654321123(self): "one hundred and twenty-three")) def test_number_to_large(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): say(1e12) def test_number_negative(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): say(-1) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/scale-generator/README.md b/exercises/scale-generator/README.md index 714e9ba7b9..b9e1065478 100644 --- a/exercises/scale-generator/README.md +++ b/exercises/scale-generator/README.md @@ -55,6 +55,21 @@ Here is a table of pitches with the names of their interval distance from the to | | | Dim 3rd | Aug 2nd | Dim 4th | | Aug 4th | Dim 5th | Aug 5th | Dim 7th | Aug 6th | Dim 8ve | | | | | | | | | Dim 5th | | | | | | | +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/scale-generator/example.py b/exercises/scale-generator/example.py index 5e39a85e21..c44e1fd2bf 100644 --- a/exercises/scale-generator/example.py +++ b/exercises/scale-generator/example.py @@ -13,8 +13,8 @@ def __init__(self, tonic, scale_name, pattern=None): self.name = self.tonic + ' ' + scale_name self.pattern = pattern self.chromatic_scale = (self.FLAT_CHROMATIC_SCALE - if tonic in self.FLAT_KEYS else - self.CHROMATIC_SCALE) + if tonic in self.FLAT_KEYS + else self.CHROMATIC_SCALE) self.pitches = self._assign_pitches() def _assign_pitches(self): @@ -27,7 +27,7 @@ def _assign_pitches(self): pitches.append(scale[last_index]) last_index += self.ASCENDING_INTERVALS.index(interval) + 1 if pitches[0] != scale[last_index % len(scale)]: - raise ValueError() + raise ValueError("Interval is broken") return pitches def _reorder_chromatic_scale(self): diff --git a/exercises/scale-generator/scale_generator_test.py b/exercises/scale-generator/scale_generator_test.py index a1825faa25..2a18918b0a 100644 --- a/exercises/scale-generator/scale_generator_test.py +++ b/exercises/scale-generator/scale_generator_test.py @@ -116,9 +116,19 @@ def test_enigmatic(self): self.assertEqual(expected, actual) def test_brokeninterval(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): Scale('G', 'enigmatic', 'mAMMMmM') + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/scrabble-score/README.md b/exercises/scrabble-score/README.md index 6886008da7..3bacb111e8 100644 --- a/exercises/scrabble-score/README.md +++ b/exercises/scrabble-score/README.md @@ -39,6 +39,21 @@ And to total: - You can play a double or a triple letter. - You can play a double or a triple word. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/secret-handshake/README.md b/exercises/secret-handshake/README.md index f596df0320..907b9e4e22 100644 --- a/exercises/secret-handshake/README.md +++ b/exercises/secret-handshake/README.md @@ -28,6 +28,21 @@ Given the input 19, the function would return the array Notice that the addition of 16 (10000 in binary) has caused the array to be reversed. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/series/README.md b/exercises/series/README.md index be07e13e4f..ebf8761b38 100644 --- a/exercises/series/README.md +++ b/exercises/series/README.md @@ -20,6 +20,21 @@ whatever you get. Note that these series are only required to occupy *adjacent positions* in the input; the digits need not be *numerically consecutive*. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/series/series_test.py b/exercises/series/series_test.py index 6bfd680e24..8e21039e39 100644 --- a/exercises/series/series_test.py +++ b/exercises/series/series_test.py @@ -37,13 +37,23 @@ def test_slices_of_five(self): [[0, 1, 2, 3, 4]], ) def test_overly_long_slice(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): slices("012", 4) def test_overly_short_slice(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): slices("01234", 0) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/sieve/README.md b/exercises/sieve/README.md index 4bcec38a12..39050a0f4f 100644 --- a/exercises/sieve/README.md +++ b/exercises/sieve/README.md @@ -27,6 +27,21 @@ Notice that this is a very specific algorithm, and the tests don't check that you've implemented the algorithm, only that you've come up with the correct list of primes. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/simple-cipher/.meta/hints.md b/exercises/simple-cipher/.meta/hints.md index b2358ef5d3..c3f8575374 100644 --- a/exercises/simple-cipher/.meta/hints.md +++ b/exercises/simple-cipher/.meta/hints.md @@ -3,14 +3,14 @@ Python, as of version 3.6, includes two different random modules. The module called `random` is pseudo-random, meaning it does not generate -true randomness, but follows an algorithm that simulates randomness. -Since random numbers are generated through a known algorithm, they are not truly random. +true randomness, but follows an algorithm that simulates randomness. +Since random numbers are generated through a known algorithm, they are not truly random. -The `random` module is not correctly suited for cryptography and should not be used, -precisely because it is pseudo-random. +The `random` module is not correctly suited for cryptography and should not be used, +precisely because it is pseudo-random. For this reason, in version 3.6, Python introduced the `secrets` module, which generates -cryptographically strong random numbers that provide the greater security required for cryptography. +cryptographically strong random numbers that provide the greater security required for cryptography. -Since this is only an exercise, `random` is fine to use, but note that **it would be +Since this is only an exercise, `random` is fine to use, but note that **it would be very insecure if actually used for cryptography.** diff --git a/exercises/simple-cipher/README.md b/exercises/simple-cipher/README.md index 594be46558..4a7ca50877 100644 --- a/exercises/simple-cipher/README.md +++ b/exercises/simple-cipher/README.md @@ -65,7 +65,7 @@ If someone doesn't submit a key at all, generate a truly random key of at least 100 characters in length, accessible via Cipher#key (the # syntax means instance variable) -If the key submitted has capital letters or numbers, throw a +If the key submitted has capital letters or numbers, raise a ValueError with a message to that effect. ## Extensions @@ -100,6 +100,22 @@ cryptographically strong random numbers that provide the greater security requir Since this is only an exercise, `random` is fine to use, but note that **it would be very insecure if actually used for cryptography.** + +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/simple-cipher/simple_cipher_test.py b/exercises/simple-cipher/simple_cipher_test.py index d61cbc717d..c08aa5e0c1 100644 --- a/exercises/simple-cipher/simple_cipher_test.py +++ b/exercises/simple-cipher/simple_cipher_test.py @@ -68,11 +68,21 @@ def test_cipher_random_key(self): 'All items in the key must be chars and lowercase!') def test_cipher_wrong_key(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): Cipher('a1cde') - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): Cipher('aBcde') + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/simple-linked-list/hints.md b/exercises/simple-linked-list/.meta/hints.md similarity index 97% rename from exercises/simple-linked-list/hints.md rename to exercises/simple-linked-list/.meta/hints.md index 87265f87e1..b582905dd1 100644 --- a/exercises/simple-linked-list/hints.md +++ b/exercises/simple-linked-list/.meta/hints.md @@ -1,10 +1,10 @@ -## Hints - -To support `list()`, see [implementing an iterator for a class.](https://docs.python.org/3/tutorial/classes.html#iterators) - -Additionally, note that Python2's `next()` has been replaced by `__next__()` in Python3. For dual compatibility, `next()` can be implemented as: - -```Python -def next(self): - return self.__next__() -``` +## Hints + +To support `list()`, see [implementing an iterator for a class.](https://docs.python.org/3/tutorial/classes.html#iterators) + +Additionally, note that Python2's `next()` has been replaced by `__next__()` in Python3. For dual compatibility, `next()` can be implemented as: + +```Python +def next(self): + return self.__next__() +``` diff --git a/exercises/simple-linked-list/README.md b/exercises/simple-linked-list/README.md index 7a0663358b..9c03551d03 100644 --- a/exercises/simple-linked-list/README.md +++ b/exercises/simple-linked-list/README.md @@ -32,6 +32,22 @@ def next(self): return self.__next__() ``` + +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/simple-linked-list/example.py b/exercises/simple-linked-list/example.py index 34374217e5..71c607639f 100644 --- a/exercises/simple-linked-list/example.py +++ b/exercises/simple-linked-list/example.py @@ -42,7 +42,7 @@ def __len__(self): def head(self): if self._head is None: - raise EmptyListException() + raise EmptyListException("The list is empty") return self._head def push(self, value): @@ -53,7 +53,7 @@ def push(self, value): def pop(self): if self._head is None: - raise EmptyListException() + raise EmptyListException("The list is empty") self._len -= 1 ret = self._head.value() self._head = self._head.next() diff --git a/exercises/simple-linked-list/simple_linked_list_test.py b/exercises/simple-linked-list/simple_linked_list_test.py index 6d79749dcc..73e574b301 100644 --- a/exercises/simple-linked-list/simple_linked_list_test.py +++ b/exercises/simple-linked-list/simple_linked_list_test.py @@ -20,7 +20,7 @@ def test_non_empty_list_has_correct_len(self): def test_error_on_empty_list_head(self): sut = LinkedList() - with self.assertRaises(EmptyListException): + with self.assertRaisesWithMessage(EmptyListException): sut.head() def test_singleton_list_has_head(self): @@ -51,12 +51,12 @@ def test_can_from_non_empty_list(self): def test_pop_from_singleton_list_removes_head(self): sut = LinkedList([1]) self.assertEqual(sut.pop(), 1) - with self.assertRaises(EmptyListException): + with self.assertRaisesWithMessage(EmptyListException): sut.head() def test_error_on_empty_list_pop(self): sut = LinkedList() - with self.assertRaises(EmptyListException): + with self.assertRaisesWithMessage(EmptyListException): sut.pop() def test_push_and_pop(self): @@ -107,6 +107,16 @@ def test_reverse_non_empty_list(self): sut = LinkedList([1, 2, 3]) self.assertEqual(list(sut.reversed()), [1, 2, 3]) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/space-age/README.md b/exercises/space-age/README.md index 2cde9fa289..4c0fb72a15 100644 --- a/exercises/space-age/README.md +++ b/exercises/space-age/README.md @@ -17,6 +17,21 @@ be able to say that they're 31.69 Earth-years old. If you're wondering why Pluto didn't make the cut, go watch [this youtube video](http://www.youtube.com/watch?v=Z_2gbGXzFbs). +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/strain/README.md b/exercises/strain/README.md index 309ad29007..ea4dd93561 100644 --- a/exercises/strain/README.md +++ b/exercises/strain/README.md @@ -9,9 +9,7 @@ For example, given the collection of numbers: - 1, 2, 3, 4, 5 -And the predicate: - -Note: _a predicate P(x) will be true or false, depending on whether x belongs to a set._ [Wikipedia - Predicate](https://en.wikipedia.org/wiki/Predicate_(mathematical_logic))) +And the predicate: - is the number even? @@ -35,6 +33,21 @@ Keep your hands off that filter/reject/whatchamacallit functionality provided by your standard library! Solve this one yourself using other basic tools instead. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/sublist/README.md b/exercises/sublist/README.md index 5686e5c622..4bb71606bd 100644 --- a/exercises/sublist/README.md +++ b/exercises/sublist/README.md @@ -17,6 +17,21 @@ Examples: * A = [1, 2, 3, 4, 5], B = [2, 3, 4], A is a superlist of B * A = [1, 2, 4], B = [1, 2, 3, 4, 5], A is not a superlist of, sublist of or equal to B +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/sum-of-multiples/README.md b/exercises/sum-of-multiples/README.md index 6434de1705..174c6c7932 100644 --- a/exercises/sum-of-multiples/README.md +++ b/exercises/sum-of-multiples/README.md @@ -1,13 +1,28 @@ # Sum Of Multiples -Given a number, find the sum of all the multiples of particular numbers up to +Given a number, find the sum of all the unique multiples of particular numbers up to but not including that number. -If we list all the natural numbers up to but not including 20 that are -multiples of either 3 or 5, we get 3, 5, 6 and 9, 10, 12, 15, and 18. +If we list all the natural numbers below 20 that are multiples of 3 or 5, +we get 3, 5, 6, 9, 10, 12, 15, and 18. The sum of these multiples is 78. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/tournament/README.md b/exercises/tournament/README.md index 2a742e8bb7..2c9b8a81b6 100644 --- a/exercises/tournament/README.md +++ b/exercises/tournament/README.md @@ -64,6 +64,21 @@ Devastating Donkeys;Courageous Californians;draw Means that the Devastating Donkeys and Courageous Californians tied. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/transpose/README.md b/exercises/transpose/README.md index b3e90971af..249607b5db 100644 --- a/exercises/transpose/README.md +++ b/exercises/transpose/README.md @@ -58,6 +58,21 @@ In general, all characters from the input should also be present in the transpos That means that if a column in the input text contains only spaces on its bottom-most row(s), the corresponding output row should contain the spaces in its right-most column(s). +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/tree-building/README.md b/exercises/tree-building/README.md index 02fc81706b..8b9ff945d8 100644 --- a/exercises/tree-building/README.md +++ b/exercises/tree-building/README.md @@ -1,3 +1,5 @@ +# Tree Building + Refactor a tree building algorithm. Some web-forums have a tree layout, so posts are presented as a tree. However @@ -23,3 +25,31 @@ root (ID: 0, parent ID: 0) | +-- grandchild3 (ID: 6, parent ID: 3) +-- child3 (ID: 5, parent ID: 0) ``` + +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + +## Submitting Exercises + +Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. + +For example, if you're submitting `bob.py` for the Bob exercise, the submit command would be something like `exercism submit /python/bob/bob.py`. + +For more detailed information about running tests, code style and linting, +please see the [help page](http://exercism.io/languages/python). + + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/exercises/tree-building/example.py b/exercises/tree-building/example.py index ff25bc84f5..71c2abe9fa 100644 --- a/exercises/tree-building/example.py +++ b/exercises/tree-building/example.py @@ -15,11 +15,9 @@ def __init__(self, node_id): def validateRecord(record): if record.equal_id() and record.record_id != 0: - # only root(id=0) should have equal id - raise ValueError + raise ValueError("Only root should have equal record and parent id") elif not record.equal_id() and record.parent_id >= record.record_id: - # non-root node id should be smaller than its parent_id - raise ValueError + raise ValueError("Node record_id should be smaller than its parent_id") def BuildTree(records): @@ -36,7 +34,7 @@ def BuildTree(records): for index, record_id in enumerate(ordered_id): if index != record_id: - raise ValueError + raise ValueError("Record id is invalid or out of order") if record_id == root_id: root = node_dict[record_id] else: diff --git a/exercises/tree-building/tree_building_test.py b/exercises/tree-building/tree_building_test.py index 47b9e2a244..af7debd01b 100644 --- a/exercises/tree-building/tree_building_test.py +++ b/exercises/tree-building/tree_building_test.py @@ -108,7 +108,7 @@ def test_root_node_has_parent(self): Record(1, 0) ] # Root parent_id should be equal to record_id(0) - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): BuildTree(records) def test_no_root_node(self): @@ -117,7 +117,7 @@ def test_no_root_node(self): Record(2, 0) ] # Record with record_id 0 (root) is missing - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): BuildTree(records) def test_non_continuous(self): @@ -128,7 +128,7 @@ def test_non_continuous(self): Record(0, 0) ] # Record with record_id 3 is missing - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): BuildTree(records) def test_cycle_directly(self): @@ -142,7 +142,7 @@ def test_cycle_directly(self): Record(6, 3) ] # Cycle caused by Record 2 with parent_id pointing to itself - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): BuildTree(records) def test_cycle_indirectly(self): @@ -156,7 +156,7 @@ def test_cycle_indirectly(self): Record(6, 3) ] # Cycle caused by Record 2 with parent_id(6) greater than record_id(2) - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): BuildTree(records) def test_higher_id_parent_of_lower_id(self): @@ -166,7 +166,7 @@ def test_higher_id_parent_of_lower_id(self): Record(1, 2) ] # Record 1 have parent_id(2) greater than record_id(1) - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): BuildTree(records) def assert_node_is_branch(self, node, node_id, children_count): @@ -178,6 +178,16 @@ def assert_node_is_leaf(self, node, node_id): self.assertEqual(node.node_id, node_id) self.assertEqual(len(node.children), 0) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/triangle/README.md b/exercises/triangle/README.md index 7b62964890..7a94daaa7a 100644 --- a/exercises/triangle/README.md +++ b/exercises/triangle/README.md @@ -22,6 +22,21 @@ The case where the sum of the lengths of two sides _equals_ that of the third is known as a _degenerate_ triangle - it has zero area and looks like a single line. Feel free to add your own code/tests to check for degenerate triangles. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/triangle/example.py b/exercises/triangle/example.py index 33e6010b84..93ef6e871a 100644 --- a/exercises/triangle/example.py +++ b/exercises/triangle/example.py @@ -7,7 +7,7 @@ def __init__(self, x, y, z): self.sides = (x, y, z) if self._invalid_lengths() or self._violates_inequality(): - raise TriangleError + raise TriangleError("Side lengths are invalid for a triangle") def _invalid_lengths(self): return any([side <= 0 for side in self.sides]) diff --git a/exercises/triangle/triangle_test.py b/exercises/triangle/triangle_test.py index 38d0ba422f..c4b7f7b0d0 100644 --- a/exercises/triangle/triangle_test.py +++ b/exercises/triangle/triangle_test.py @@ -34,25 +34,35 @@ def test_very_small_triangles_are_legal(self): self.assertEqual(Triangle(0.4, 0.6, 0.3).kind(), "scalene") def test_triangles_with_no_size_are_illegal(self): - with self.assertRaises(TriangleError): + with self.assertRaisesWithMessage(TriangleError): Triangle(0, 0, 0) def test_triangles_with_negative_sides_are_illegal(self): - with self.assertRaises(TriangleError): + with self.assertRaisesWithMessage(TriangleError): Triangle(3, 4, -5) def test_triangles_violating_triangle_inequality_are_illegal(self): - with self.assertRaises(TriangleError): + with self.assertRaisesWithMessage(TriangleError): Triangle(1, 1, 3) def test_triangles_violating_triangle_inequality_are_illegal_2(self): - with self.assertRaises(TriangleError): + with self.assertRaisesWithMessage(TriangleError): Triangle(2, 4, 2) def test_triangles_violating_triangle_inequality_are_illegal_3(self): - with self.assertRaises(TriangleError): + with self.assertRaisesWithMessage(TriangleError): Triangle(7, 3, 2) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/trinary/README.md b/exercises/trinary/README.md index fa75e47a6b..a2fb566711 100644 --- a/exercises/trinary/README.md +++ b/exercises/trinary/README.md @@ -21,6 +21,21 @@ is the 3's place, the third to last is the 9's place, etc. If your language provides a method in the standard library to perform the conversion, pretend it doesn't exist and implement it yourself. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/twelve-days/README.md b/exercises/twelve-days/README.md index f121441592..e2b94f8905 100644 --- a/exercises/twelve-days/README.md +++ b/exercises/twelve-days/README.md @@ -28,6 +28,21 @@ On the eleventh day of Christmas my true love gave to me, eleven Pipers Piping, On the twelfth day of Christmas my true love gave to me, twelve Drummers Drumming, eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree. ``` +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/two-bucket/README.md b/exercises/two-bucket/README.md index 1faf655299..bbd7f8f1cc 100644 --- a/exercises/two-bucket/README.md +++ b/exercises/two-bucket/README.md @@ -7,15 +7,15 @@ Since this mathematical problem is fairly subject to interpretation / individual To help, the tests provide you with which bucket to fill first. That means, when starting with the larger bucket full, you are NOT allowed at any point to have the smaller bucket full and the larger bucket empty (aka, the opposite starting point); that would defeat the purpose of comparing both approaches! Your program will take as input: -- the size of bucket one, passed as a numeric value -- the size of bucket two, passed as a numeric value -- the desired number of liters to reach, passed as a numeric value -- which bucket to fill first, passed as a String (either 'one' or 'two') +- the size of bucket one +- the size of bucket two +- the desired number of liters to reach +- which bucket to fill first, either bucket one or bucket two Your program should determine: -- the total number of "moves" it should take to reach the desired number of liters, including the first fill - expects a numeric value -- which bucket should end up with the desired number of liters (let's say this is bucket A) - expects a String (either 'one' or 'two') -- how many liters are left in the other bucket (bucket B) - expects a numeric value +- the total number of "moves" it should take to reach the desired number of liters, including the first fill +- which bucket should end up with the desired number of liters (let's say this is bucket A) - either bucket one or bucket two +- how many liters are left in the other bucket (bucket B) Note: any time a change is made to either or both buckets counts as one (1) move. @@ -29,6 +29,21 @@ To conclude, the only valid moves are: Written with <3 at [Fullstack Academy](http://www.fullstackacademy.com/) by [Lindsay](http://lindsaylevine.com). +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/two-fer/README.md b/exercises/two-fer/README.md index a3e3cc0126..46da23102a 100644 --- a/exercises/two-fer/README.md +++ b/exercises/two-fer/README.md @@ -11,6 +11,22 @@ When X is a name or "you". If the given name is "Alice", the result should be "One for Alice, one for me." If no name is given, the result should be "One for you, one for me." + +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/variable-length-quantity/README.md b/exercises/variable-length-quantity/README.md index de56c5f39b..bf900c7fbe 100644 --- a/exercises/variable-length-quantity/README.md +++ b/exercises/variable-length-quantity/README.md @@ -31,6 +31,21 @@ Here are examples of integers as 32-bit values, and the variable length quantiti 0FFFFFFF FF FF FF 7F ``` +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/variable-length-quantity/variable_length_quantity_test.py b/exercises/variable-length-quantity/variable_length_quantity_test.py index 369b09d9fb..cb65404422 100644 --- a/exercises/variable-length-quantity/variable_length_quantity_test.py +++ b/exercises/variable-length-quantity/variable_length_quantity_test.py @@ -81,11 +81,11 @@ def test_maximum_32_bit_integer(self): self.assertEqual(decode([0x8f, 0xff, 0xff, 0xff, 0x7f]), [0xffffffff]) def test_incomplete_sequence_causes_error(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): decode([0xff]) def test_incomplete_sequence_causes_error_even_if_value_is_zero(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): decode([0x80]) def test_multiple_values(self): @@ -95,6 +95,16 @@ def test_multiple_values(self): [0x2000, 0x123456, 0xfffffff, 0x0, 0x3fff, 0x4000] ) + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/word-count/README.md b/exercises/word-count/README.md index bbf9c783ff..23576a4ecb 100644 --- a/exercises/word-count/README.md +++ b/exercises/word-count/README.md @@ -11,6 +11,21 @@ come: 1 free: 1 ``` +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/word-search/README.md b/exercises/word-search/README.md index 726b977b82..8c82286675 100644 --- a/exercises/word-search/README.md +++ b/exercises/word-search/README.md @@ -26,6 +26,21 @@ vertical and diagonal. Given a puzzle and a list of words return the location of the first and last letter of each word. +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/wordy/README.md b/exercises/wordy/README.md index c4c00c6620..53b92eabe6 100644 --- a/exercises/wordy/README.md +++ b/exercises/wordy/README.md @@ -51,6 +51,21 @@ If you'd like, handle exponentials. 32 +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/wordy/wordy_test.py b/exercises/wordy/wordy_test.py index e967e50586..fd0e844e1d 100644 --- a/exercises/wordy/wordy_test.py +++ b/exercises/wordy/wordy_test.py @@ -51,23 +51,33 @@ def test_multiple_division(self): calculate("What is -12 divided by 2 divided by -3?"), 2) def test_unknown_operation(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): calculate("What is 52 cubed?") def test_non_math_question(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): calculate("Who is the President of the United States?") # Additional tests for this track def test_missing_operation(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): calculate("What is 2 2 minus 3?") def test_missing_number(self): - with self.assertRaises(ValueError): + with self.assertRaisesWithMessage(ValueError): calculate("What is 7 plus multiplied by -2?") + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex = self.assertRaisesRegexp + except AttributeError: + pass + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + if __name__ == '__main__': unittest.main() diff --git a/exercises/zebra-puzzle/README.md b/exercises/zebra-puzzle/README.md index e47107dc1a..78ceeb2a72 100644 --- a/exercises/zebra-puzzle/README.md +++ b/exercises/zebra-puzzle/README.md @@ -25,6 +25,21 @@ drink different beverages and smoke different brands of cigarettes. Which of the residents drinks water? Who owns the zebra? +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. diff --git a/exercises/zipper/README.md b/exercises/zipper/README.md index d1241c229c..ce4ac15dc6 100644 --- a/exercises/zipper/README.md +++ b/exercises/zipper/README.md @@ -27,17 +27,30 @@ list of child nodes) a zipper might support these operations: `next` node if possible otherwise to the `prev` node if possible, otherwise to the parent node, returns a new zipper) -### Submitting Exercises +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should 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. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + + +## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. For example, if you're submitting `bob.py` for the Bob exercise, the submit command would be something like `exercism submit /python/bob/bob.py`. - For more detailed information about running tests, code style and linting, please see the [help page](http://exercism.io/languages/python). ## Submitting Incomplete Solutions - It's possible to submit an incomplete solution so you can see how others have completed the exercise.