Skip to content
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

[No MRG, No Review] Refactor _fetch_file(s) #1835

Closed
wants to merge 7 commits into from

Conversation

kchawla-pi
Copy link
Collaborator

While working on #1792 I found it painful & slow to read the _fetch_files() & fetch_file().
Additionally, the new URLs on OSF may necessitate changing how the passed in URLs are handled.

As per issue #1769, I have refactored the function into several smaller ones, which have made it considerably easier for me to comprehend it, and modify it if necessary.

The logic has not been altered to maintain Python2 compatibility & make the refactoring quick, safe, and easy.
This is in line with industry standard clean code principles for improving code quality and something I intend to do simultaneously where ever I see code that is difficult to comprehend.

* 'master' of https://github.com/nilearn/nilearn:
  Update nilearn/image/tests/test_resampling.py
  Update nilearn/image/tests/test_resampling.py
  Update nilearn/image/tests/test_resampling.py
  Updated News section of the website nilearn.github.io
  Updated number of commits by contributer
  Changed version from 0.5.0a to 0.5.0b
  Added list of contributors for the alpha release
  Updated whats new & aAdded list of contributors for the beta release
  Simplified the test and fixed an issue previously introduced in the doc.
  Renamed extrapolation parament "fill_value". Added one test.
  Added support for fill value in resampling tools.
Copy link
Member

@bthirion bthirion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we take this opportunity to test these functions ?

@@ -774,6 +765,49 @@ def _fetch_files(data_dir, files, resume=True, mock=False, verbose=1):
return files_


def _download_or_mock_file(abort, dl_file, file_, mock, target_file,
temp_target_file, url):
if mock:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a one-line function description to explain in a nutshell what the function does ?

return abort


def _move_dataset_files(dl_file, opts, temp_dir):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(same for other private functions)

@kchawla-pi
Copy link
Collaborator Author

The appveyor failure is spurious.

@codecov
Copy link

codecov bot commented Oct 23, 2018

Codecov Report

Merging #1835 into master will increase coverage by <.01%.
The diff coverage is 94.87%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1835      +/-   ##
==========================================
+ Coverage   95.21%   95.21%   +<.01%     
==========================================
  Files         135      135              
  Lines       17071    17092      +21     
==========================================
+ Hits        16254    16275      +21     
  Misses        817      817
Impacted Files Coverage Δ
nilearn/datasets/utils.py 81.21% <94.87%> (+0.88%) ⬆️
nilearn/signal.py 91.12% <0%> (ø) ⬆️
nilearn/tests/test_signal.py 100% <0%> (ø) ⬆️
nilearn/image/tests/test_image.py 98.85% <0%> (ø) ⬆️
nilearn/image/image.py 97.94% <0%> (+0.02%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a993148...4a3d679. Read the comment docs.

target_file_does_not_exist = not os.path.exists(file_1)
temp_target_file_does_not_exist = not os.path.exists(file_2)
file_does_not_exist = target_file_does_not_exist and temp_target_file_does_not_exist
return file_does_not_exist
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3-liner functions are in general bad practice. The reason being that when doing a careful read of the codebase, one needs to read them to know what they actually do (the name is not a contract, and bugs hide this).

I am not very enthousiastic about merging changes that turn 3 lines of code used twice into a function. It makes the code harder to read and maintain.

os.remove(file_)
except Exception as e:
abort = str(e)
return abort
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is used only once, and understanding what it does is as difficult from the code as from the function name and description. It should not be factored out.

-------
dst_filepath: string
Path to the moved file.
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another function that is very simple and used once.

@GaelVaroquaux
Copy link
Member

@kchawla-pi : I am really sorry, but these changes do not make the codebase easier to maintain, rather harder. I've worked on many codebases, and codebases that are structured like this (with many tiny functions used once or twice) are very hard to follow: they require jumping from one function to another all the time, and keeping in one's working memory what the functions do.

I am not in favor of merging these changes. Let's try to discuss this IRL.

@kchawla-pi kchawla-pi changed the title [WIP] Refactor _fetch_file(s) [No MRG, No Review] Refactor _fetch_file(s) Oct 24, 2018
@kchawla-pi
Copy link
Collaborator Author

I am open to learning a better way to refactor existing code to improve its readability.

@kchawla-pi kchawla-pi closed this Oct 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants