Alex conrad - Pyramid Tweens (PloneConf 2011)

73
Alex Conrad @alexconrad Code Monkey at SurveyMonkey PloneConf 2011 Nov 5, 2011 Tweens

description

understanding Pyramid Tweens

Transcript of Alex conrad - Pyramid Tweens (PloneConf 2011)

Page 1: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Alex Conrad@alexconrad

Code Monkeyat SurveyMonkey

PloneConf 2011Nov 5, 2011

Tweens

Page 2: Alex conrad  - Pyramid Tweens (PloneConf 2011)

What are Pyramid Tweens?

Page 3: Alex conrad  - Pyramid Tweens (PloneConf 2011)

omg tweens r soooo, like, totally awesome!!!!!!1! lol

(totally friend me on twtr)

^^

Page 4: Alex conrad  - Pyramid Tweens (PloneConf 2011)

They are NOTpre-teenager girls

(too old for toys, too young for boys)

Page 5: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Pyramid Tweens were namedafter the word “between”

Page 6: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Pyramid Tweensare middlewares

Page 7: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Pyramid Tweens live betweenPyramid code and your application views

Page 8: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Pyramid Tweens are NOTWSGI middlewares

Page 9: Alex conrad  - Pyramid Tweens (PloneConf 2011)
Page 10: Alex conrad  - Pyramid Tweens (PloneConf 2011)

REQUEST

Page 11: Alex conrad  - Pyramid Tweens (PloneConf 2011)

REQUEST

Page 12: Alex conrad  - Pyramid Tweens (PloneConf 2011)

So why use Tweens?

Page 13: Alex conrad  - Pyramid Tweens (PloneConf 2011)

To apply a common behavioron all requests

Page 14: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Think of them as decoratorsapplied to each of your Pyramid views.

Page 15: Alex conrad  - Pyramid Tweens (PloneConf 2011)

@foo@bardef useless_view(request):    return {}

Page 16: Alex conrad  - Pyramid Tweens (PloneConf 2011)

@foo@bardef useless_view(request):    return {}

These are notTweens, but actualdecorators, ok?

Page 17: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Tweens can be stacked together

Page 18: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Pyramid Tweensvs.

WSGI Middlewares

Page 19: Alex conrad  - Pyramid Tweens (PloneConf 2011)

WSGI middleware Tweens

Pipelining YES YES

Error catching YES YES

WSGI compliant YES NO

Access to application

(registry, template engine, ...)

NO YES

Page 20: Alex conrad  - Pyramid Tweens (PloneConf 2011)

How does it work?

Page 21: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Create a tween factory: a function

Page 22: Alex conrad  - Pyramid Tweens (PloneConf 2011)

A tween factory takes 2 arguments:

- handler- registry

Page 23: Alex conrad  - Pyramid Tweens (PloneConf 2011)

A tween factory returns a function:a tween

(just like a decorator does)

Page 24: Alex conrad  - Pyramid Tweens (PloneConf 2011)

The tween function takes 1 argument:- request

(just like a view would)

Page 25: Alex conrad  - Pyramid Tweens (PloneConf 2011)

The tween function returns a response object

(just like a view would)

Page 26: Alex conrad  - Pyramid Tweens (PloneConf 2011)

A tween factory is calledat application startup

Page 27: Alex conrad  - Pyramid Tweens (PloneConf 2011)

A tween is calledper request

Page 28: Alex conrad  - Pyramid Tweens (PloneConf 2011)

The tween factory decidesif the tween should be part of the tween stack

Page 29: Alex conrad  - Pyramid Tweens (PloneConf 2011)

def useless_tween_factory(handler, registry):    if registry.settings['useless'] == '1':        def useless_tween(request):            response = handler(request)            return response        return useless_tween    return handler

handler: the next tween in the stackregistry: the pyramid registry

Page 30: Alex conrad  - Pyramid Tweens (PloneConf 2011)

def useless_tween_factory(handler, registry):    if registry.settings['useless'] == '1':        def useless_tween(request):            response = handler(request)            return response        return useless_tween    return handler

handler: the next tween in the stackregistry: the pyramid registry

this is a tween

Page 31: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Tween positioning

Page 32: Alex conrad  - Pyramid Tweens (PloneConf 2011)

REQUEST

Page 33: Alex conrad  - Pyramid Tweens (PloneConf 2011)

REQUEST

Page 34: Alex conrad  - Pyramid Tweens (PloneConf 2011)

REQUEST

Page 35: Alex conrad  - Pyramid Tweens (PloneConf 2011)

---------------------- INGRESS ----------------------

------------------------ MAIN ------------------------

REQUEST

Page 36: Alex conrad  - Pyramid Tweens (PloneConf 2011)

3 ways of positioning tweens

- implicit- implicit (hint)

- explicit

Page 37: Alex conrad  - Pyramid Tweens (PloneConf 2011)

3 ways of positioning tweens

- implicit- implicit (hint)

- explicit

Page 38: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Implicit positioning

from pyramid.config import Configurator

# ... more imports

def main(global_config, **settings):

    config = Configurator(...)

    config.add_tween('useless.useless_tween_factory')

    config.add_tween('useless.null_tween_factory')

    # ... more stuff

    return config.make_wsgi_app()

Page 39: Alex conrad  - Pyramid Tweens (PloneConf 2011)

REQUEST

Implicit positioning

from pyramid.config import Configurator

# ... more imports

def main(global_config, **settings):

    config = Configurator(...)

    config.add_tween('useless.useless_tween_factory')

    config.add_tween('useless.null_tween_factory')

    # ... more stuff

    return config.make_wsgi_app()

Page 40: Alex conrad  - Pyramid Tweens (PloneConf 2011)

INGRESSuseless.null_tween_factory

useless.useless_tween_factorypyramid.tweens.excview_tween_factory

MAIN

Page 41: Alex conrad  - Pyramid Tweens (PloneConf 2011)

REQUEST

INGRESSuseless.null_tween_factory

useless.useless_tween_factorypyramid.tweens.excview_tween_factory

MAIN

Page 42: Alex conrad  - Pyramid Tweens (PloneConf 2011)

3 ways of positioning tweens

- implicit- implicit (hint)

- explicit

Page 43: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Suggested positioning (hint)

config.add_tween('useless.useless_tween_factory')

config.add_tween('useless.null_tween_factory',

                 over=pyramid.tweens.MAIN,

                 under='useless.useless_tween_factory')

Page 44: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Tween constants

pyramid.tweens.INGRESSpyramid.tweens.EXCVIEWpyramid.tweens.MAIN

Page 45: Alex conrad  - Pyramid Tweens (PloneConf 2011)

You cannot position a tweenbefore INGRESS (over)nor after MAIN (under)

Page 46: Alex conrad  - Pyramid Tweens (PloneConf 2011)

3 ways of positioning tweens

- implicit- implicit (hint)

- explicit

Page 47: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Explicit positioning

# development.ini

[app:main]

...

pyramid.tweens = useless.useless_tween_factory

                 useless.null_tween_factory

                 pyramid.tweens.excview_tween_factory

Page 48: Alex conrad  - Pyramid Tweens (PloneConf 2011)

WARNING:

Explicit tween ordering(via the .ini file)

will ignore any calls toconfig.add_tween( … )

Page 49: Alex conrad  - Pyramid Tweens (PloneConf 2011)

WARNING:

[app:main]...pyramid.includes = pyramid_debugtoolbarpyramid.tweens = useless.useless_tween_factory                 useless.null_tween_factory                 pyramid.tweens.excview_tween_factory

Page 50: Alex conrad  - Pyramid Tweens (PloneConf 2011)

WARNING:

[app:main]...pyramid.includes = pyramid_debugtoolbarpyramid.tweens = useless.useless_tween_factory                 useless.null_tween_factory                 pyramid.tweens.excview_tween_factory

Page 51: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Explicitly tween positioning requiresthat you declare ALL tweens.

Page 52: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Listing tweens

Page 53: Alex conrad  - Pyramid Tweens (PloneConf 2011)

$ paster ptweens development.ini#main

Position    Name­­­­­­­­    ­­­­­           INGRESS0           useless.useless_tween_factory1           useless.null_tween_factory2           pyramid.tweens.excview_tween_factory­           MAIN

Page 54: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Tweens you might be using:

- pyramid_debugtoolbar- pyramid_tm

Page 55: Alex conrad  - Pyramid Tweens (PloneConf 2011)

pyramid_debugtoolbar

Page 56: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Debug Toolbar renders and injects HTMLinto your response

Page 57: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Although it can decide whether or not it should render HTML depending on your host address

Page 58: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Transaction Manager(pyramid_tm)

Page 59: Alex conrad  - Pyramid Tweens (PloneConf 2011)

For every request,it creates a new transactionand closes it on response.

Page 60: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Transaction Manageris NOT about database transactions

Page 61: Alex conrad  - Pyramid Tweens (PloneConf 2011)

pyramid_tm relies on the transaction package

Page 62: Alex conrad  - Pyramid Tweens (PloneConf 2011)

1

Register your commit / rollback action withthe transaction package (join() the transaction)

Page 63: Alex conrad  - Pyramid Tweens (PloneConf 2011)

2pyramid_tm calls:

transaction.begin() on requestthen

transaction.commit() or transaction.rollback()

Page 64: Alex conrad  - Pyramid Tweens (PloneConf 2011)

3

the transaction package will thencall subscribed callbacks

Page 65: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Why is this useful?

Page 66: Alex conrad  - Pyramid Tweens (PloneConf 2011)

One single point of commit.

Page 67: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Avoid sending an email“Your account has been created! \o/”

when your DB write actually failed

Page 68: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Things you could do:

- commit your DB transaction- invalidate cache- send an email

...

Page 69: Alex conrad  - Pyramid Tweens (PloneConf 2011)

I already have try/except blocks in my Pyramid views, I don't need pyramid_tm.

Until you get a stupid Exception outside your block (e.g., in the renderer) for whatever reason.

Also know as: a bug.

Page 70: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Pyramid registers SQLAlchemy automatically

extension=ZopeTransactionExtension()

Page 71: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Did you know?

pyramid_tm is responsiblefor the existence of Pyramid tweens.

It was the first use case for tweens.

Page 72: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Now you are tween experts!

Use wisely.

Page 73: Alex conrad  - Pyramid Tweens (PloneConf 2011)

Thank you !!!

let's totally follow each other like,on twitter!!!1! LOOOL :D

@alexconrad