Alex conrad - Pyramid Tweens (PloneConf 2011)

  • 2,014 views
Uploaded on

understanding Pyramid Tweens

understanding Pyramid Tweens

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,014
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
17
Comments
0
Likes
3

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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