06.17.08
MVC dispatcher in python
As part of the exciting task of refactoring the ticket-text web codebase we decided to go the mod_python/cheetah templates route (previously we had a JSP based solution). So far I’m liking it, if for no other reason than it makes the code easy to hold in your head all at once. One of the first things I had to write was a dispatcher, i.e. a module that reads the incoming URI and decides which module to load and which function to call in that module to handle the request. Given a URI like this:
http://www.mydomain.com/foo/bar/baz/bip
the dispatcher will load the module foo and call the function bar with arguments baz and bip. In the ticket-text case, there are a few exceptions designed to make the resulting URLs even shorter and more intuitive for the end user. Anyway, dispatcher.py comes to 65 lines, of which 11 are whitespace and 18 are comments. That’s 36 lines of code. Having worked with the Zend Framework (PHP) and MVC.NET (C#) in the past, I am amazed at how simple and powerful the pythonic approach is. How did I ever manage before?