Skip to content

Converting Python Code for Compatibility

DebRez edited this page Jun 29, 2016 · 3 revisions

IN DEVELOPMENT - NOT READY FOR USE

Python-Future is a compatibility layer between Python 2.6+ and Python 3.3+. Code written in python 2.6+ can be "futurized" to make it compatible with python 3.3+. Code written in python 3.3+ can be "pasteurized" to make it compatible with python 2.6+. Reasons for using python-future are described here: python-future.org/faq.html.

Combining python virtual environments with code produced using Python-Future will help resolve compatibility issues.

To convert Python 2.6+ code to Python 2/3 code you need to set up the appropriate python virtual environment, as described in How to Set up Python Virtual Environments. Use the pyenv.txt provided with MTT as your resource (-r) file. The version of Python you use for the conversion does not matter. (NEED TO CONFIRM THIS)

Conversion to Python 2/3 compatible code

Scripts written in Python 2 (2.6+) will need to be updated as follows:

$ futurize -0 -w filename.py

Scripts written in Python 3 will need to be updated like this:

$ pasteurize -w filename.py

The -w option actually does the code conversion. If you want to see a list of changes that will be made but not actually change your file leave the -w out. Also, a copy of the original file is saved as .bak so you can easily retrieve your files. Whole directories can be converted with /*.py as the filename as well.

Further changes may need to be done by hand. See Writing Python 2-3 Compatible Code for details.

Test Your Code

Confirm that the file was not broken by the conversion. Need to set up a testing system. Possibly pytest using unittests in the code that could be run before a pull.