Student Login

Painlessly porting programs to Python

One of you charming, shrewd, and insightful readers asked:

"I was wondering if you could give me some advice on porting large programs written in other languages to Python. The problem is I don't know where to start. Do you have any suggestions?"

This is a great question.

Where I would start: with the tests.

I'd choose a specific subssytem I want to convert first. (Since we're talking about a large application here.) And then I'd look at its automated tests already in place, in the original language.

In particular: look for "end-to-end" tests.

These are tests that cover a specific flow of activity in the application, from start to finish. If there aren't any, find tests that seem to cover a lot of activity at a high level.

Once you find a good one... translate that TEST into Python, first.

Since unit test code tends to be simpler than regular code, this will usually be pretty straightforward. And if the original test uses data files, or anything like that, you can just copy those into your Python test directly.

This is your anchor. Your primary guiding light, as you start writing Python code to make that high-level test pass.

You have choices for how to do that. A smart way is to look at the original application, in its native, non-Python language, and look at the classes and architectural patterns it uses.

Use that as a guide as you start implementing it in Python. Even at the level of translating individual classes.

Porting a large program to a new language is never a simple task. But this approach is the best I've found.

And it underscores the importance of automated testing.

This is, after all, one of the key skills separating average developers from the best engineers on the planet. And with enough effort, anyone can learn.

For Teams Bootcamp