Posts tagged: PYTHON

Pastebin update: Pygments

This past spring I posted about a simple pastebin app I wrote using Django. This week I updated it to use the excellent Pygments syntax-coloring library (formerly known as Pykleur).

Pygments has support for a healthy number of languages/syntaxes, offers a great deal of flexibility, ships with several different color schemes, and can produce output in HTML/CSS, LaTEX, or ANSI terminal colors. I created a “pcat” alias to take advantage of that last one when working in the shell.

Python MySQL junk pointer not making sense

(There’s got to be a name for blog posts that you make just so you’ll be able to remember something later – or so somebody who is banging their head against a problem you just solved can find your solution via Google magic. This is one of those posts.)

A while back I started getting bizarre errors when using Python with MySQL. They looked like this:

python in free(): warning: junk pointer, too high to make sense

It didn’t make sense to me either! Tech support for my VPS and copious Google searching didn’t turn up anything I could use.

Handling legacy URLs with Django

One of the great things about Django is its simple and flexible URL handling. If your Django work, like mine, includes converting existing sites, you’ll probably be doing some URL cleanup along the way. Django’s “generic views” system includes a view called “redirect_to” that handles cases like this. A rule might look like this:

urlpatterns = patterns('django.views.generic.simple',
    ('^foo/oldsite/BadOldUrl33247.blech$', 'redirect_to', {'url': '/bar/nice-new-url/'}),
    )

But because the URL pattern building happens in Python, if you have many of these you can do better than filling your urls.py with variants of that line. Here’s the root urlconf for one of my sites:

The "path" module

Somewhat belatedly I’ve started using Jason Orendorff’s path module for Python. It’s great. Here’s a comparison with the stock os.path facilities, grabbed from Jason’s site:

# with os.path.walk
def delete_backups(arg, dirname, names):
    for name in names:
        if name.endswith('~'):
            os.remove(os.path.join(dirname, name))
os.path.walk(os.environ['HOME'], delete_backups, None)

# with path
d = path(os.environ['HOME'])
for f in d.walkfiles('*~'):
    f.remove()

The second snippet is not just shorter, it’s easier to read and easier to write. I’m writing some code to recursively process a tree of short text files (Blosxom entries), and the path module is a godsend. If you’re curious, see the description and examples on Jason’s site; he also posted some interesting comments on Ian Bicking’s blog about the design of the module.

Johnny can too code

David Brin, in a piece at Salon.com entitled “Why Johnny Can’t Code”, complains:

almost none of the millions of personal computers in America offers a line-programming language simple enough for kids to pick up fast

Maybe Apple’s marketshare is so small that they equal “almost none,” but all OS X Macs come with Python and Ruby among other options. But wait, Brin seems to have heard of some of these newfangled scripting languages: