Archive for April 2010

Web Applications in Python 3

Everyone knows that there aren’t any decent web stacks in Python 3, right? Nobody seems interested in porting Django or Turbogears or Pylons or Plone or Zope to the new, improved much more fun to write language.

Almost true. There are a few less popular frameworks for Python 3, including my own half-finished psyclone: http://archlinux.me/dusty/2010/01/17/python-3-web-framework/

But there aren’t any full stack frameworks. But it’s not too hard to hack one together using available tools.

In my mind, a full stack web framework requires several features:

  • The core server
  • A database engine
  • A templating languages

In addition, it’d be nice if it supported sessions out of the box and had a built-in auth framework. Basically, the more like Django it is, the better.

The good news is, we can hack one together with popular Python 3 ready tools including:

CherryPy provides a working web application engine. SQLAlchemy, which supports sqlite and posgres under Python 3 provides us an extremely flexible and robust ORM. Jinja is an advanced Django-like templating system. I implemented a very basic blog (sans authentication) in these tools in a matter of an hour or so, and it wasn’t any worse to work with than Turbogears. This isn’t a big surprise, since Turbogears is built on CherryPy and SQLAlchemy as well. We don’t have access to the extensive library support Turbogears has to offer, but it is now accurate to say that Python 3 supports a (nearly) full stack web framework.

You’ll need the svn version of CherryPy, and the newly released 0.6 version of SQLAlchemy. Jinja 2 cooperates well.

I don’t find it as pleasant as working with Django (I’m actually not a huge fan of Alchemy’s syntax), but it’s certainly a working solution. It wouldn’t overly much library support to turn it into a really nice system.

Image Manipulation in Python 3

Enough libraries have been ported to Python 3 to finally make it seriously possible to write real world code in this modern Python interpreter. Sure, we don’t have django or really any decent web framework yet (CherryPy runs, but it’s not a full web stack), and database support is limited (SQLAlchemy supports postgres and sqlite3), but for the most part, if you need to do something Python 3, you can.

The major exception I’ve discovered is Image manipulation. The Python Imaging Library has not been ported to Python 3, and there is no indication when it will be. The latest version of PIL, 1.1.7 was released in late 2009, with an indication that it would be “made available for Python 3,” but no estimate as to timeline. There are no mailing list posts answering an increasingly popular question “when will PIL be available for python 3?” I found no source repositories that indicate that it has been started. There is a patch floating around that can supposedly be applied to PIL 1.1.6 to make it Python 3 compatible, but it didn’t work for me.

I tried doing a port of PIL 1.1.7 myself, but was unable to find documentation for the modifications to the C extension API in Python 3. My C is pretty rusty, and my schedule is way too full for the next two months, so I gave up on the task. Because of the lack of support from the official PIL developers (I hold nothing against them; we’re all busy in the open source world, and can only contribute what we have time and interest to contribute), I’m hoping this post will motivate someone to attempt a port of PIL, making Python 3 that much more attractive.

If you came to this post looking for some kind of Python 3 image manipulation library and were disappointed, don’t be! The Pygame image module allows us to load a few image formats, and we can manipulate the resulting surfaces in a variety of ways. It’s lower level and not as comprehensive as the Python Imaging Library, but it is one useful alternative if you need to do image manipulation in Python 3.

How (not) to submit a W8-BEN: Canadian retailers dealing with US income

As a Canadian retailer selling books on Amazon’s Createspace, (an American company), I suffer a 30% tax by the US government. (Aside: this is not a good way to promote global trade in a country that is nearer to economic collapse than ever before). Canadian residents are supposed to have this entire tax waived. In order to have the tax waived, I need to supply a W8-BEN form to Createspace. This sounds quite trivial, but the steps involved are time consuming and error prone. If you’re trying to figure out how to do this, here’s what you need to do:

  1. Get a Certified Copy of your Canadian passport. If you’re lucky enough to live near a city that has an official Passport Canada Office go there and have them certify it. Service Canada and Canada Post cannot perform this service. If, like many Canadians, you are not so lucky, you’ll have to mail it to this address:
    Passport Canada
    Foreign Affairs and International Trade Canada
    Gatineau, Canada K1A 0G3

    with a letter requesting the certified copy service. It will take up to four weeks for your passport to be returned, so don’t plan any international travel. You can also get the passport certified at a US embassy or by a US notary (the IRS won’t trust a Canadian Notary).

  2. Fill out a W-7 form. This is fairly straightforward. The IRS has instructions but they don’t make much sense. Mail the form to this address, with the certified copy of your passport:

    Internal Revenue Service
    Austin Service Center
    ITIN Operation
    P.O. Box 149342
    Austin, TX 78714-9342

  3. Wait for the IRS to get back to you. In my case they didn’t. They did get the form though, because I started getting official IRS e-mail spam. They just never gave me a TIN or responded in any physical fashion whatsoever. So I had to start the process over, continuing to lose 30% of my income every month, and going without my passport for another month.
  4. Fill out the W8-BEN form. The IRS has instructions for this too. You’ll have to include your TIN in this form.
  5. Send the form to whichever company is withholding taxes from you for the US government.

After the IRS lost or destroyed or rejected my application, without telling me, I had no way to contact them to find out what had happened. There is no e-mail address on their site, no web submit form, no phone number for international service. They don’t even seem to encourage using their mailing address.

Even worse, for me, Zazzle has now also started withholding this 30%, and they refuse to send my funds AT ALL until I submit the W8-BEN. A substantial portion of my income is now being held hostage or stolen by the US government, and they are cleverly refusing to give me the information I need to free it.

Taxes are a difficult matter, and we all complain about them. Dealing with the IRS has given me a huge appreciation for how intelligently the Canada Revenue Agency has streamlined their processes in recent years. Interacting with the IRS is like using smoke signals. Interacting with the CRA is like using a telegraph. Hopefully both countries will be able to handle the simplicity of the Internet someday soon.