A Pyramid tween (contraction of between) is a bit like a piece of middleware. They allow you to act upon a request or response before/after hitting the controller in a context where they have access to the request, response and app registry.

They comprise of a factory and the function that does the work, e.g in a file called tweens/example_tween:

def example_tween_factory(handler, registry):
    def example_tween(request):
        response = handler(request)

        return response
return example_tween

Which are then registered in the main function:

config.add_tween('server.tweens.example_tween.example_tween_factory')

This short series will detail tweens that I’ve used in projects and found useful.