Tuesday, November 27, 2007

A failed experiment

On a whim, I decided to try a fun experiment: have Crunchy run correctly when invoked either from Python 2.4 or Python 2.5 ... or from Python 3.0! I thought that if I could make it to work, it would be a great tool to go through the 3.0 docs and tutorial, and finding out if there were any mistakes. (I was planning to do this, hoping to help the developing team in my own limited way.)

In the end, I had to give up, as Crunchy uses third-party modules (ElementTree, HTMLTreeBuilder, ElementSoup, BeautifulSoup, etc.) that I could not make compatible with both Python 2.x and 3.0 without essentially rewriting them in two separate modules each. Actually, this was already done for ElementTree (included in the stdlib under a different name) but not for the others.

The main stumbling block was string handling/encoding... This should not be a surprise to anyone who has followed the Py3k development.

Still, I've learned a fair bit from that experiment. One thing that I sort of knew already related to using print statements for debugging purpose. I often sprinkle my code with "if DEBUG: print ...", having multiple print statements appearing. Often I find that I want to have a few debug flags in the same file and think to myself

Shouldn't I replace these print statements by a debug() function with a variable setting the debug level ...

If I would have done something like this, I could have had just a few print statements located in a debug.py module. As it was, with the change in Py3k for print becoming a function, I had to do a lot of changes by hand. Yes, it might have been possible to use the automated 2to3 tool ... but I wanted to maintain compatibility with Python 2.x and, more importantly, get a feel for what was involved in making the code Py3k compatible.

If or when BeautifulSoup, ElementSoup and HTMLBuilder become Py3k compatible, I'll have to give it a try again...


Kent Johnson said...

Instead of a debug() function you might want to use the logging module.

glyph said...

Did you read Guido's "Compatibility and Transition" blog post?


You're not supposed to write source code that can work on python 2.5 and 3.0: it isn't supported and it isn't recommended.

André said...

@glyph: yes, I had read the blog, I know it is not supported nor recommended. However, programming for me is a hobby, something I do just for the shear pleasure of it. This was a fun (if a bit tedious at times) thing to try.

I'm pretty sure that, if I had access to Py3k versions of all the 3rd party modules I use, I could have succeeded in making Crunchy cross-compatible in just one (long) evening. There seemed to be only one of my own modules that needed two different versions to make this possible. This would be a lot easier (for me) to maintain than having two completely different versions of Crunchy.