Sunday, July 31, 2005

Bye bye, Cheetah

Well, over the last week I finally took the plunge and wrote my own templating system. It's semantically pretty similar to Cheetah, but using PHP-like <% %> syntax. It's working title is simply Blocks.

Basic features:
  • All the functionality from Cheetah is present.
  • Many of the useful tags from Django's template system are included.
  • Instead of Cheetah's compilation system Blocks uses in-memory partial compilation.
  • Each page and defined block within it each specify their own caching properties.
  • Cache parameters can include aging and dependencies on Python objects.
  • Caching works with inheritance and is fully hierarchical.
Okay, time for a simple example:
<% def blogEntry(entry) %>
<% depends entry.lastModified %>
<%? entry.contents %>
<% end def %>

<% for entry in entries %>
<div class="entry">
<span class="entry_title"><%? entry.title %></span>
<span class="entry_content"><% block blogEntry(entry) %></span>
<% end for %>

As you can see, pretty similar to Cheetah. The syntax is better in many ways - particularly as many HTML editors will happily ignore it as PHP code. The distinction between a tag and an evaluation has been made a'la Cheetah, although slightly more verbosely.

The cacheing system is the most important part of Blocks. The output of each page and 'def' block is stored by default and re-used for the next render. This can be controlled by the use of 4 tags within the block itself. No-cache speaks for itself, as does MaxAge. The age parameter for MaxAge however, is evaulated dynamically as so could change per render if necessary. The Depends tag allows cache invalidation based on either the output an evaluated expression or its lastModified property. Multiple caches may be present using the Varies tag, for instance to cache stats per user by varying on user ID.

I haven't implemented Django's filter system yet, mainly because I believe that most of it it just horrible syntax for calling a function. However, I can see a use for filtering the contents of a block - so that will be implemented pretty soon. It fits easily within the current design of Blocks.

My only disadvantage now is that I can't think of any reason to still be using Subway...

If anyone wants a copy or more info, let me know. I'll open if up if there's any interest.

Friday, July 22, 2005

London Sticks Up For Itself

Today terrorists attempted to copy their success in disrupting London, exactly two weeks after the original event. Fortunately, none of the 4 bombs seem to have detonated correctly, leaving some shattered windows but only one person injured.

Listening to the radio this evening I can help but smile at the determined attitude of the people of London today. From the DJ who just opened her show on Virgin Radio by saying "The bastards tried it again," to the many news reports of the public attempting to chase and pin down suspects running from at least two of the bomb scenes.

You have to realise, we've been through this before, and for a long, long time. The IRA terrorised London for years. It seems the more terrorists try to disrupt us, the more determined we get.

I should say I don't personally live in London, but not far from it. My brother lives and works there though, and I felt the traffic chaos two weeks ago. I also felt the wave of passion that swelled out from London over the days following the original event.

Let's hope that the positive attitude continues and the real people responsible are caught soon.

Thursday, July 21, 2005

Python Web Application Frameworks : Django and Subway

I've been looking around for a long time now for an application framework I can use to speed up my web development. At the beginning I was reimplementing all six wheels on my wagon but eventually realised that if things were to get any better, I'd have to build on what others have done.

After much searching and investigating I've found two interesting projects:

Django looks to be Python's nearest challenger to the over-hyped (IMHO) Ruby On Rails. Despite many nice features and a wonderful automatic database admin system, it also suffers many of the problems I see with Rails. The very features that make it look great in tutorials (or fancy videos) are the ones that make it infeasible for complex site development. In the beginning it's lovely to be told where to put your source files, or how to add simple authentication to your site. When things get tougher this these frameworks seems to become heavy weights getting in the way of what you need to do.

On the plus side, Django's template system includes many features that I had not seen before. Simple little things like a "cycle" tag that easily allows changing classes for alternating row colours are a great idea!

Subway is an effort to merge some powerful low-level tools (most notably CherryPy, Cheetah and SQLObject) into an easy to use high-level framework.

I had already looked at CherryPy quite a lot, and based my current development on it (a sports team organisation site). It's a great low-level framework that stays out of your way well. Coupled with my own integration of Cheetah, SQLObject and a home-grown forms library it was going well. Problem was, it wasn't very scalable and there were many things I felt I was lacking (especially in the template departement).

Subway makes the Cheetah integration as perfect as it can be. It ties in FormEncode very nicely and makes the whole lot just work.

Unfortunately, my application wants more. I've long been thinking about a template system that knows about the cachability of each part that makes up a page. Cheetah's compilation system is nice, but seem out of place after using them a while. I don't really need the timesaving that pre-compiling gives me, I need to be sure most of the webpage that will be served a thousand times isn't re-rendered at all, but just thrown out from a buffer.

So, regrettably, I've written my own template system. Fortunately to get to a working solution only took a couple of days part-time, and now I have a framework in which I can all all the caching cleverness that I need, plus all the lovely tags that I found so appealing in Django.

I'm now left with the dilema - am I using Subway at all now or should I go back to CherryPy raw?