Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 porting workflow?

Tags:

I have a small project I want to try porting to Python 3 - how do I go about this?

I have made made the code run without warnings using python2.6 -3 (mostly removing .has_key() calls), but I am not sure of the best way to use the 2to3 tool.

Use the 2to3 tool to convert this source code to 3.0 syntax. Do not manually edit the output!

Running 2to3 something.py outputs a diff, which isn't useful on it's own. Using the --write flag overwrites something.py and creates a backup.. It seems like I have to do..

2to3 something.py
python3.0 something.py
mv something.py.bak something.py
vim something.py
# repeat

..which is a bit round-a-bout - ideally I could do something like..

mv something.py py2.6_something.py # once

2to3 py2.6_something.py --write-file something.py
vim py2.6_something.py
# repeat
like image 801
dbr Avatar asked Dec 22 '08 02:12

dbr


2 Answers

Aha, you can pipe the 2to3 output to the patch command, which can write the modified file to a new file:

mv something.py py2.6_something.py
2to3 py2.6_something.py | patch -o something.py
like image 51
dbr Avatar answered Oct 05 '22 12:10

dbr


2.x should be your codebase of active development, so 2to3 should really be run in a branch or temporary directory. I'm not sure why you'd want to have the 2.x and 3.x versions lying around in the same directory. distutils has a build_2to3 script that will run 2to3 on a 3.0 install.

like image 35
Benjamin Peterson Avatar answered Oct 05 '22 12:10

Benjamin Peterson