Repository navigation
Description
As of Biopython 1.76 (released December 2019), we are dropping Python 2 support and focusing on Python 3.6 or later. This means we no longer need our (internal) Python 2 vs 3 compatibility library Bio._py3k (which is a bit like the third party library six).
This issue is suitable and specifically targeting first time contributors.
There are lots of cases:
$ grep _py3k Bio*/*.py Bio/*/*.py Bio/*/*/*.py
Bio/File.py:from Bio._py3k import basestring
Bio/MarkovModel.py: from Bio._py3k import StringIO
Bio/Seq.py:from Bio._py3k import range
Bio/Seq.py:from Bio._py3k import basestring
...
Example One
Taking the first example, from Bio._py3k import basestring we see that this is defined under Python 3 as an alias of str:
# Lots of our Python 2 code uses isinstance(x, basestring)
# which after 2to3 becomes isinstance(x, str)
basestring = str
unicode = strTherefore the fix for Bio/File.py is to remove the from Bio._py3k import basestring line, and update where basestring was used to instead use str, which in this case means editing one line:
if isinstance(handleish, basestring):with:
if isinstance(handleish, str):Example Two
Taking the second example, Bio/MarkovModel.py has from Bio._py3k import StringIO which we find on Python 3 can just be replaced with from io import StringIO
Contributing
Could any newcomer wanting to work on this first comment on this issue saying which file(s) they are going to start with (e.g. Bio/File.py, or BioSQL/) to avoid duplication of effort.
(Update: The consensus was to switch to be function or constant instead, since they generally require the same technique/fix each time)
Then read https://github.com/biopython/biopython/blob/master/CONTRIBUTING.rst and setup flake8 on your machine.
Then make a pull request making the necessary changes so that those files no longer import from Bio._py3k. Once that's done, you could pick some more to work on.
Eventually there will be nothing using Bio._py3k and that code itself can be removed, and this issue closed.
Activity
Dsujan commentedon Dec 24, 2019
I would be starting in
BioSQL/Dsujan commentedon Dec 25, 2019
@peterjc What to do incase of methods?
Example:
https://github.com/biopython/biopython/blob/biopython-176/BioSQL/Loader.py#L564
uses the
_is_init_or_longmethod declared inBio._py3kfor assertionhttps://github.com/biopython/biopython/blob/biopython-176/Bio/_py3k/__init__.py#L91
https://github.com/biopython/biopython/blob/biopython-176/Bio/_py3k/__init__.py#L176
Python 3 doesn't have longs, so should we drop the assertion and also in other similar cases where methods are used fro
_py3kwhat should be done?peterjc commentedon Dec 25, 2019
Python 3 doesn't separate int and long, just use
isinstance(parent_taxon_id, int)instead.Drop use of basestring via Bio._py3k
Dsujan commentedon Dec 26, 2019
@peterjc I am new to this code base so I am little bit confused.
In _py3k there are many Private functions for encoding/decoding. Like:
https://github.com/biopython/biopython/blob/biopython-176/Bio/_py3k/__init__.py#L64
https://github.com/biopython/biopython/blob/biopython-176/Bio/_py3k/__init__.py#L158
And used like this:
What should be done in these cases?
peterjc commentedon Dec 26, 2019
I think
_bytes_bytearray_to_stris only used here in theMysqlConnectorAdaptorclass in BioSQL. It is defined as follows for Python 3:So, moving this definition into
BioSQL/BioSeqDatabase.pymight be the best short term solution.Dsujan commentedon Dec 26, 2019
@peterjc Please review #2440
Now I would be working in
Tests/lorforlinux commentedon Dec 26, 2019
Started working on functions in
Bio/(🍄 = working on it, ✔️ = done)
peterjc commentedon Dec 26, 2019
That seems like a few, and covers quite a range. I wonder in hindsight if I should have suggested people work on a functions/constants basis rather than a module by module basis? Oh well, this way you'll all learn more about Python 2 vs Python 3.
lorforlinux commentedon Dec 26, 2019
@peterjc After I started doing it I was thinking of the same, I guess I should do it on a function basis rather than a module. Also, can you please check the code below and tell me if it is the right way of doing it.
peterjc commentedon Dec 26, 2019
@deepaklorkhatri007 For this line:
https://github.com/biopython/biopython/blob/biopython-176/Bio/Align/__init__.py#L1359
In Python 3 we'd just do this:
This explicitly discards the traceback context (this was inside a try/except with a
StopIterationerror).lorforlinux commentedon Dec 26, 2019
@peterjc thanks for clarifying the change needed, I have one more question, to check if the code is valid I should be running the test file
$ python test_align.pyand for formatting and code style, I should be using$ flake8right?peterjc commentedon Dec 26, 2019
@deepaklorkhatri007 Depending which code you are editing, there may be more than one test covering it -
run_tests.pywould take longer but cover everything (see also the testing chapter in the Tutorial).And yes, please do run
flake8locally with all the plugins listed on https://github.com/biopython/biopython/blob/master/CONTRIBUTING.rst - the git pre-commit hook helps here.Together that will catch most of the possible failures the automated testing with TravisCI and AppVeyor would pick up (and more since they don't run the online tests needing network access, like some of the BLAST tests you ought to run if you're editing
Bio.Blastfiles).22 remaining items
Directly import urllib, not via Bio._py3k (#2476)
peterjc commentedon Jan 8, 2020
The final bit to deal with is
_binary_to_string_handleused in three of our online functions to convert binary network handles into text handles. On the subject of encodings (see also #2512), and online resources:https://github.com/biopython/biopython/blob/biopython-176/Bio/_py3k/__init__.py#L111
We might want to rename and move this to
Bio/_utils.pyif there is no concise equivalent in the standard library?peterjc commentedon Jan 8, 2020
Didn't mean to close this via #2513, still have
_binary_to_string_handleto deal with.mdehoon commentedon Jan 8, 2020
In my experience, utility modules like Bio/_utils.py tend to disappoint in the end (see ParserSupport for an older example). They tend to get underused (because people are unaware of them, and anyway will first look at the standard library and find there what they need), tend to become overengineered (to accommodate all different applications), and after a while we decide to remove them again (costing time and effort in deprecation warnings etc.).
The
_binary_to_string_handlefunction is currently being used in Bio/KEGG and Bio/TogoWS (once each), and four times in Bio/ExPASy. For Bio.ExPASy at least, it's easy to just replace_binary_to_string_handlewith the code explicitly. Most of the lines in_binary_to_string_handlewould not apply there anyway. This has the added benefit of the code being easier to read for the next person looking at this code.peterjc commentedon Jan 8, 2020
I don't understand what you mean about most of the lines not applying?
The KEGG, TogoWS and ExPASy usage (and historic Entrez usage) is all the same - wrapping a binary network handle to give a unicode text handle. The key point is following the W3C recommendation quoted earlier, we determine the encoding from the HTTP headers if possible, defaulting to latin-1. The secondary functionality was to ensure the
.urlattribute was still available on the wrapped handle.If we know the encoding used by each site in advanced, then yes, the code is greatly simplified. But do we? What made you assume UTF8 for Entrez? This was something I missed in reviewing #2461 where as part of the Python 3 updates, you switched to use:
I note the Python urllib example also assumes a UTF8 encoding, rather than checking the sever's headers, but stated that the example site used that encoding:
https://docs.python.org/3/library/urllib.request.html#examples
Unless that W3C encoding advice has been superseded, I think KEGG, TogoWS, ExPASy and Entrez should all use the same helper function here.
If we could just assume UTF8, then there is less call for a shared helper function.
mdehoon commentedon Jan 9, 2020
We can. KEGG, TogoWS, ExPASy, and Entrez all report
utf-8inhandle.headers.get_content_charset(). Even if some day they report latin-1, my first instinct would be that this is a bug and the contents is actually in UTF-8.The other part of
_binary_to_string_handleis to add the url as an attribute. Here you can drop the try: except: block since the handle was opened by urlopen. Then_binary_to_string_handlebecomes trivial.peterjc commentedon Jan 9, 2020
On that basis (that all four relevant online servers declare UTF8), then I agree - do you want to make that pull request, or perhaps write it up as an issue suitable for a new contributor? Or I could implement it.
mdehoon commentedon Jan 9, 2020
See #2521
Remove Bio._py3k
peterjc commentedon Jan 9, 2020
And that job is done - thank you everyone 🎉
mdehoon commentedon Jan 10, 2020
Somebody should also go over the Tutorial and remove references to Python 2