mailer version 0.5 released

I've released version 0.5 of my mailer python module for sending emails. Thanks to a patch from Douglas Mayle, this version makes it possible to send HTML emails with attachments (previous versions only let you do one or the other). Project homepage pypi page

Converting kanji numbers to integers with Python

A question on StackOverflow about converting kanji numbers (e.g. "五十五") into integers in C++ got me interested in solving this using Python. The result is my kanjinums module, with a function kanji2num that will convert a string containing a kanji num to a Python integer. Download the source distribution (kanjinums-0.1.zip) Download the Windows installer (kanjinums-0.1.win32.exe) […]

Conditional “tee” with Python

This post describes the conditional tee ("ctee") module I wrote to split a sequence into two generators, according to a filter function. The problem David Beazley has a great article about generator pipelining using Python. This is a technique for handling (potentially very large) streams of data in a flexible yet efficient way. As an […]

Coming from C++/Java to Python should bend your mind

Coming from C++ or Java to Python should bend your mind. If it doesn't, then you haven't learned Python yet — you're just writing C++ or Java in Python. If you only knew a statically typed and compiled language like C++ or Java before, and learning Python hasn't changed the way you think about programming, […]

An easy way to write XML in Python

David Mertz's gnosis utilities include the module gnosis.xml.objectify, which makes parsing XML in python as simple as could be. from gnosis.xml import objectify xmltext = """<?xml version="1.0" encoding="UTF-8"?> <root><first canned="true" yummy="false">spam</first><second>egg</second></root>""" inst = objectify.make_instance(xmltext) print "first:", inst.first.PCDATA print "  canned:", inst.first.canned print "  yummy:", inst.first.yummy print "second:", inst.second.PCDATA Output: first: spam   canned: true   […]

Iterating over a window in Python

This weekend, I had a fairly common problem: given a sequence of elements and an element in that sequence, I needed to get the next element; if the element was the last in the sequence, then I needed the first element. My first stab at a solution was pretty straightforward: def item_after(elements, item):     […]

Excerpt from Expert Python Programming: Writing a Package in Python

I'm currently reading Expert Python Programming by Tarek Ziadé. I plan on writing a review once I'm done, but in the meantime the publishers over at Packt have kindly sent me an excerpt from the book to publish here. Download "Writing a Package in Python" (PDF)

Notes for using Unicode with Python 2.x

Python is very Unicode friendly, but there are still a few quirks that people new to the language (or not so new!) need to assimilate in order to use Unicode effectively. To avoid going over old ground, for a primer please see this excellent article on using Unicode with Python. Here, I want to talk […]

Generic adapter class in Python

The adapter pattern is often used in programming when you need to adapt one interface to another. Here's a simple generic adapter class that can adapt just about any interface to just about any other. class Adapter(object):     """     Adapts an object by replacing methods.     Usage:     dog = […]

Don’t overuse classes in Python

Unlike some mainstream languages like Java, you don't have to package everything into a class in Python. A class is a good tool when you want to package up state and behavior, but when all you've got is a bundle of related functionality, the module is the natural unit of packaging in Python. In my […]