[Biopython-dev] Python 2 and 3 migration thoughts

Peter Cock p.j.a.cock at googlemail.com
Mon Oct 14 11:00:46 EDT 2013


Hello all,

Despite a nasty cold, I've made further progress over the
weekend. Switching to assuming Python 3 style dictionaries
is a single biggest step forward - and as long as we have
good test coverage I think this is low risk. I think a dual
code base without needing 2to3 may be attainable for
the next Biopython release.

However, before that, I'd like to take a moment to discuss
changing imports, e.g. Doc/examples/getgene.py

Do people prefer something explicit like this,

try:
    import gdbm # Python 2
except ImportError:
    from dbm import gnu as gdbm # Python 3

Or something via a helper library (e.g. our Bio._py3k or
a bundled copy of the six library):

from six import dbm_gnu as gdbm

That's a rare example, something far more common is
StringIO, which also crops up in our doctests. e.g.

Python 2 only:

>>> from StringIO import StringIO

Both 2 and 3:

>>> try:
...     from StringIO import StringIO # Python 2
... except ImportError:
...     from io import StringIO # Python 3
...

Both via Bio._py3k, not ideal for a doctest as it is a
private module intended as an implementation detail:

>>> from Bio._py3k import StringIO

Both via six, not ideal if we're bundling it as Bio._six
or similar:

>>> from six import StringIO

Or, for a more common and more complex example, have a
look at how urllib has changed under Python 3. See some
of the commits here:

https://github.com/peterjc/biopython/tree/six

For docstrings, I actually prefer the explicit commented
version with the try/except. For the main code, using a
central helper like Bio._py3k or a bundled copy of six
makes sense from a code management perspective -
it would ensure consistency (and be easy to remove
once we drop Python 2 support).

Any thoughts?

Thanks,

Peter


More information about the Biopython-dev mailing list