Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Minimal Pyramid Tutorial
Python
Tree: b764221911

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
pyramidtutorial
CHANGES.txt
MANIFEST.in
README.rst
development.ini
setup.cfg
setup.py

README.rst

Pyramid

http://docs.pylonsproject.org/docs/pyramid.html

Pyramid is Pylons 2.0, a merger of Pylons and repoze.bfg. Pyramid is smaller, faster, more easily extended and lighter weight than Pylons.

Changes

  • No globals, almost everything hangs off of a request
  • Resources rather than Models
  • Resource Traversal supplants Routes
  • Simpler application extention
  • Better test support
  • Built-in Auth

Setup

I've loaded this tutorial to github:

git clone git://github.com/wapolabs/PyramidTutorial
cd PyramidTutorial
python setup.py develop

setup.py will pull in all of the Pyramid dependencies.

File Tree

  • setup.py, setup.cfg, README.txt - standard python packaging
  • development.ini - similar to Pylons
  • pyramidtutorial/__init__.py - can contain the entire app
  • pyramidtutorial/tests.py
  • pyramidtutorial/static - public files
  • pyramidtutorial/templates - templates: mako, pt, etc.

No middleware files or other handwaving to support the framework. All setup is in __init__.

Minimal Application

Pyramid permits a single file application (master branch):

from pyramid.config import Configurator

def simple(request):
  return {'project':'PyramidTutorial'}

def main(global_config, **settings):
  config = Configurator(settings=settings)
  config.add_view('pyramidtutorial.simple',
    renderer='pyramidtutorial:templates/home.mako')
  return config.make_wsgi_app()

Pylons Style Routes

Pyramid supports routes through add_route (routes branch):

def simple(request):
  return {'project':'PyramidTutorial','params':request.matchdict}

def main(global_config, **settings):
  config = Configurator(settings=settings)
  config.add_route('home', '/', view='pyramidtutorial.simple',
    view_renderer='pyramidtutorial:templates/home.mako')
  config.add_route('doc', '/prefix/{one}/{two}', view='pyramidtutorial.simple',
    view_renderer='pyramidtutorial:templates/params.mako')
  config.add_static_view('static', 'pyramidtutorial:static')
  return config.make_wsgi_app()

Akhet Template for Pylons

  • Minimal pain for Pylons migration
  • But also minimal gain
  • Wraps BeforeRender with h and c to support mako templates
  • Rolls in SqlAlchemy and access to 'model'

Resources and Traversal

  • Real advantage of Pyramid
  • Conceptually a tree of nested dicts
  • Practically a tree of objects implementing __getitem__()
  • Ideal for REST implementations

Testing

  • pyramid.testing provides resource setup and some mock objects
  • views return dicts so most testing is simpler than Pylons
  • paster pshell provides interactive access to resources for debugging

Auth

  • authentication and authorization policies set in Configurator.
  • Authorization failures throw a forbidden error, which is caught by the framework as context=Forbidden
  • builtin ACLAuthorizationPolicy works with resources and handles most ACL models.
Something went wrong with that request. Please try again.