Tweens Alex Conrad @alexconrad  Code Monkeyat SurveyMonkeyPloneConf 2011   Nov 5, 2011
What are Pyramid Tweens?
omg tweens r soooo, like, totally awesome!!!!!!1!                      lol             (totally friend me on twtr)        ...
They are NOT         pre-teenager girls(too old for toys, too young for boys)
Pyramid Tweens were named  after the word “between”
Pyramid Tweensare middlewares
Pyramid Tweens live betweenPyramid code and your application views
Pyramid Tweens are NOT   WSGI middlewares
REQUEST
REQUEST
So why use Tweens?
To apply a common behavior      on all requests
Think of them as decoratorsapplied to each of your Pyramid views.
@foo@bardef useless_view(request):    return {}
These are not          Tweens, but actual@foo          decorators, ok?@bardef useless_view(request):    return {}
Tweens can be stacked together
Pyramid Tweens       vs.WSGI Middlewares
WSGI                                       Tweens                          middleware       Pipelining            YES     ...
How does it work?
Create a tween factory: a function
A tween factory takes 2 arguments:            - handler           - registry
A tween factory returns a function:             a tween     (just like a decorator does)
The tween function takes 1 argument:            - request        (just like a view would)
The tween function returns a response object            (just like a view would)
A tween factory is called at application startup
A tween is called   per request
The tween factory decidesif the tween should be part of the tween stack
def useless_tween_factory(handler, registry):    if registry.settings[useless] == 1:        def useless_tween(request):   ...
def useless_tween_factory(handler, registry):    if registry.settings[useless] == 1:        def useless_tween(request):   ...
Tween positioning
REQUEST
REQUEST
REQUEST
REQUEST   ---------------------- INGRESS ----------------------    ------------------------ MAIN ------------------------
3 ways of positioning tweens           - implicit       - implicit (hint)           - explicit
3 ways of positioning tweens           - implicit       - implicit (hint)           - explicit
Implicit positioningfrom pyramid.config import Configurator# ... more importsdef main(global_config, **settings):    confi...
Implicit positioningfrom pyramid.config import Configurator# ... more importsdef main(global_config, **settings):    confi...
INGRESS      useless.null_tween_factory    useless.useless_tween_factorypyramid.tweens.excview_tween_factory              ...
REQUEST              INGRESS      useless.null_tween_factory    useless.useless_tween_factorypyramid.tweens.excview_tween_...
3 ways of positioning tweens           - implicit       - implicit (hint)           - explicit
Suggested positioning (hint)config.add_tween(useless.useless_tween_factory)config.add_tween(useless.null_tween_factory,   ...
Tween constantspyramid.tweens.INGRESSpyramid.tweens.EXCVIEW  pyramid.tweens.MAIN
You cannot position a tween  before INGRESS (over)  nor after MAIN (under)
3 ways of positioning tweens           - implicit       - implicit (hint)           - explicit
Explicit positioning# development.ini[app:main]...pyramid.tweens = useless.useless_tween_factory                 useless.n...
WARNING:Explicit tween ordering    (via the .ini file)will ignore any calls toconfig.add_tween( … )
WARNING:[app:main]...pyramid.includes = pyramid_debugtoolbarpyramid.tweens = useless.useless_tween_factory                ...
WARNING:[app:main]...pyramid.includes = pyramid_debugtoolbarpyramid.tweens = useless.useless_tween_factory                ...
Explicitly tween positioning requires  that you declare ALL tweens.
Listing tweens
$ paster ptweens development.ini#mainPosition    Name­­­­­­­­    ­­­­­           INGRESS0           useless.useless_tween_...
Tweens you might be using: - pyramid_debugtoolbar       - pyramid_tm
pyramid_debugtoolbar
Debug Toolbar renders and injects HTML          into your response
Although it can decide whether or not it shouldrender HTML depending on your host address
Transaction Manager    (pyramid_tm)
For every request,it creates a new transaction and closes it on response.
Transaction Manageris NOT about database transactions
pyramid_tm relies on the transaction package
1  Register your commit / rollback action withthe transaction package (join() the transaction)
2              pyramid_tm calls:       transaction.begin() on request                    thentransaction.commit() or trans...
3the transaction package will then    call subscribed callbacks
Why is this useful?
One single point of commit.
Avoid sending an email“Your account has been created! o/” when your DB write actually failed
Things you could do:- commit your DB transaction     - invalidate cache       - send an email              ...
I already have try/except blocks in my Pyramid        views, I dont need pyramid_tm.  Until you get a stupid Exception out...
Pyramid registers SQLAlchemy automatically     extension=ZopeTransactionExtension()
Did you know?      pyramid_tm is responsiblefor the existence of Pyramid tweens.It was the first use case for tweens.
Now you are tween experts!       Use wisely.
Thank you !!!lets totally follow each other like,      on twitter!!!1! LOOOL :D        @alexconrad
Alex conrad  - Pyramid Tweens (PloneConf 2011)
Upcoming SlideShare
Loading in...5
×

Alex conrad - Pyramid Tweens (PloneConf 2011)

2,218

Published on

understanding Pyramid Tweens

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,218
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
17
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Alex conrad - Pyramid Tweens (PloneConf 2011)

  1. 1. Tweens Alex Conrad @alexconrad Code Monkeyat SurveyMonkeyPloneConf 2011 Nov 5, 2011
  2. 2. What are Pyramid Tweens?
  3. 3. omg tweens r soooo, like, totally awesome!!!!!!1! lol (totally friend me on twtr) ^^
  4. 4. They are NOT pre-teenager girls(too old for toys, too young for boys)
  5. 5. Pyramid Tweens were named after the word “between”
  6. 6. Pyramid Tweensare middlewares
  7. 7. Pyramid Tweens live betweenPyramid code and your application views
  8. 8. Pyramid Tweens are NOT WSGI middlewares
  9. 9. REQUEST
  10. 10. REQUEST
  11. 11. So why use Tweens?
  12. 12. To apply a common behavior on all requests
  13. 13. Think of them as decoratorsapplied to each of your Pyramid views.
  14. 14. @foo@bardef useless_view(request):    return {}
  15. 15. These are not Tweens, but actual@foo decorators, ok?@bardef useless_view(request):    return {}
  16. 16. Tweens can be stacked together
  17. 17. Pyramid Tweens vs.WSGI Middlewares
  18. 18. WSGI Tweens middleware Pipelining YES YES Error catching YES YESWSGI compliant YES NO Access to application NO YES (registry, template engine, ...)
  19. 19. How does it work?
  20. 20. Create a tween factory: a function
  21. 21. A tween factory takes 2 arguments: - handler - registry
  22. 22. A tween factory returns a function: a tween (just like a decorator does)
  23. 23. The tween function takes 1 argument: - request (just like a view would)
  24. 24. The tween function returns a response object (just like a view would)
  25. 25. A tween factory is called at application startup
  26. 26. A tween is called per request
  27. 27. The tween factory decidesif the tween should be part of the tween stack
  28. 28. 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 stack registry: the pyramid registry
  29. 29. 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 stack registry: the pyramid registry this is a tween
  30. 30. Tween positioning
  31. 31. REQUEST
  32. 32. REQUEST
  33. 33. REQUEST
  34. 34. REQUEST ---------------------- INGRESS ---------------------- ------------------------ MAIN ------------------------
  35. 35. 3 ways of positioning tweens - implicit - implicit (hint) - explicit
  36. 36. 3 ways of positioning tweens - implicit - implicit (hint) - explicit
  37. 37. Implicit positioningfrom pyramid.config import Configurator# ... more importsdef 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()
  38. 38. Implicit positioningfrom pyramid.config import Configurator# ... more importsdef main(global_config, **settings):    config = Configurator(...)    config.add_tween(useless.useless_tween_factory)    config.add_tween(useless.null_tween_factory)    # ... more stuff REQUEST    return config.make_wsgi_app()
  39. 39. INGRESS useless.null_tween_factory useless.useless_tween_factorypyramid.tweens.excview_tween_factory MAIN
  40. 40. REQUEST INGRESS useless.null_tween_factory useless.useless_tween_factorypyramid.tweens.excview_tween_factory MAIN
  41. 41. 3 ways of positioning tweens - implicit - implicit (hint) - explicit
  42. 42. 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)
  43. 43. Tween constantspyramid.tweens.INGRESSpyramid.tweens.EXCVIEW pyramid.tweens.MAIN
  44. 44. You cannot position a tween before INGRESS (over) nor after MAIN (under)
  45. 45. 3 ways of positioning tweens - implicit - implicit (hint) - explicit
  46. 46. Explicit positioning# development.ini[app:main]...pyramid.tweens = useless.useless_tween_factory                 useless.null_tween_factory                 pyramid.tweens.excview_tween_factory
  47. 47. WARNING:Explicit tween ordering (via the .ini file)will ignore any calls toconfig.add_tween( … )
  48. 48. WARNING:[app:main]...pyramid.includes = pyramid_debugtoolbarpyramid.tweens = useless.useless_tween_factory                 useless.null_tween_factory                 pyramid.tweens.excview_tween_factory
  49. 49. WARNING:[app:main]...pyramid.includes = pyramid_debugtoolbarpyramid.tweens = useless.useless_tween_factory                 useless.null_tween_factory                 pyramid.tweens.excview_tween_factory
  50. 50. Explicitly tween positioning requires that you declare ALL tweens.
  51. 51. Listing tweens
  52. 52. $ paster ptweens development.ini#mainPosition    Name­­­­­­­­    ­­­­­           INGRESS0           useless.useless_tween_factory1           useless.null_tween_factory2           pyramid.tweens.excview_tween_factory­           MAIN
  53. 53. Tweens you might be using: - pyramid_debugtoolbar - pyramid_tm
  54. 54. pyramid_debugtoolbar
  55. 55. Debug Toolbar renders and injects HTML into your response
  56. 56. Although it can decide whether or not it shouldrender HTML depending on your host address
  57. 57. Transaction Manager (pyramid_tm)
  58. 58. For every request,it creates a new transaction and closes it on response.
  59. 59. Transaction Manageris NOT about database transactions
  60. 60. pyramid_tm relies on the transaction package
  61. 61. 1 Register your commit / rollback action withthe transaction package (join() the transaction)
  62. 62. 2 pyramid_tm calls: transaction.begin() on request thentransaction.commit() or transaction.rollback()
  63. 63. 3the transaction package will then call subscribed callbacks
  64. 64. Why is this useful?
  65. 65. One single point of commit.
  66. 66. Avoid sending an email“Your account has been created! o/” when your DB write actually failed
  67. 67. Things you could do:- commit your DB transaction - invalidate cache - send an email ...
  68. 68. I already have try/except blocks in my Pyramid views, I dont need pyramid_tm. Until you get a stupid Exception outside yourblock (e.g., in the renderer) for whatever reason. Also know as: a bug.
  69. 69. Pyramid registers SQLAlchemy automatically extension=ZopeTransactionExtension()
  70. 70. Did you know? pyramid_tm is responsiblefor the existence of Pyramid tweens.It was the first use case for tweens.
  71. 71. Now you are tween experts! Use wisely.
  72. 72. Thank you !!!lets totally follow each other like, on twitter!!!1! LOOOL :D @alexconrad
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×