Request for elucidation: enhanced generators

Michele Simionato michele.simionato at gmail.com
Wed Sep 20 09:58:54 EDT 2006


Duncan Booth wrote:
> One example which is commonly given is to implement a web application using
> continuations. So in Python terms a session is handled by a generator which
> yields a form to be sent back to the user and the user's response is sent
> in to the request using the generator's send method. State would be
> maintained in the generator. Whether this actually works well in practice
> (and how you handle things like a back button on the forms) is another
> matter.
>
> So very roughly your web code might look like this:
>
> def checkout(basket):
>     orderOk = False
>
>     while not OrderOk:
>        order = (yield OrderForm(basket))
>        address = (yield AddressForm())
>        payment = (yield PaymentForm(order.total))
>        confirmation = (yield ConfirmationForm(order, address, payment))
>        orderOk = confirmation.confirmed
>
>     placeOrder(order, address, payment)
>     yield OrderPlacedPage()

I too wonder about the back button. I am familiar with this reference
about modal
web servers, that you probably know:
http://www.double.co.nz/scheme/modal-web-server.html
In that case, the back button can be made to work since in Scheme there
are full
continuations and it is possibile to store the state at a given point
in time and go
back to that point later on (i.e. when the user press the back button).

Python generators however are not full continuations and you cannot go
back
to a previous time. I remember I did some experiment and it was
possible to
solve the problem by generating a lot of closures and associating each
closure to
one state in the time and one page in the UI. But is was complicated
enough
and finally I ended up simply storing the values entered by the user in
HTML hidden widgets.
Sometimes the simplest solutions are the better ones ;)

                       Michele Simionato




More information about the Python-list mailing list