-
Notifications
You must be signed in to change notification settings - Fork 8
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
Make error handling part of the public interface #25
Changes from all commits
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,40 @@ | ||
Error Handling | ||
============== | ||
|
||
Reading of ecl files will throw :py:class:`ecl_data_io.EclParsingError` | ||
when the given file does not contain valid ecl data: | ||
|
||
.. doctest:: | ||
|
||
>>> from ecl_data_io import read, write, EclParsingError, EclWriteError | ||
>>> from io import StringIO | ||
>>> | ||
>>> file_contents = StringIO("Not valid ecl content") | ||
>>> try: | ||
... read(file_contents) | ||
... except EclParsingError as e: | ||
... print(e) | ||
Expected "'" before keyword, got N at 1 | ||
|
||
Similarly, write will produce :py:class:`ecl_data_io.EclWriteError` | ||
when the given data is not suitable for writing. | ||
|
||
.. doctest:: | ||
|
||
>>> try: | ||
... write("my_file.egrid", [("FILEHEAD", ["a"*100])]) | ||
... except EclWriteError as e: | ||
... print(e) | ||
Could not convert numpy type <U100... | ||
|
||
|
||
For file and stream operations, the underlying exceptions from open(), read(), and | ||
write() are passed through: | ||
|
||
.. doctest:: | ||
|
||
>>> try: | ||
... read("does_not_exist/my_file.egrid", []) | ||
... except OSError as e: | ||
... print(e) | ||
[Errno 2] No such file or directory... |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,17 @@ def lazy_read(filelike, fileformat=None): | |
:param fileformat: Either ecl_data_io.Format.FORMATTED for ascii | ||
format, ecl_data_io.Format.UNFORMATTED for binary formatted files | ||
or None for guess. | ||
|
||
:raises ecl_data_io.EclParsingError: If the file is not a valid | ||
ecl file. | ||
|
||
.. note:: | ||
If given a file to be open (as opposed to a stream), the errors | ||
(various `IOError` s) associated with the default behavior of the | ||
built-in `open()` function may be raised. | ||
|
||
When given a stream, the exceptions associated with the stream will | ||
pass through. | ||
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. Would it be possible to have a test for error handling? It would be easier to visualise what you mean by the condition 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 have added some doctests for this |
||
""" | ||
if fileformat is None: | ||
fileformat = guess_format(filelike) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,17 @@ def write(filelike, contents, fileformat=Format.UNFORMATTED): | |
will be converted according to ecl_data_io.types.to_np_type | ||
:param fileformat: Either ecl_data_io.Format.FORMATTED for ascii | ||
format or ecl_data_io.Format.UNFORMATTED for binary format. | ||
|
||
:raises ecl_data_io.EclWriteError: If the given contents cannot be | ||
written to an ecl file. | ||
|
||
.. note:: | ||
If given a file to be open (as opposed to a stream), the errors | ||
(various `IOError` s) associated with the default behavior of the | ||
built-in `open()` function may be raised. | ||
|
||
When given a stream, the exceptions associated with the stream will | ||
pass through. | ||
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. Same as above. 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 added doctest for this. |
||
""" | ||
stream, didopen = get_stream(filelike, fileformat, mode="w") | ||
|
||
|
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.
Is
ecl
always referring to Eclipse? If so, why don't we write the full name?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.
ecl is the name of the group that made eclipse. Its a convention now, perhaps something to change, but that is perhaps a bigger issue than this PR as ecl is in the name of the package.