21
Pyramid "Pay only for what you eat" http://bit.ly/bcnmeetup-pyramid

Introduction to Pyramid

Embed Size (px)

DESCRIPTION

Talk performed at the Barcelona python meetup with the topic "Python web frameworks"

Citation preview

Page 1: Introduction to Pyramid

Pyramid"Pay only for what you eat"

http://bit.ly/bcnmeetup-pyramid

Page 2: Introduction to Pyramid

Víctor Fernández de AlbaLead web developer at Barcelona Tech UniversityAuthor of Plone 3 Intranets (2010, PacktPub)Developing Plone sites since 2004Developing Pyramid applications since its early betas

- / Víctor's Blog @sneridagh

Page 3: Introduction to Pyramid

Brief historyPylons (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

Page 4: Introduction to Pyramid

SimplicityAbout ~5000 lines of code

Page 5: Introduction to Pyramid

MinimalismTry to solve the fundamental problems

Mapping URLs to codeTemplatingSecurityServe static assets

Page 6: Introduction to Pyramid

DocumentationJust a word... awesome

Obsessively up-to-date

More than 800 printed pages

Page 7: Introduction to Pyramid

SpeedOptimized for fast code execution

Page 8: Introduction to Pyramid

Reliability100% obsessive tested

"If it ain’t tested, it’s broke"

Page 9: Introduction to Pyramid

and...Beautifuly pythonic

Pragmatism everywhere

Lots of: "Fuck, yeah!"

Page 10: Introduction to Pyramid

Heavy influence of ZopeChris McDonoughConfiguration via zcml (optional, not core)Component architecture (zope.component)ZODB (optional, not enforced)URL Traversal concept

Page 11: Introduction to Pyramid

Single file applicationfrom wsgiref.simple_server import make_serverfrom pyramid.config import Configuratorfrom 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()

Page 12: Introduction to Pyramid

Application configurationDeclarative (previous example)Imperative (via decorators)

from pyramid.response import Responsefrom pyramid.view import view_config

@view_config(name='hello', request_method='GET')def hello(request): return Response('Hello')

Page 13: Introduction to Pyramid

Developer toolsSetuptools/Distribute compliantProject scaffoldingConvenience scriptsDebug toolbar

Page 14: Introduction to Pyramid

TemplatingChameleon (Zope Page Templates clone)MakoJinja2Virtually any other pythonic template system

Page 15: Introduction to Pyramid

PersistenceSQLAlchemy (OOTB)ZODB (OOTB)MongoDBVirtually any other persistence system or database

Page 16: Introduction to Pyramid

SecurityHigh level of granularityExtensiblePluggableLocal, LDAP, SQL, oAuth providers, etc.

Page 17: Introduction to Pyramid

MiscelaneousPastedeploy configurationi18nEvent systemHooksTweens conceptSession management

Page 18: Introduction to Pyramid

Cornice: A REST framework forPyramid

[..]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]

Page 19: Introduction to Pyramid

Cornice: A REST framework forPyramid (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}

Page 20: Introduction to Pyramid

Resourceshttp://docs.pylonsproject.org/http://cornice.readthedocs.org/https://github.com/pylons

Page 21: Introduction to Pyramid

Sorry for theconvenience