Max Polun's blog

What rails could learn from pyramid

I like to look at new web frameworks every once in a while. I’ve used and enjoyed rails, I’ve used django, I’ve used bottle, but it’s hard to find a new one that I like. Too many have weird ideas of what webdev should be like. Pyramid has some odd ideas, but it also has some really good ideas.

First some terminology: in pyramid you have Models (using whatever sort of ORM you want, usually sqlalchemy), Views (which are similar to rails controllers), Renderers (similar to rails views), and configuration (which is used for a variety of things, but mainly routing).

Here’s what I think is valuable in pyramid:

# in views.py:
@view_config(route_name='myroute' renderer='templates/myroute.jinja2')
def myroute(request):
  return {"hello": request.matchdict["name"]}
#in tests.py
class MyRouteTest(unittest.TestCase):
  def test_name(self):
    request = testing.DummyRequest()
    request.matchdict['name'] = 'Test'
    response = myroute(request)
    self.assertEqual(response['name'], 'Test')

Some things are really judgement calls, rails is very convention based: you don’t have to tell it what view to call, it inferrs that from the name of your controller method. Pyramid is much more configuration based, which is more pythonic, but maybe not better (or worse, just different). I prefer configuration when it’s simple (like in pyramid), but it can get out of control.