際際滷

際際滷Share a Scribd company logo
Pyramid
"Payonlyforwhatyoueat"
http://bit.ly/bcnmeetup-pyramid
V鱈ctorFern叩ndezdeAlba
Lead web developer at Barcelona Tech University
Author of Plone 3 Intranets (2010, PacktPub)
Developing Plone sites since 2004
Developing Pyramid applications since its early betas
- /V鱈ctor's Blog @sneridagh
Briefhistory
Pylons (2005-2010)
+
repoze.bfg (2008-2010)
=
Pyramid (2010- )
Joined under the same umbrella, the
TurboGears joined to the project few months later
Version 1.4 supports Python 2.6 ~ 3.x
Pylons Project
Simplicity
About ~5000 lines of code
Minimalism
Try to solve the fundamental problems
Mapping URLs to code
Templating
Security
Serve static assets
Documentation
Just a word... awesome
Obsessively up-to-date
More than 800 printed pages
Speed
Optimized for fast code execution
Reliability
100% obsessive tested
"If it aint tested, its broke"
and...
Beautifuly pythonic
Pragmatism everywhere
Lots of: "Fuck, yeah!"
HeavyinfluenceofZope
Chris McDonough
Configuration via zcml (optional, not core)
Component architecture (zope.component)
ZODB (optional, not enforced)
URL Traversal concept
Singlefileapplication
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
def hello_world(request):
return Response('Hello %(name)s!' % request.matchdict)
if __name__ == '__main__':
config = Configurator()
config.add_route('hello', '/hello/{name}')
config.add_view(hello_world, route_name='hello')
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 8080, app)
server.serve_forever()
Applicationconfiguration
Declarative (previous example)
Imperative (via decorators)
from pyramid.response import Response
from pyramid.view import view_config
@view_config(name='hello', request_method='GET')
def hello(request):
return Response('Hello')
Developertools
Setuptools/Distribute compliant
Project scaffolding
Convenience scripts
Debug toolbar
Templating
Chameleon (Zope Page Templates clone)
Mako
Jinja2
Virtually any other pythonic template system
Persistence
SQLAlchemy (OOTB)
ZODB (OOTB)
MongoDB
Virtually any other persistence system or database
Security
High level of granularity
Extensible
Pluggable
Local, LDAP, SQL, oAuth providers, etc.
Miscelaneous
Pastedeploy configuration
i18n
Event system
Hooks
Tweens concept
Session management
Cornice:ARESTframeworkfor
Pyramid
[..]
from cornice import Service
info_desc = """This service is useful to get and set data for a user."""
user_info = Service(name='users', path='/{username}/info',
description=info_desc)
_USERS = defaultdict(dict)
@user_info.get()
def get_info(request):
"""Returns the public information about a **user**.
If the user does not exists, returns an empty dataset.
"""
username = request.matchdict['username']
return _USERS[username]
Cornice:ARESTframeworkfor
Pyramid(II)
@user_info.post()
def set_info(request):
"""Set the public information for a **user**.
You have to be that user, and *authenticated*.
Returns *True* or *False*.
"""
username = authenticated_userid(request)
if request.matchdict["username"] != username:
raise Forbidden()
_USERS[username] = request.json_body
return {'success': True}
Resources
http://docs.pylonsproject.org/
http://cornice.readthedocs.org/
https://github.com/pylons
Sorryforthe
convenience

More Related Content

Introduction to Pyramid