-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
error-handling: implement exercise #798
Changes from 7 commits
f9765ef
04f385b
b1c2967
135cdb7
e8e0ae1
2a75c79
2c9575a
6543fcd
6adccde
68b8f22
fbaefd6
24c7f79
995a836
bc7eb88
6414219
d01e63e
1cb2a6d
55b13a2
b147fcd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Error Handling | ||
|
||
Implement various kinds of error handling and resource management. | ||
|
||
An important point of programming is how to handle errors and close | ||
resources even if errors occur. | ||
|
||
This exercise requires you to handle various errors. Because error handling | ||
is rather programming language specific you'll have to refer to the tests | ||
for your track to see what's exactly required. | ||
|
||
### Submitting Exercises | ||
|
||
Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory. | ||
|
||
For example, if you're submitting `bob.py` for the Bob exercise, the submit command would be something like `exercism submit <path_to_exercism_dir>/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 | ||
|
||
Problem 6 at Project Euler [http://projecteuler.net/problem=6](http://projecteuler.net/problem=6) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This source isn't right - I presume that there's actually no source needed for this exercise? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct; I shall remove it. The included source section was in the README from another exercise from which I created this one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cmccandless if it was based on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was not. I copied the exercise-specific sections from the C# README and the track-specific sections (submitting, incomplete, etc) from another Python exercise. The source section was accidentally copied with the info from the other Python exercise. |
||
|
||
## Submitting Incomplete Solutions | ||
It's possible to submit an incomplete solution so you can see how others have completed the exercise. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
def handle_error_by_throwing_exception(): | ||
pass | ||
|
||
|
||
def handle_error_by_returning_none(input_data): | ||
pass | ||
|
||
|
||
def handle_error_by_returning_tuple(input_data): | ||
pass | ||
|
||
|
||
def filelike_objects_are_closed_on_exception(filelike_object): | ||
pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import unittest | ||
|
||
import error_handling as er | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please also leave a comment stating what version of
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At this time, there is no canonical data for this exercise; these tests were adapted from the C# implemetation of this exercise. Is there a standard way of noting this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there is no tests in problem-specifications repo for this exercise, then it's Ok to leave it as is. We agreed with @N-Parsons to use comment above to denote the version of |
||
|
||
class FileLike(object): | ||
def __init__(self): | ||
self.is_open = False | ||
|
||
def open(self): | ||
self.is_open = True | ||
|
||
def __enter__(self): | ||
self.is_open = True | ||
|
||
def close(self): | ||
self.is_open = False | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you add |
||
def __exit__(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
self.is_open = False | ||
|
||
|
||
class ErrorHandlingTest(unittest.TestCase): | ||
def test_throw_exception(self): | ||
with self.assertRaises(Exception): | ||
er.handle_error_by_throwing_exception() | ||
|
||
def test_return_none(self): | ||
self.assertEqual(1, er.handle_error_by_returning_none('1'), | ||
'Result of valid input should not be None') | ||
self.assertIsNone(er.handle_error_by_returning_none('a'), | ||
'Result of invalid input should be None') | ||
|
||
def test_return_tuple(self): | ||
successful_result, result = er.handle_error_by_returning_tuple('1') | ||
self.assertTrue(successful_result, 'Valid input should be successful') | ||
self.assertEqual(1, result, 'Result of valid input should not be None') | ||
|
||
failure_result, result = er.handle_error_by_returning_tuple('a') | ||
self.assertFalse(failure_result, | ||
'Invalid input should not be successful') | ||
|
||
def test_filelike_objects_are_closed_on_exception(self): | ||
filelike_object = FileLike() | ||
filelike_object.open() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that we should be passing an open object to the user's function. It seems strange to me that we'd pass something open to a function that (if we weren't just throwing an exception) may or may not close it - I would expect closing to be handled in the place that it's opened. I think it should be up to the user to open and close the filelike object however they see fit. |
||
with self.assertRaises(Exception): | ||
er.filelike_objects_are_closed_on_exception(filelike_object) | ||
self.assertFalse(filelike_object.is_open) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
def handle_error_by_throwing_exception(): | ||
raise Exception() | ||
|
||
|
||
def handle_error_by_returning_none(input_data): | ||
try: | ||
return int(input_data) | ||
except ValueError: | ||
return None | ||
|
||
|
||
def handle_error_by_returning_tuple(input_data): | ||
try: | ||
return (True, int(input_data)) | ||
except ValueError: | ||
return (False, None) | ||
|
||
|
||
def filelike_objects_are_closed_on_exception(filelike_object): | ||
with filelike_object: | ||
filelike_object.close() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't closing the filelike object on an exception, it's closing the filelike object and then raising an exception afterwards. A better solution might be to have a It'd probably then be worth using the exercise's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, as a learner I would expect that a context manager could be used here (eg. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The built-in |
||
raise Exception() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've switched to
snake_case
naming convention for topics to increase consistency among tracks, so could you please choose them from the following list - https://github.com/exercism/problem-specifications/blob/master/TOPICS.txt