Category Archives: Python

In 2008 I started learning Python (with *a lot* of help from David Beazley, thank you good sir!). I can’t get enough. Here I discuss what I’m learning, and eventually, how it applies to the web.

Putting Python on the Web

The more I use Python, the more I think about using it online. Often this amounts to creating simple tools to help me get things done quicker, like embed a simple script in a web page, process a form, etc. without having to install an entire framework. Besides, it’s handy having the basics of Python web programming under your belt before trying to tame the framework beast.

I wasn’t having much look Googling anything, but then I thought, what’s the most basic program on the planet? That would the be typical hello world! program, so I Googled something like python web hello world, and came up with this excellent Python web programming tutorial1 that outlines the various ways to get your Python scripts generating web pages. Check it out.

1 Note the juicy anchor text here. Not only did I provide a link to this website, which will give it another inbound link from a non-spammy site to help boost its rankings, but it will also help Google figure out that this is a Python web programming tutorial because that text is actually within the link’s anchor text. If more folks did this sort of thing, instead of the typical “click here” link text (isn’t “click” implied by it being a link? This ain’t the 90′s anymore…), it’d be easier to find stuff online. To learn more about the power of anchor text, check out Google bombs.

Getting MySQL-python Running on Leopard

As I pursue my python adventures, I of course wanted to learn how to do one of the things I do most – pull data out of MySQL. It seems the simplest way to start doing that is Python is to use MySQL-Python. So, after a downloading the package from SourceForge, and following the build instructions (tip: keep all this junk in a “src” folder in your user directory), I wound up with this loveliness:

error:
command ‘gcc’ failed with exit status 1

How annoying – I recall seeing this error when installing the Django database bindings (more on that some other time), and it’s uber stupid as gcc, a compiler, is indeed installed on OS X (as running a “gcc -V” in the terminal will tell you). Turns out, a little hacking is needed to get this running, and it’s pretty easy, thanks to Red Elephants’ excellent MySQL-Python and Apple OSX 10.5 (Leopard) article. If that still doesn’t work for you, try this:

  1. Open the site.cfg file
  2. Uncomment line 13, and change it to read:
    mysql_config = /usr/local/mysql/bin/mysql_config
  3. Save it, delete the build directory (rm -r build)
  4. Run: python setup.py clean
  5. Try again, starting from Step 5 of the Red Elephant article

Hope that does it for you. Once you have that up and running, you can dive in with this old, but handy, devshed.com article – MySQL Connectivity With Python (actually, this page seems to have the same info in a more readable format). There is one difference I’ve noted so far – to get the last insert id, you create a “cursor” object (as show in the article), and run (assuming you’re working at the interactive prompt):

>> cursor.lastrowid

If you see an “L” – which just means “long number,” (maybe because it’s an “int” field?) you can get rid of it with a print statement:

>> print cursor.lastrowid

And don’t forget the handy “dir” function, which tell you everything an object can do:

>> dir(cursor)

Also note that MySQL-Python has some handy support for transactions and rollbacks (page 7 of the devshed article). Hope this helps alleviate some headaches ;)

Get TextMate to Recognize More Python Keywords

Syntax highlighting is definitely your friend when you’re coding, and while I love TextMate, it’s support for some languages is still a bit lacking.

For reference, you can always get my current customized theme(s) here. You can browse more themes at the Textmate Wiki.

Fortunately, it’s rather easy to customize the language packs. Here’s what I did to expand TextMate’s support for some common Python keywords:

  1. In TextMate, select Bundles » Bundle Editor » Edit Languages
  2. Scroll down, tick the arrow next to Python, and then select Python again (see bottom picture)
  3. Look for the following line:
           {   comment = "keywords that haven't fit into other groups (yet).";
    	   match = '\b(as|assert|del|exec|print)\b';
    	   captures = { 1 = { name = 'keyword.other.python'; }; };
           },
  4. After print you can add whatever you want, separated by the “pipe” sign – I added: |str|int|set|repr|float|ord|chr – you can add as many as you like, and I’ll likely add more down the road. Just make sure you keep the closing parenthesis in there.
  5. Click the “Test” button at the bottom to confirm.

That’s it – now when you use one of these functions, you’ll get some syntax coloring.Python Bundle

Update:

To get the ‘global’ keyword recognized, add the following to your theme:

TM Python Global

I’ve taken a similar approach with the “support” scope selector to get syntax highlighting on __name__ type objects.

Basic File Parsing with PHP and Python

I’m currently taking a masters class on dynamic languages with DaBeaz, and our first assignment was to parse a basic text file containing a stock portfolio, like so (here’s a simple text file w/ all of them):

YHOO 50 19.25
AAPL 100 143.41
SCOX 500 4.21...

The values are symbol, shares, and price. We had to produce a nicely formatted, text-based table, and calculate the total value. Not so bad, but we had to do it in 9 different languages. I won’t talk about all of them, rather, just the PHP and Python versions. I’ve been working in PHP for quite awhile now, and and learning quite a bit about Python as DaBeaz wrote a book about it.

Simplest is Best

So, there’s all kinds of ways to perform the task at hand. Since you know there are 3 values on each line, you could “explode” each line into an array, based on the space between each value. You could toss in a regular expression or two. But let’s not get too clever, and try to write the most efficient code possible with the least overhead – what if our portfolio contains thousands of lines?

Here’s the Python solution:

total = 0
print "%10s %10s %10s " % ('Names', 'Shares', 'Price')
print "---------- " * 3;
for line in open('portfolio.txt', 'r'):
    vals = line.split()
    symbol = vals[0]
    shares = int(vals[1])
    price = float(vals[2])
    print "%10s %10s %10.2f" % (symbol, shares, price)
    total += shares * price
print "\nTotal value : $%0.2f" % total

One thing that’s really winning me over to Python is the syntax – it’s compact and easy to read. Also note that in Python, we can’t create “on the fly” variables via expressions, which is why we had to declare the “total” variable on the first line, whereas PHP is very loosie-goosie:


Also note that with PHP, we don’t have to convert strings to numbers and floats, it just sort of “magically” knows what we want to do by looking at the context (in this case, perform a math calculation). While I found Python’s “strictness” a bit annoying at first, I’ve come to appreciate it’s explicit behavior as it’s less error-prone. (And I say “strictness” because compared to compiled languages like Java, this isn’t strict at all.) But note PHP’s list function – it’s highly useful, and can be used to iterate database queries as well.

Printf (PHP) and % (Python) are Your Friends

Seeing that all of my work is on the web, I never really used these puppies before. But they sure are useful for formatting text and numbers. The syntax can be a little intimidating, but not when you understand it. Let’s look at this statement in PHP:

printf("%10s %10d %10.2f\n", $name, $shares, $price);

Think of “printf” like printing w/ formatting. All this is saying is to take the stuff to the right of the quotes (our variables), and format it like this:

%10s = print a string, right-aligned, within a 10 space field
%10d = print a digit, right-aligned, within a 10 space field
%10.2f = print a floating point number, right-aligned, within a 10 space field, and round it to 2 decimal points.

(And the “\n” is just a line-break command.)

In Python, you just use a regular print statement, with a “%” between the formatted output (always in quotes, as with PHP), and the variables to sub in:

 print "%10s %10s %10.2f" % (symbol, shares, price)

(In Python, a new line is automatically inserted after each print statement.)

And there you have it. In the next article, we’ll take this to the next level, and parse real stock data from a (huge) CSV file – whooot!