-
-
Notifications
You must be signed in to change notification settings - Fork 573
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
Fido.fetch now accepts pathlib.Path objects #2559
Conversation
Hello @vn-ki! Thanks for updating the PR. Cheers ! There are no PEP8 issues in this Pull Request. 🍻 Comment last updated on March 21, 2018 at 20:18 Hours UTC |
sunpy/net/dataretriever/client.py
Outdated
@@ -336,6 +337,15 @@ def fetch(self, qres, path=None, error_callback=None, **kwargs): | |||
------- | |||
Results Object | |||
""" | |||
# Check for type of path | |||
if isinstance(path, str) or path is None: | |||
pass |
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.
Do we need this if statement?
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.
Also I wonder why the documentation string for this is empty of the keywords.
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.
This is to ensure that the path which is None
, str
or pathlib.Path
goes past. Any other type would raise
a TypeError
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 the if,elif,else
block be refactored to avoid having the if:pass part?
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.
I don't see how we can go past not checking whether path is str
or None
.
If you have some suggestion on how to do this, can you share?
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.
@Cadair might
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.
I haven't tested this, so it probably has several syntax issues, but something like this maybe:
if path is not None:
if isinstance(path, pathlib.Path) and HAS_PATHLIB:
path = str(path.absolute())
elif not isinstance(path, str): # need py2 fix for six here?
raise TypeError("path keyword is invalid. Should be a 'str' or 'pathlib.Path'.\
Got '{}'.".format(type(path)))
I think if we check for str we need to use six to ensure python 2 compatibility.
assert len(response1) == qr._numfile | ||
path2 = pathlib.Path('.') | ||
response2 = Fido.fetch(qr, path=path2) | ||
assert len(response2) == qr._numfile |
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.
I wonder if Fido.fetch doesn't already have tests for this somewhere else. Not sure that this should be added just under an eve test.
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.
Sorry for not looking properly. Just found the real test under net.tests.test_fido
sunpy/net/dataretriever/client.py
Outdated
elif isinstance(path, pathlib.Path): | ||
path = str(path.absolute()) | ||
else: | ||
err = 'path should be either pathlib.Path object or str object. '\ |
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.
Not sure I like using the word object here.
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.
What is a 'word' object?
sunpy/net/tests/test_fido.py
Outdated
@@ -97,6 +98,14 @@ def test_save_path(): | |||
assert target_dir in f | |||
assert "eve{}0".format(os.path.sep) in f | |||
|
|||
with tempfile.TemporaryDirectory() as target_dir: | |||
qr = Fido.search(a.Instrument('EVE'), a.Time("2016/10/01", "2016/10/02"), a.Level(0)) | |||
path = pathlib.Path(os.path.join(target_dir, "{instrument}"+os.path.sep+"{level}")) |
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.
Do pathlib.Path
need to use os.path.join
?
I had thought that pathlib.Path was meant to handle that.
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.
Missed it while copy-pasting!
sunpy/net/tests/test_fido.py
Outdated
@@ -97,6 +98,14 @@ def test_save_path(): | |||
assert target_dir in f | |||
assert "eve{}0".format(os.path.sep) in f | |||
|
|||
with tempfile.TemporaryDirectory() as target_dir: |
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.
I do wonder if we should split it into two tests.
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 are downloading the same files. So I figured downloading the files into a different directory would be the best option to test for flaws.
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.
Well in that case, do we need to redo the Fido.search part? Could we not just fetch it twice one after the other?
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.
Should I split it into two tests? @nabobalis
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 we not just fetch the files twice with two different paths?
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.
Now the searching is only done once.
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.
Can you add a comment somewhere to just state what the difference in the two parts of the test are.
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.
It is in a different test now! ^_^
Add changelog Add keyword description in docstring Change error message yo requested format Remove recurrent os.path.join
b31ecb9
to
53be326
Compare
sunpy/net/tests/test_fido.py
Outdated
import pathlib | ||
HAS_PATHLIB = True | ||
except ImportError: | ||
HAS_PATHLIB = False |
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.
Would we not need this for the main file as well?
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.
Fixed!
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.
instead of doing this we should use pytest.importorskip("pathlib")
in the test
eab6efe
to
d767404
Compare
Looks my logic doesn't work:
|
sunpy/net/dataretriever/client.py
Outdated
Returns | ||
------- | ||
Results Object | ||
""" | ||
# Check for type of path | ||
if path is not None and HAS_PATHLIB and isinstance(path, pathlib.Path): |
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.
You want if path is not None
to encompass the other if/elif
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.
Oops! Sorry!
9f0808f
to
d66b2e3
Compare
sunpy/net/dataretriever/client.py
Outdated
@@ -8,6 +8,15 @@ | |||
from abc import ABCMeta | |||
from collections import OrderedDict, namedtuple | |||
from functools import partial | |||
import six |
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.
You probably want this to be import sunpy.extern.six
.
d66b2e3
to
3b226ce
Compare
sunpy/net/tests/test_fido.py
Outdated
files = Fido.fetch(qr, path=os.path.join(target_dir, "{instrument}"+os.path.sep+"{level}")) | ||
for f in files: | ||
assert target_dir in f | ||
assert "eve{}0".format(os.path.sep) in f | ||
|
||
# Test when path is pathlib.Path |
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.
Shouldn't this go in its own test? It would be easier to manage and control when one or the other fails.
sunpy/net/tests/test_fido.py
Outdated
with tempfile.TemporaryDirectory() as target_dir: | ||
qr = Fido.search(a.Instrument('EVE'), a.Time("2016/10/01", "2016/10/02"), a.Level(0)) | ||
files = Fido.fetch(qr, path=os.path.join(target_dir, "{instrument}"+os.path.sep+"{level}")) |
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.
os.path.sep
is not needed there... unless that's what you want to test. os.path.join
does it for you.
files = Fido.fetch(qr, path=os.path.join(target_dir, "{instrument}", "{level}"))
sunpy/net/tests/test_fido.py
Outdated
# Test when path is pathlib.Path | ||
if HAS_PATHLIB: | ||
with tempfile.TemporaryDirectory() as target_dir: | ||
path = pathlib.Path(target_dir, "{instrument}"+os.path.sep+"{level}") |
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.
You don't need it here either, pathlib
will add the sep for each argument:
path = pathlib.Path(target_dir, "{instrument}", "{level}")
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.
Looks good to me.
This also now fixes the two known test failures on the online tests. |
This can be merged now, right? |
I have requested a review and I am waiting on that before I merge this. But as far as I am concerned, yes it can be merged in. |
sunpy/net/dataretriever/client.py
Outdated
@@ -9,6 +9,14 @@ | |||
from collections import OrderedDict, namedtuple | |||
from functools import partial | |||
|
|||
# Remove in 1.0 |
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.
can you modify this comment to start with python 3:
as I have been using that in a few places for post-0.9 grepping.
sunpy/net/tests/test_fido.py
Outdated
import pathlib | ||
HAS_PATHLIB = True | ||
except ImportError: | ||
HAS_PATHLIB = False |
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.
instead of doing this we should use pytest.importorskip("pathlib")
in the test
|
||
@pytest.mark.remote_data | ||
def test_save_path_pathlib(): | ||
pathlib = pytest.importorskip('pathlib') |
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 this not a decorator?
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.
no, decorators are run at import time.
We updated the Changelog, you will need to fix the conflict now. |
Closes #2400.