Version 0.2 of mailer module released

I updated my mailer module (blogged about here) to version 0.2, and also uploaded it to pyPI. Improvements in this version: Default arguments in Message.__init__() method Support for non-ascii charsets (in body and subject) Support for Python 2.4 With the support for non-ascii encodings, you can now do this: from mailer import Mailer from mailer […]

The Adapter Pattern in Python

Interface mismatches are one of the banes of code reuse. You want to integrate component A and component B into your program, but component A expects a method called half_empty() while component B only has a method called half_full(). The Gang of Four (GoF) Adapter pattern is used to convert the interface of an object […]

A module to send email simply in Python

Update: I've released version 0.3 of the mailer module. See the mailer home page for details and the latest version. The email and smtplib modules in Python are very powerful, but they're also a bit complex when you just want to send an email. I wrote the mailer module as a front end to these […]

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 = […]