Repository navigation
Expose Entrez cache to allow user to set custom cache directory (AWS Lambda deployment, etc) - #1522
Conversation
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
| E-utilities.""", UserWarning) | ||
| # Tell Entrez if we want to set a custom cache location. | ||
| if "cache" not in params: | ||
| params["cache"] = cache |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
@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.
|
|
||
| - 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). |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
@peterjc Much better when it's explicit. I've replaced the comment with your new one.
|
|
||
| # Check if user has set a custom cache location | ||
| if Entrez.cache is not None: | ||
| directory = Entrez.cache |
There was a problem hiding this comment.
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)")There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
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 I think to be complete, the test would need to set |
|
@peterjc Thank you for this guidance on best practices for the Test script. I'll work on this! |
|
@peterjc - I'm working on setting up the 'complete test' as a unittest. Can
you confirm my strategy, in particular the best way to force Parser to
download a new DTD to the cache? This issue may be why you suggested such a
test is complicated, but I thought I'd give it a shot. Happy to proceed
with testing as you deem best.
*Possible unit test strategy*
- In global DTD directory, rename this file: Bio/Entrez/DTDs/
esummary_v*.dtd.
Change name to something Parser won't find, like holdout_for_cache_test.dtd.
- Set cache to /temp
- Call Entrez.esummary for a PMID.
- Confirm parsing worked.
- Confirm that Entrez downloaded a new esummary_v*.dtd file and put it
in temp/Bio/Entrez/DTDs.
- Delete /temp directory
- Restore the original Global DTD file - Rename holdout_for_cache_test.dtd
back to esummary_v*.dtd
I hesitate because this unit test would modify (renames) a working DTD
file, simply for the purposes of testing. Otherwise, Parser will find the
DTD in global DTDs, and not download a local one to the cache.
(On AWS Lambda, the new cache feature works successfully. There is of
course no global DTD directory then, and all DTDs are downloaded as needed
to a tmp cache. The above strategy mimics that environment, I suppose.)
Is there another Entrez call that always requires a fresh download to local
(no DTD in global DTDs, on any test environment a user might have)?
*If it's better to do a print-and-compare test *
I could remove the steps that involve renaming/restoring the Global DTD
file.
I'd leave a note at top of the test script to direct the user to perform
those steps manually before/after running the script, to test the cache
feature.
Would that be sufficient and consistent with best practices for Biopython
testing?
Thank you, Peter!
…On Thu, Feb 1, 2018 at 7:31 AM, Peter Cock ***@***.***> wrote:
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.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1522 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AYQVb3CR_8HYJvUu3hebPQB7Dr87IWwdks5tQa6ygaJpZM4RzV0k>
.
|
|
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 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 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. |
|
@mdehoon do you have any input here please (on exposing the DTD cache folder as a user-configurable setting)? Thanks! |
|
@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). |
|
@peterjc @mdehoon Sounds great. i will give it a shot and let you know any
questions.
Could you also suggest a preferred strategy for unit /other testing? Please
see discussion above.
…On Mon, Apr 2, 2018 at 9:53 PM mdehoon ***@***.***> wrote:
@peterjc <https://github.com/peterjc> @cparmet
<https://github.com/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.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1522 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AYQVbyE5SI9_Ds8ZYubJuPbeSUgjGt5Jks5tktYbgaJpZM4RzV0k>
.
|
|
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 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) |
|
@peterjc I'm sorry I don't understand the issues related to user-set
timeout values or online resources, with regards to the custom DTD/XSD
directories. I'm new to the guts of Biopython so forgive me!
To confirm, you'd be content with the strategy of the simple "print" type test
file
<https://github.com/biopython/biopython/blob/538223fdf0af149204f6c450185efa9467012b40/Tests/Entrez/test_Entrez_cache.py>I
included with the February commits? I'd, of course, update it after making
the directory changes recommended by @mdehoon. The test file calls esummary
with a custom directory (currently Bio.Entrez.cache) and checks if the call
worked and the DTD file was downloaded there.
I believe back in February we were discussing whether there was a
reasonable unit test I could write instead of print type tests. Sorry about
not being more specific, this is what I was hoping to double check in my
last comment.
I'm happy to do whatever you two advise would be most Biopython-ic!
…On Wed, Apr 4, 2018 at 5:20 AM, Peter Cock ***@***.***> wrote:
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) 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)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1522 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AYQVb6o3bq-e0brYqw39UvcstkXftgpyks5tlJBDgaJpZM4RzV0k>
.
|
|
Apologies about the timeouts thing - I commented on the wrong issue |
|
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 This would probably fit within Note that as I wrote earlier:
|
|
OK makes sense. @mdehoon let us know what you think about testing? And note
@peterjc suggested some unit test options in our Feb comments here.
Thanks, both!
…On Wed, Apr 4, 2018 at 7:06 AM Peter Cock ***@***.***> wrote:
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 <https://github.com/mdehoon> is the module
maintainer so his thoughts are important here.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1522 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AYQVbwpnJWZ5qIITSHXLiNnSDt29FX0Aks5tlKlCgaJpZM4RzV0k>
.
|
Merge Custom Directory pull request with upstream.
|
@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 |
| self.dtd_urls.pop() | ||
| return 1 | ||
|
|
||
| def _initialize_directory(self, directory_type): |
There was a problem hiding this comment.
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.
|
I had a quibble about the internal API, but the user facing API seems nice 👍 Also, the test looks sensible. |
|
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 @cparmet Do you want to be thanked by name in the |
|
@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. |
|
I've not worked with a server which aggressively removes removes temp files, so I had not thought of that. |
|
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. |
|
@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! |
|
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. |
|
Very cool that you have it set up to distinguish offline vs online tests. Thanks for explaining, @peterjc ! |
|
Looks good to me. I would suggest to remove the empty |
Following PR 1522 biopython#1522
|
|
||
| def open_dtd_file(self, filename): | ||
| path = os.path.join(DataHandler.local_dtd_dir, filename) | ||
| self._initialize_directory() |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@mdehoon Great question. I'm refreshing my memory.
Am I correct that the directory.setter function will be called:
- when the
DataHandlerobject is created (__init__) - 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
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.rstfile and understand that AppVeyor andTravisCI will be used to confirm the Biopython unit tests and
flake8stylechecks pass with these changes.
I am happy be thanked by name in the
NEWS.rstandCONTRIB.rstfiles,and have added myself to those files as part of this pull request. (This
acknowledgement is optional. Note we list the names sorted alphabetically.)