Skip to content

Expose Entrez cache to allow user to set custom cache directory (AWS Lambda deployment, etc) - #1522

Merged
mdehoon merged 21 commits into
biopython:masterfrom
cparmet:master
Apr 28, 2018
Merged

mdehoon merged 21 commits into
biopython:masterfrom
cparmet:master

Conversation

@cparmet

@cparmet cparmet commented Jan 31, 2018

Copy link
Copy Markdown
Contributor

This pull request addresses issue #1517

I hereby agree to dual licence this and any previous contributions under both
the Biopython License Agreement AND the BSD 3-Clause License.

I have read the CONTRIBUTING.rst file and understand that AppVeyor and
TravisCI will be used to confirm the Biopython unit tests and flake8 style
checks pass with these changes.

I am happy be thanked by name in the NEWS.rst and CONTRIB.rst files,
and have added myself to those files as part of this pull request. (This
acknowledgement is optional. Note we list the names sorted alphabetically.
)

@cparmet
cparmet requested a review from mdehoon as a code owner January 31, 2018 02:45
@codecov

codecov Bot commented Jan 31, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #1522 into master will increase coverage by 0.36%.
The diff coverage is 76.92%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1522      +/-   ##
==========================================
+ Coverage   85.05%   85.42%   +0.36%     
==========================================
  Files         326      327       +1     
  Lines       49506    49687     +181     
==========================================
+ Hits        42108    42444     +336     
+ Misses       7398     7243     -155
Impacted Files Coverage Δ
Bio/Entrez/Parser.py 69.78% <76.92%> (+1.21%) ⬆️
Bio/AlignIO/__init__.py 82.64% <0%> (-2.11%) ⬇️
Bio/AlignIO/EmbossIO.py 73.68% <0%> (-1.71%) ⬇️
Bio/AlignIO/ClustalIO.py 88.88% <0%> (-1.59%) ⬇️
Bio/AlignIO/MafIO.py 92.88% <0%> (-0.24%) ⬇️
Bio/Data/SCOPData.py 100% <0%> (ø) ⬆️
Bio/Data/__init__.py 100% <0%> (ø) ⬆️
Bio/Graphics/GenomeDiagram/_Diagram.py 98.23% <0%> (ø) ⬆️
BioSQL/BioSeq.py 93.29% <0%> (ø) ⬆️
Bio/Seq.py 98.48% <0%> (ø) ⬆️
... and 83 more

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 1cb73fc...bf0e58e. Read the comment docs.

Comment thread Bio/Entrez/__init__.py Outdated
E-utilities.""", UserWarning)
# Tell Entrez if we want to set a custom cache location.
if "cache" not in params:
params["cache"] = cache

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think these lines are needed. IIRC, this is building a record of all the parameters to be encoding into the URL or post sent to the NCBI. They don't need to know about where we might be caching DTD files on the user's machine (and revealing this could leak information like their computer account username).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@peterjc Ah I see. Will delete. I mistakenly thought they were for internal parameters, and it makes sense that they're for NCBI given tool & email.

Comment thread Bio/Entrez/__init__.py Outdated

- email Set the Entrez email parameter (default is not set).
- tool Set the Entrez tool parameter (default is ``biopython``).
- cache Set the Entrez cache parameter (default is not set).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about Set the directory to cache NCBI DTD files for the Entrez XML parser (if not set the default is under your home directory).

We could be even more explicit about the default path?

@cparmet cparmet Jan 31, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@peterjc Much better when it's explicit. I've replaced the comment with your new one.

Comment thread Bio/Entrez/Parser.py Outdated

# Check if user has set a custom cache location
if Entrez.cache is not None:
directory = Entrez.cache

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we add a check here? Maybe:

if not os.path.isdir(directory):
    raise ValueError("Please set Entrez.cache to a directory name (or None)")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Just confirming I understand, this would mean the cache must be an existing directory that user creates before running Entrez, right? In my test code, I used the cache parameter to have Entrez create the tmp folder ad hoc. If this is bad form, it makes sense to me to enforce that the cache must pre-exist.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

True, this could be done either way since the code will create the directory if it does not exist... its a design choice. Now that you've pointed this out I have no strong view either way.

@peterjc

peterjc commented Feb 1, 2018

Copy link
Copy Markdown
Member

I've only just noticed the test. While we do have some print-and-compare tests, they require the expected output be saved as a text file (see the chapter in the Tutorial on this). Using the Python unittest framework would be nicer.

I think to be complete, the test would need to set Entrez.cache to an empty folder created with tempfile, and then do an Entrez XML parsing example which is expected to download a DTD file, and confirm not only that the parsing worked, but also that the DTD file was downloaded. That does seem quite complicated though... so something based on your current test ought to be enough.

@cparmet

cparmet commented Feb 2, 2018

Copy link
Copy Markdown
Contributor Author

@peterjc Thank you for this guidance on best practices for the Test script. I'll work on this!

@cparmet

cparmet commented Feb 5, 2018 via email

Copy link
Copy Markdown
Contributor Author

@peterjc

peterjc commented Feb 6, 2018

Copy link
Copy Markdown
Member

Changing working DTD files in the global DTD directory during the tests seems to me a risky idea.

I've not looked into this in enough detail to be confident how we could write a non-invasive test with no side effects.

For the test I was suggesting in #1522 (comment) , you are right - we'd need to find a valid NCBI DTD file which is not currently included in Bio/Entrez/DTDs. Or, we deliberately stop providing one of the minor rarely used files (since in principle Biopython would download and cache it on demand in real world usage).

Perhaps you could make an artificial example with an invented DTD file, and an XML file which refers to the invented DTD file. With the cache set to a test folder with the invented DTD file in it, confirm that Entrez.parse can parse the XML file.

I'm willing to be practical here - if we can't easily come up with an automated test, we could still include the new functionality (after manual testing)? However, @mdehoon is the module maintainer so his thoughts are important here.

@peterjc

peterjc commented Apr 2, 2018

Copy link
Copy Markdown
Member

@mdehoon do you have any input here please (on exposing the DTD cache folder as a user-configurable setting)? Thanks!

@mdehoon

mdehoon commented Apr 3, 2018

Copy link
Copy Markdown
Contributor

@peterjc @cparmet I understand the need to set the directory, but I would reorganize the code a bit. Specifically, I would suggest to make "directory" a property of the DataHandler class (instead of a local variable when DataHandler is first initialized), so that it can be changed at any time. Then, instead of initializing local_dtd_dir etc. at the top of DataHandler class, they can be initialized whenever the directory property changes, or whenever local_dtd_dir etc. is actually used. Using directory as a property, it must be possible to set up the code such that (a) local_dtd_dir etc. always makes use of the current value of directory, and (b) local_dtd_dir etc. does not get initialized when the module is first imported (i.e. before the user had a chance to set local_dtd_dir).
If directory is a property of DataHandler, then there is no need for a separate Bio.Entrez.cache variable. I agree that Bio.Entrez.cache would be nicer than a directory variable hidden in Bio.Entrez.Parser.DataHandler, but I don't see an easy way right now to track if the value of Bio.Entrez.cache has changed.

@cparmet

cparmet commented Apr 4, 2018 via email

Copy link
Copy Markdown
Contributor Author

@peterjc

peterjc commented Apr 4, 2018 •

Copy link
Copy Markdown
Member

Please ignore this comment; I wrote this on the wrong issue:

In terms of testing the timeout specifically? That's tricky as we don't control the online resources.

We could do this with a mock-up (monkey patch something internal, even urlopen - perhaps using the mock framework) to confirm the user-set timeout value gets passed in correctly.

However, in this case I would be content with the existing tests exercising the code path.

(Note for the CodeCov figures reported here on GitHub, we are measuring the test coverage without the online tests, as we don't want to impact the various online resources we use)

@cparmet

cparmet commented Apr 4, 2018 via email

Copy link
Copy Markdown
Contributor Author

@peterjc

peterjc commented Apr 4, 2018

Copy link
Copy Markdown
Member

Apologies about the timeouts thing - I commented on the wrong issue

@peterjc

peterjc commented Apr 4, 2018

Copy link
Copy Markdown
Member

OK, Entrez directory cache settings, and tests.

In terms of testing, no, please do not use a print-and-compare test.

Instead use the Python unittest framework. There is a chapter about our testing setup in the Biopython Tutorial, see http://biopython.org/DIST/docs/tutorial/Tutorial.html or http://biopython.org/DIST/docs/tutorial/Tutorial.pdf

This would probably fit within Tests/test_Entrez.py

Note that as I wrote earlier:

I'm willing to be practical here - if we can't easily come up with an automated test, we could still include the new functionality (after manual testing)? However, @mdehoon is the module maintainer so his thoughts are important here.

@cparmet

cparmet commented Apr 4, 2018 via email

Copy link
Copy Markdown
Contributor Author

@mdehoon

mdehoon commented Apr 5, 2018

Copy link
Copy Markdown
Contributor

@cparmet I agree with @peterjc 's comments to use Python's unittest framework, and to use Python's tempfile module to create the temporary file. Creating a file under /tmp directly may work, but it can be fragile on some platforms, especially Windows.

@cparmet

cparmet commented Apr 7, 2018

Copy link
Copy Markdown
Contributor Author

@mdehoon Hi - I've implemented directory as a property of DataHandler, and added an offline unittest. The revised module works on a test app I deployed to AWS Lambda, which was my initial use case for this PR. (Lambda only gives the app write access to a specific tmp directory, so Entrez calls fail without this modification.)

See what you think.

cc: @peterjc

Comment thread Bio/Entrez/Parser.py Outdated
self.dtd_urls.pop()
return 1

def _initialize_directory(self, directory_type):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It isn't clear from the docstring what argument directory_type should be. You use a mix of strings like directory_type='XSD' and a list like directory_type=['DTD', 'XSD']

In anyway case, why not always initialise both the DTD and XSD paths? That would make the internal API simpler too.

@peterjc

peterjc commented Apr 9, 2018 •

Copy link
Copy Markdown
Member

I had a quibble about the internal API, but the user facing API seems nice 👍

Also, the test looks sensible.

@peterjc

peterjc commented Apr 9, 2018

Copy link
Copy Markdown
Member

What do you think @mdehoon? OK to merge (I'd suggest squash-and-merge given how much this as changed)?

This lets the user set a private attribute, but the cache directory could in future be exposed as an explicit argument to __init__ instead.

@cparmet Do you want to be thanked by name in the NEWS.rst and CONTRIB.rst files?

@cparmet

cparmet commented Apr 9, 2018

Copy link
Copy Markdown
Contributor Author

@peterjc Hi - Per your comment, I removed that directory_type parameter and committed a new version.

I would be happy to be thanked by name in those files. Much appreciated!

PS: My original thought on the directory_name was whether there could be an efficiency to specifying DTD or XSD, if a setup would only need/allow the creation of 1 such directory. Such as a serverless deployments that delete the tmp folder immediately. But I see now from your comment on this, any efficiency would be tiny and not worth the added user complexity.

@peterjc

peterjc commented Apr 10, 2018

Copy link
Copy Markdown
Member

I've not worked with a server which aggressively removes removes temp files, so I had not thought of that.

@cparmet

cparmet commented Apr 10, 2018

Copy link
Copy Markdown
Contributor Author

On AWS Lambda at least (I’m not sure about other serverless platforms from Google etc), code/apps are forbidden from creating files that persist on the app server. The temp directory is purged immediately after the Python code finishes.

@cparmet

cparmet commented Apr 24, 2018

Copy link
Copy Markdown
Contributor Author

@peterjc @mdehoon Hi. Just checking, is the codecov patch test an expected fail since this unit test is, by design, an offline test only? So we're not planning to test all the online pathways through the DTD/XSD resourcing steps?

Do I understand correctly, or are there more unit tests I should add?

Thanks!

@peterjc

peterjc commented Apr 24, 2018

Copy link
Copy Markdown
Member

Currently our TravisCI and AppVeyor setups deliberately do not run our online tests, which means that our CodeCov information does not track that.

In any case, we treat the coverage as advisory rather than a hard requirement for pull requests.

@cparmet

cparmet commented Apr 24, 2018

Copy link
Copy Markdown
Contributor Author

Very cool that you have it set up to distinguish offline vs online tests. Thanks for explaining, @peterjc !

@mdehoon

mdehoon commented Apr 27, 2018

Copy link
Copy Markdown
Contributor

Looks good to me. I would suggest to remove the empty returns at the end of _initialize_directory and directory.setter, as they are unnecessary. Otherwise I'm OK to squash and merge.

@cparmet

cparmet commented Apr 27, 2018

Copy link
Copy Markdown
Contributor Author

@mdehoon @peterjc Empty returns removed and pushed new commit.

Thank you both for all the guidance you gave me along the way of this PR. I learned a lot!

@mdehoon
mdehoon merged commit 374833c into biopython:master Apr 28, 2018
cparmet added a commit to cparmet/biopython that referenced this pull request Apr 29, 2018
peterjc pushed a commit that referenced this pull request May 1, 2018
Following PR 1522
#1522
Comment thread Bio/Entrez/Parser.py

def open_dtd_file(self, filename):
path = os.path.join(DataHandler.local_dtd_dir, filename)
self._initialize_directory()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actually why was this line needed here? We are already calling self._initialize_directory from directory.setter.

Same for the calls to self._initialize_directory below.

@cparmet cparmet Nov 3, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@mdehoon Great question. I'm refreshing my memory.

Am I correct that the directory.setter function will be called:

  1. when the DataHandler object is created (__init__)
  2. again if the user sets a custom self_directory (local cache)
    ?

If that's true, then I see your point. There shouldn't be a need to call _initialize_directory yet again in the open* and save* functions, if self_directory should not have changed in the interim? It will already have gotten initialized one way or another? It's possible those 4 instances were added for another reason I can't recall. I apologize, I haven't been in this codebase since the PR.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's easy to set it up like that. But should directory be a class attribute or an object attribute? Right now it is an object attribute, meaning that it has to be set separately each time a DataHandler is created.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry for the delay in responding, @mdehoon .

Can you catch me up? It looks like the _initialize_directory method from this PR has since evolved into the directory method decorated by @directory.setter. But it sounds like we are still making this unnecessary call to that code (formerly in self.initialize_directory) in the 4 open and save methods? Can you trace the story for me?

Apologies, I'm rusty from being away from this repo. I'm very happy to help clean up my PR!

When I look at the latest Master, open_dtd_file and cousin methods don't seem to initialize anymore or set directory directly. From your comment I am guessing each method somehow invokes a directory=, etc, and I'm just missing it now. Thank you!

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.

3 participants