Rails and Why Are Hacks & "Pythonic" Code Mutually Exclusive?
posted by brian at 06:54 AM
If you've just been admiring Ruby from a distance like me, check out this beautiful presentation given by the creator of Rails at the Snakes and Rubies event last weekend. It is simply delicious.
Along with the announcement of Reddit's switch to Python came the announcement of yet another Python web framework called web.py. I am a fan of its author, so I'm inclined to think that it does actually have something new to offer. And while it does some things differently, it still doesn't do things exactly how I'd want them. And that's why there's such fragmentation in the Python web area — because everyone seems to think that about some framework or another.
Here are a list of downsides that currently popular Python web frameworks have:
- Building URL schemes from classes/methods (TurboGears, anything CherryPy based, and many others) may result in lots of nested objects.
- Mapping URL schemes to classes/methods using regular expressions (web.py, Django) sucks because regular expressions suck. Yes they do (and they are overkill for most RESTful URLs).
- Forcing the programmer into specific design patterns due to the above two.
- It's easy to lose track of which URLs go to which methods.
- In almost every method, I'm converting URL parameters from strings (some frameworks automatically convert them if you specify a validator) and setting up try/except blocks to retrieve things with SQLObject, which gets very repetitive.
- Arbitrary resource nesting can get messy to handle if there are non-arbitrary elements in between (consider the path: /files/(any number of subdirectories)/images/(any number of subdirectories)/filename).
- Some mini-frameworks used to be templating-agnostic, but now all these "real solutions" force you into theirs. Is someone going to have to make the templating equivalent of what SQLObject did for SQL?
I think Python programmers are buying into these solutions (and I'm guilty of this as well) because they're "good enough." Even though TurboGears is more cohesive than the DIY glue I was using (Cheetah, CherryPy, SQLObject), it still needs some polish. This is obviously because all the Python frameworks out there are gluing together existing projects and therefore making sacrifices in how well these pieces mesh. Rails has a lot to show for being developed in-house.
So, I was inspired to start a new web project. That's right, you can call me part of the problem. The difference is that I have a simple goal in mind, which has nothing to do with making the "best" solution: use every conceivable Python hack out there. Many Python frameworks claim to be "Pythonic," which usually means they don't do a lot of wizardry and black magic, or at least don't expose that to the programmer.
Well, I want to do things a completely different way and hope to inspire some new solutions to existing problems. And if that means using crazy metaclasses, custom infix operators, constructs not meant for Python, and excessive operator overloading, then so be it. I'm going to experiment with possibilities others feared due to "Pythonic"-imposed limitations, and I'm going to use hacks that others would never dream of putting in "real" code.
My first task was to design a clever and easy-to-understand URL mapping scheme. Tomorrow I'll post the results of my effort and why I think what I came up with beats anything else out there for the use cases I most commonly encounter.
Comments
"all the Python frameworks out there are gluing together existing projects and therefore making sacrifices in how well these pieces mesh"
This isn't true about Django, which was extracted from a real-world project and doesn't glue any unrelated existing projects together. (Which is a huge advantage in cohesiveness, in my opinion.)
How about just contributing your ideas and patches to Django? We're very keen on making things as good as possible.
If you are looking for other URL mappings, you should look at Routes as well (http://routes.groovie.org) which doesn't use regexes and is reversable.
Also, if you are designing a framework (everyone has to design one or two, it's like some sort of right of passage) you should look into Paste, as it has a lot of tools to make that easier, and the general archictecture will allow you to punt on lots of boring details (e.g., serving static files, handling errors, etc). Paste is mostly a canvas for framework authors; if you see its API leaking through, it means someone forgot to finish their framework.