Thursday, December 27, 2007

Pylons and Django

Thinking about switching the webapp portions of GameClay from Pylons to Django. This would be our third framework (web.py -> Pylons, Pylons -> Django), and Pylons is mostly doing acceptably, though it's pretty annoying in some places and needs some code to fill in functionality that Django provides for free. So this is not something I'm going to do lightly.

Here's a list of the various aspects and advantages & disadvantages of the frameworks:

Templates

For Pylons, I'd stick with the default of Mako. Django comes with its own templating language, but it's not terribly hard to swap it out. However, the existing contrib apps expect templates to be written in Django's language, so if we do any customization of contrib apps, we'll need to use Django's. It's probably better to stick with that for all web templates, even though we'll be using standalone Mako in the game compiler.

Advantages for Pylons:
  • I don't have to rewrite our existing Mako templates
  • Mako is significantly more powerful than Django, with arbitrary expressions, defs, template inheritance (beyond layouts)
  • Mako filters get pulled from the template namespace automatically, which is a useful convenience when using potentially lots of filters
  • Same template system for website and game compiler
  • I can extend it with defs, in the template, without needing to write a separate Python module to write new tags
Advantages for Django:
  • I like the syntax better; it seems cleaner and easier to remember
  • Filters can take arguments; useful for things like indent()
  • The inclusion_tag syntax provides cleaner-syntaxed and more flexible blocks of reusable content than Mako's defs. The pitfall is that these are not defined with the other templates; they live in the template_tags directory.
Models

For Pylons, I'd use our custom Diffle DB database wrapper. For Django, I'd use its ORM.

Advantages for Pylons:
  • Simpler; I know SQL already, don't need to learn a custom model definition language
  • Can handle multiple databases reasonably easily. It seems very difficult to add this to Django; there's been a branch with it since 2006, but no movement on it. It seems like it'd be a real pain to adapt save() and the query methods to a horizontally partitioned database.
  • Gives finer-grained control over queries; I can often avoid some extraneous SELECT or UPDATE statements that Django's ORM adds.
  • Supports multi-column primary keys, something I've found pretty useful in past DB models.
Advantages for Django:
  • The model definition classes are actually kinda convenient to use; would eliminate the mental context switch needed to go between SQL and Python. (I can get similar benefits by moving to SQLAlchemy + Pylons, but then I lose the other advantages of using only a thin wrapper).
  • The admin interface is very convenient; it'd let Mike respond to routine complaints from users instead of requiring my intervention. (I might be able to build something similar by introspecting the database, but it'd take time.)
Controllers
Pylons controllers are basically WSGI apps, setup by the "paster controller" script. Django controllers are functions that take a Request and return a Response. Pylons uses WSGI pretty extensively throughout; in order to understand the choices it's made for controllers, it helps to understand WSGI really well (I don't, yet).

Advantages for Pylons:
  • Each Pylons controller is its own WSGI app and in theory can use any other WSGI middleware. (In practice, I'm not sure this works, since Pylons constructs the controller objects with each request.)
  • Provides an easy way to attach data to controllers, like a database or mogile connection
  • Routes syntax is a little cleaner and more flexible than Django's URL-based dispatching.
Advantages for Django:
  • Don't have to use the ugly c, request, session, etc. global variables. Since WSGI apps have a defined signature, they can't take additional parameters, which can be quit inconvenient in Pylons.
  • Don't need to mess with StackedObjectProxies. Globals themselves aren't suitable for use in a webapp (they aren't threadsafe), so Pylons has its own version of a threadlocal. Unfortunately this is one hell of a leaky abstraction; you can't do dir() or vars() on it and get back anything sensible, and they break when used in generators.
  • More explicit. Less to keep in mind.
  • Less verbose. Controllers are just functions, and they can all live in views.py (though you can break them out into separate modules if necessary). Presumably they could be callables if necessary, though that breaks the next point.
  • Can be decorated. Django provides a bunch of useful decorators, like @login_required.
I much prefer Django's controller system - it's simple, intuitive, and doesn't have lots of boilerplate. Personally, I think that Pylons' approach of using globals is a big mistake; it was one of the main reasons I moved away from web.py

Sessions/Login/Registration
Pylons gives you AuthKit by default, but that looks so unpolished, undocumented, and unsuitable to GameClay that I'd probably have to write my own. Django offers the contrib.auth application, which depends on the Session middleware.

Advantages for Pylons:
  • Completely flexible, since I'm writing my own. Can include things like IP addresses in the session table, or reference the sessions table to see who's playing which games, or check out what your friends are playing.
  • Integrates easily with the rest of the app, eg. templates and other controllers
Advantages for Django:
  • Provides usable default controllers for login, logout, and registration
  • User objects integrated with request; no need to explicitly check for login
  • Can retrieve all sessions through Session model; however, session models have only the key and data, and so if we want to search by user or page, we have to do it ourselves. We probably need to use custom middleware for this; we can't do it as a session backend because we don't know the username at that point, and it'd be a pain to do it as an authentication backend because we'd also need to change the Session model and that's not directly accessible.
  • Handles details like tamper-proof sessions, hashing, generating unique sessions, etc. transparently
  • Has convenient @login_required decorators for marking posts that require logins. Also allows filter functions; this could be useful once we have shared games and such


Other goodies

With Pylons, you're basically on your own. You can use any Python libraries you need, but you can do that with Django too. Django provides a bunch of nifty features that we might like to use:

Advantages of Django:
  • CSRF detection. We need to add this to all our forms otherwise; this is pretty handy middleware.
  • Redirects. Particularly if we change the ID or URL scheme. We don't want to ever break URLs; this could be a handy way to avoid that.
  • Syndication feeds. There are Python libraries to do this, but the contrib app is likely easier to use


Configuration, Packaging, and Testing
Pylons is based on Paste, setuptools, and nosetests. Django has its own config and packaging system, and uses doctest and unittest for testing. The two are roughly equally easy to use - they both provide single commands to setup most things.

Advantages for Pylons:
  1. They handle multiple config files very well. Each config file is a separate .ini, so you have a test.ini for testing and a development.ini for development and a production.ini for production. Each of these can be checked in, and you specify the configuration to run when you execute Paster. Django lets you have multiple settings modules, but you have to switch between them with an environment variable or command-line switch, which is kinda annoying.[edit: the command-line switch turns out to not be that annoying in practice, though a little confusing if you don't know about it.]
  2. Searches for doctests in all locations, not just models and a specific test module. When you have lots of non-DB code, as we do, there will likely be lots of other stuff.
  3. Easy to specify foreign dependencies via setuptools, and create command-line scripts, and all the other great stuff that setuptools does. Using setuptools with Django is somewhat clumsy; I hope it's possible.
Advantages for Django:
  • Using a Python module as a config file keeps everything Pythonic, and gives some added flexibility
  • The concept of separate "sites" and "apps" results in a cool architecture where we can potentially utilize other people's contributed apps. I doubt this will be terribly relevant for us, but it may turn up something neat.
Overall, I think Pylons has the advantage for packaging and distribution. I'm disappointed that Django seems to be moving away from setuptools, even dropping it for packaging the Django distribution itself. Pylons also really thought through deployment in multiple environments, while Django seems to have done a quick & dirty solution so they could go on to other things.
Other reviews

http://pythonmag.blogspot.com/2006/02/pylons-vs-turbogears.html
http://jesusphreak.infogami.com/blog/why_django
http://jesusphreak.infogami.com/blog/vrp1
http://www.cmlenz.net/blog/2006/08/the_python_web_.html

11 comments:

Crispin said...

We have forked Django and removed the template engine and replaced it with Mako. This includes porting the entire Admin and supporting applications. Check out our project here!

escort ankara said...

Ankara Escort,
Escort Bayan Ankara,
Ankara Escort,
Escort Ankara,
Escort Ankara,
Ankara Escort,
Escort Bayan Ankara,
Ankara Escort,
Escort Bayan Ankara,
Escort Ankara,
Escort Bayan Ankara,
Escort Bayan Kayseri,
Escort Kayseri,
Kayseri Escort,
Escort Ankara,

mr.mustafa said...

=====================================
دردشة عراقية
دردشة احساس العراق

escort ankara said...


Hi, Thank you write.
to share is Ankara Escort thank you share.
good site admin


Escort Ankara
Ankara Escort
Escort Bayan Ankara
Escort Bayan Ankara
Ankara Escort
Escort Ankara
Escort Ankara
Escort Ankara
Escort Bayan Ankara
Escort Ankara
Escort Ankara
Escort Ankara
Escort Ankara
Ankara Escort

Nam said...

I use Mako for my site - softindex.info

think it's faster than django template

Unknown said...

Thanks for sharing. I hope it will be helpful for too many people that are searching for this topic.
Signature:
Jugar juego de frozen en línea gratis, los nuevos de princesa de Disney juegos frozen - la princesa encantadora y linda. Divertirse frozen!

Unknown said...

All are just taking advantage of your kindness to stop his star mng hate this kind whatsapp gratis , unblockedgames very nice , free unblockedgames online to play , descargar whatsapp , unblocked games 77 , unblocked games online

Bella Tran said...

Well this a very a nice blog
----
play game juegos de terror and play game jogos online online and play game unblockedgames

general manager said...

Thanks for sharing about django,
"blueapplecourses"

Unknown said...

TURKEY ESCORTS REKLAMLARI
ankara escort,
ankara escortlar,
escort bayan ankara,
çankaya escort,
balgat escort,
çukurambar escort,
dikmen escort,
ankara vip escort,
ankara escort,
eryaman escort,
mersin escort,
adana escort,
konya escort,
samsun escort,

Unknown said...

ankara escort
cankaya escort
turangünes escort
dikmen escort
cukurambar escort
emek escort
demetevler escort
ostim escort
batıkent escort
yenimahalle escort
eryaman escort