Updating a Program from Python 2 to Python 3

Thu 09 March 2017

I've been working with Python 2 and Python 3 codebases for a couple years now. I write new code in Python 3 and maintain past work written in Python 2, but I've never ported a codebase from two to three.

This topic is interesting to me because the 2/3 split is a big deal in every Python-related thread on Hacker News, but despite Python being my go-to language of seven years the split hasn't ever bothered me much.

To explore the problem I decided to port a small program I wrote five years ago. PyLife is an implementation of Conway's Game of Life that uses the terminal as its display. I wrote the program to practice a few things:

  • Writing Python in a functional style. The program generates each generation as a function of the last.
  • Using the itertools library to practice using iterators.
  • Using the curses library to make text based interfaces.

You can view the most recent, Python 2/3 compatible, code at GitHub

The Porting Process

I started porting by running the program with the python3 interpreter to see what errors I got. The first one was an ImportError involving itertools. I wasn't familiar with how the library had been changed and reorganized in Python 3 so I Googled for an overview. I found that Python 3's filter function and related functions use iterators under the hood, making itertool's filter function redundant. I removed the bad import and replaced all calls to itertools.ifilter with calls to filter.

The next error was similar. Python 2 provides the range and xrange functions to create sequences of integers. range returns a list while xrange returns a faster and more memory-efficient generator. In Python 3 range behaves like xrange does in Python 2 and in the inefficient version of the function is removed. Updating my program required a minute of find-and-replacing.

And with that my program is Python 2/3 compatible! The program no longer uses generator backed ranges when run as Python 2. I could work around this, but the program performs fine so I don't think it's worth the trouble.

Conclusions

Small programs are trivial to port. Larger programs that depend on libraries beyond the standard library could be harder. Programs that explicitly handle text encoding are almost certainly harder to port.


Previous Posts

Sat 01 October 2016

rsync and Android
Here's what you need to transfer files to an Android phone over the network using rsync. sshd for Android: SSHelper How to use rsync on a nonstandard port: rsync -rv -e "ssh -p 2222" ~/Music 192.168.0.100:/sdcard/Music I'm not using the compression flag (-z ...

Thu 18 August 2016

Iterative Tree Traversal, and Simulating Recursion
Students sometimes ask, "what's the point of recursion?" My stock answer is that some problems, like tree traversal, are much easier to solve recursively.1 But what does an iterative tree traversal look like? Let's find out! This is the tree-node I'll be working with (in Python ...

Fri 28 March 2014

Color Cycling
Color cycling is a nifty technique from the days of yore for creating animations. The key idea of color cycling is to modify the color palette an image uses rather than the image itself. Since each pixel on the screen is refers to some color in the palette, it's ...

Thu 27 February 2014

Bitmap Graphics on the Nintendo DS - Part 2: Mode 3
This is the second post in a two post series on drawing and animating bitmap graphics on the Nintendo DS. The first part is here. The biggest problem with frame buffer mode is that it could only draw on the DS's primary display. To solve that issue we'll ...

Sat 01 February 2014

Bitmap Graphics on the Nintendo DS - Part 1: Framebuffer mode
This is the first post in a two part series on drawing and animating bitmap graphics on the Nintendo DS. The second part is here. The simplest way to draw graphics on the DS is frame buffer mode. Frame buffer most is the closest analogue to the Game Boy Advance ...

Fri 04 October 2013

Meandering Momentum AI
I wrote an example of basic NPC movement AI for a class I TA. The NPCs chase or flee from the player based on their race (Giant or Pygmy) and their proximity to the player. When they aren't chasing or fleeing they meander about with some sense of purpose ...

Wed 20 February 2013

Linear Recurrence Generator
A sequence whose elements are calculated from earlier elements in the series is a linear recurrence. The Fibonacci sequence (0, 1, 1, 2, 3, 5, …) is an example: With the equation and the first two numbers in the sequence, you can calculate the rest. There are more complicated sequences that ...

Sun 27 January 2013

TourStarter 0.5
My most recent project, TourStarter is feature complete! The Adventure Cycling Route Network is a system of scenic and bicycle friendly routes that criss-cross North America. Their maps are great for planning a tour, but how do you get on a route in the first place? That's where TourStarter ...

Wed 16 May 2012

Now Featuring Syndication
This site now has an atom feed. Click the nifty icon to subscribe!

Thu 10 May 2012

GitHub Drinkup - Atlanta 5/9/2012
GitHub hosted a drinkup in midtown last night and it was pretty great. Here's the executive summary: Ruby seemed like the most popular language among attendees. The combined forces of the Atlanta Ruby Users Group (who had a meeting a couple blocks down the street just prior to the ...

Mon 10 October 2011

Nerd Herder
Nerd Herder is an arcade style game for the Gameboy Advance. Your goal as head TA is to guide your students to their academic destinies. You do this by chasing the best and brightest students to the dorm to study, while driving the less capable to the M-Train¹. At your ...

Sun 19 June 2011

Music Histogram
This graph shows the count of songs in my music library by year. The graph was produced from a CSV generated by a Python script that pulls data from iTunes using Apple’s Scripting Bridge technology. Eventually I want to go back and redo the script so that it draws ...

Sun 10 October 2010

Hero's Quest
Hero’s Quest is a proof of concept RPG for the Game Boy Advance. It features animated sprites, scrolling backgrounds, music, sound effects, menus, and other functionality. The game is implemented in C. To play the game download the ROM and open it in with a Game Boy Advance emulator ...

Sat 01 May 2010

Music Info Avatar Generator
This script reads information about my most recently played song from iTunes, draws it on an image, and uploads it so it can be used online. The script is written in AppleScript and uses iMagine Photo, a lightweight and scriptable image editor, to draw the avatar. The script runs invisibly ...