-
Notifications
You must be signed in to change notification settings - Fork 4
Description
At some point we should add a developers guide, and this will need to address python 2/3 compatibility. For now, list the main points here and below.
Main points
Try to write in idiomatic python3 with python2 compatibility limited (where possible) to module imports. As far as python2 goes, we only care about 2.7 (which is the only modern python2, and makes things very much easer. Some common issues are listed below.
In python3 we print with a function not a keyword so:
print "Hi!"
becomes:
print("Hi!")
To make this run under python2 we need to from __future__ import print_function at the top of any module where we print.
Strings and bytes
Python3 distinguishes between sequences of Unicode characters (strings) and sequences of bytes (bytes). Dive into python3 provides a good overview. In Python2 strings are ascii and lists of bytes. This means we need to take care about the mode when opening files, and we ought to define string literals as unicode (u"a unicode string") .
The default encoding of python source files is also different (unicode for python3, ascii for python2). To
make unicode OK everywhere, add # -*- coding: utf-8 -*- as the first (or second line, after #!/usr/bin/env python) of all source files.
Testing and developing
We have Travis continuous integration set up to run our tests in a python2 and python3 environment. Try to avoid merging anything that does not pass all tests on both. This means we need to submit changes as pull requests (and get somebody else to review and merge these).
If using anaconda, it is possible to have python2 and python3 environments set up for local development. See https://github.com/andreww/python3_friends/blob/master/introToPython3.md#2-installation for a basic introduction.