-
-
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
Merged
N-Parsons
merged 19 commits into
exercism:master
from
cmccandless:implement-error-handling
Oct 24, 2017
+134
−0
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
f9765ef
(WIP) error-handling: implement exercise
cmccandless 04f385b
error-handling: update config.json
cmccandless b1c2967
error-handling: add README
cmccandless 135cdb7
error-handling: add solution template
cmccandless e8e0ae1
error-handling: write test cases
cmccandless 2a75c79
write example solution
cmccandless 2c9575a
error-handling: fixes for flake8 compliance
cmccandless 6543fcd
error-handling: change topics to snake_case
cmccandless 6adccde
error-handling: remove incorrect Source section
cmccandless 68b8f22
error-handling: improve use of context manager
cmccandless fbaefd6
Merge branch 'master' into implement-error-handling
cmccandless 24c7f79
error-handling: further improve context manager implementation
cmccandless 995a836
Merge branch 'master' into implement-error-handling
cmccandless bc7eb88
error-handling: remove redundant error handling inside "with"
cmccandless 6414219
Merge branch 'master' into implement-error-handling
cmccandless d01e63e
error-handling: replace assertTrue and assertFalse with assertIs
cmccandless 1cb2a6d
Merge branch 'implement-error-handling' of github-personal:cmccandles…
cmccandless 55b13a2
error-handling: conform to parameter order convention
cmccandless b147fcd
Merge branch 'master' into implement-error-handling
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# 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). | ||
|
||
## Submitting Incomplete Solutions | ||
It's possible to submit an incomplete solution so you can see how others have completed the exercise. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import unittest | ||
|
||
import error_handling as er | ||
|
||
|
||
class FileLike(object): | ||
def __init__(self): | ||
self.is_open = False | ||
self.was_open = False | ||
self.did_something = False | ||
|
||
def open(self): | ||
self.was_open = False | ||
self.is_open = True | ||
|
||
def close(self): | ||
self.is_open = False | ||
self.was_open = True | ||
|
||
def __enter__(self): | ||
self.open() | ||
return self | ||
|
||
def __exit__(self, *args): | ||
self.close() | ||
|
||
def do_something(self): | ||
self.did_something = True | ||
raise Exception() | ||
|
||
|
||
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(er.handle_error_by_returning_none('1'), 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.assertIs(successful_result, True, | ||
'Valid input should be successful') | ||
self.assertEqual(result, 1, 'Result of valid input should not be None') | ||
|
||
failure_result, result = er.handle_error_by_returning_tuple('a') | ||
self.assertIs(failure_result, False, | ||
'Invalid input should not be successful') | ||
|
||
def test_filelike_objects_are_closed_on_exception(self): | ||
filelike_object = FileLike() | ||
with self.assertRaises(Exception): | ||
er.filelike_objects_are_closed_on_exception(filelike_object) | ||
self.assertIs(filelike_object.is_open, False, | ||
'filelike_object should be closed') | ||
self.assertIs(filelike_object.was_open, True, | ||
'filelike_object should have been opened') | ||
self.assertIs(filelike_object.did_something, True, | ||
'filelike_object should call do_something()') | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
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 as fobj: | ||
fobj.do_something() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could you please also leave a comment stating what version of
canonical-data.json
the tests were adopted as discussed in #784 if aplicable?The format is:
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.
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 comment
The 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
canonical-data.json
from which tests were adopted