At 09:12 PM 4/24/05 -0600, Steven Bethard wrote:

I guess it would be helpful to see example where the looping
with-block is useful.

Automatically retry an operation a set number of times before hard failure:

    with auto_retry(times=3):
        do_something_that_might_fail()

Process each row of a database query, skipping and logging those that cause a processing error:

    with x,y,z = log_errors(db_query()):
        do_something(x,y,z)

You'll notice, by the way, that some of these "runtime macros" may be stackable in the expression.

I'm somewhat curious what happens to yields in the body of the macro block, but I assume they'll just do what would normally occur. Somehow it seems strange, though, to be yielding to something other than the enclosing 'with' object.

In any case, I'm personally more excited about the part where this means we get to build co-routines with less magic. The 'with' statement itself is of interest mainly for acquisition/release and atomic/rollback scenarios, but being able to do retries or skip items that cause errors is often handy. Sometimes you have a list of things (such as event callbacks) where you need to call all of them, even if one handler fails, but you can't afford to silence the errors either. Code that deals with that scenario well is a bitch to write, and a looping 'with' would make it a bit easier to write once and reuse many.

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to