Get it done
This is the first part of a tutorial on how to write well structured programs using python. It is intended for novice programmers who have some familiarity with python, and have perhaps written a couple of small scripts on their own. I will not be explaining what python language constructs do. The official documentation does a fine job of that. I will, however, be trying to explain when and why you would use one construct over another.
If you haven't yet been through the python tutorial in the official documentation, go check it out. We'll still be here when you get back. If you find yourself stuck on some language constructs, the reference material should answer your questions. If you're still stuck after that, post a question, and I'll try to help you out.
Over the course of the series, we will take a simple one-off script, and expand on it, making it into a robust and reusable utility library. Each step of the way, we will introduce a new tenet of good programming practice, implement it in our sample project, and explain why it is important.
Something is bothering you. Most programming projects start this way. Maybe you like that your music gets automatically tagged when you rip a CD, but you hate that words like "of" and "in" and "the" get capitalized half the time, and you want to clean it up, but you have 30GB of music, and it would take you a week to do it. Maybe you have a folder with several thousand documents in it, and you want to organize them into sub-folders by date, or by author, or by size.
Whatever your project, programming is the art of solving problems. Real problems. Your problems. But it's more than that, too. It's also the art of being able to reuse your work when you need it for something different later, and it's the art of communicating with other programmers, and even with your future self, so that people looking at your code can understand what it does, and why, and can understand how to work with it.
Let's take a simple project. You want to convert a weight given in pounds to kilograms. So let's create a program called converter.py. A little googling tells us that there are about 0.4536 kg in 1 lb, so we'll use that value in our code below.
#!/usr/bin/env python # converter.py input = raw_input() x = float(input) y = x * 0.4536 print y
This does everything we need. It gets input from the user. That input comes in as a text string, so we convert it to a floating point number. Then we multiply it by the number of kilograms in a pound, and print out the result.
$ python converter.py 200 90.9090909091 $ _
But we have to run the program over and over again to calculate multiple weights. That's not much better than just using a calculator. So let's have the program handle multiple calculations.
while 1: input = raw_input() x = float(input) y = x / 2.2 print y
Now you can run the program once, and enter as many weights as you like. As soon as you enter something that isn't a number, the program exits.
$ python converter.py 200 90.9090909091 132 60.0 q Traceback (most recent call last): ... ValueError: invalid literal for float(): q $ _
The program exited with an error, but it got the job done, which is the first thing we need it to do. Now we can clean it up. We'll catch the NameError exception, and exit gracefully.
import sys while 1: input = raw_input() try: x = float(input) except ValueError: sys.exit() y = x / 2.2 print y
Now it exits silently, as you'd expect from a normal shell script.
$ python converter.py 132 60.0 q $ _
So far, we've taken a simple idea, and implemented it in python code. What we have works, it solves our problem, and it saves us effort. However, the interface isn't very user-friendly, and the code is fairly ugly. Over the course of this series we'll use our little converter as example code to talk about communicating with other developers, users, and ourselves with the code we write, so that it is easier to use and easier to read and understand. We'll also discuss code modularity, which makes it possible to use the same code for multiple purposes without copying the same code from one place to another. Join us again in a week or so, for Stylish Programming in Python, Part 1: Say What You Mean, where we talk about variable names, documentation, and user interface.