ANN: withrestart 0.2.1

Discussion in 'Python' started by Ryan Kelly, Dec 18, 2009.

  1. Ryan Kelly

    Ryan Kelly Guest

    Hi All,

    Apologies if you receive multiple copies of this, my python-announce
    posts don't seem to be making it through.

    I've just released a new python module called "withrestart". It's an
    attempted Pythonisation of the restart-based condition system of Common
    Lisp. Details are on PyPI:

    For an introduction to conditions and restarts, see "Beyond Exception
    Handling" by Peter Seibel:

    For a quick demo of the module in action, keep reading...



    About withrestart:

    Version: 0.2.1
    Licence: MIT

    withrestart is a Pythonisation (Lispers might rightly say "bastardisation") of
    the restart-based condition system of Common Lisp. It's designed to make error
    recovery simpler and easier by removing the assumption that unhandled errors
    must be fatal.

    A "restart" represents a named strategy for resuming execution of a function
    after the occurrence of an error. At any point during its execution a
    function can push a Restart object onto its call stack. If an exception
    occurs within the scope of that Restart, code higher-up in the call chain can
    invoke it to recover from the error and let the function continue execution..
    By providing several restarts, functions can offer several different strategies
    for recovering from errors.

    A "handler" represents a higher-level strategy for dealing with the occurrence
    of an error. It is conceptually similar to an "except" clause, in that one
    establishes a suite of Handler objects to be invoked if an error occurs during
    the execution of some code. There is, however, a crucial difference: handlers
    are executed without unwinding the call stack. They thus have the opportunity
    to take corrective action and then resume execution of whatever function
    raised the error.

    As an example, here's a function that doesn't like the number seven:

    def anything_but_seven(v):
    if v == 7:
    raise ValueError("Argh! A Seven!")
    return v

    And here's a function that can recover from the occurrence of a seven
    using the pre-defined restarts "skip" and "use_value":

    def sum_items(items):
    total = 0
    for i in items:
    with restarts(skip,use_value) as invoke:
    total += invoke(anything_but_seven,i)
    return total

    Naively calling this will raise a ValueError:

    >>> sum_items(range(8))

    Traceback (most recent call last):
    ValueError: Argh! A Seven!

    But if we handle ValueErrors by invoking the "skip" restart, we can
    still get the sum of the remaining items:

    >>> with Handler(ValueError,"skip"):

    ... sum_items(range(8))

    Alternately, we can invoke the "use_value" restart to replace the sevens
    with another value:

    >>> with Handler(ValueError,"use_value",12):

    ... sum_items(range(8))

    By splitting the responsibility for error recovery between Handlers and
    Restarts, we can cleanly separate the low-level mechanics of recovering
    from an error from the high-level decisions about what sort of recovery
    to perform.

    Ryan Kelly | This message is digitally signed. Please visit
    | for details

    Version: GnuPG v1.4.9 (GNU/Linux)

    -----END PGP SIGNATURE-----
    Ryan Kelly, Dec 18, 2009
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Sampson [MSFT]

    [ANN]: NNTP Server slow downs.

    Mike Sampson [MSFT], Oct 7, 2003, in forum: ASP .Net
    Mike Sampson [MSFT]
    Oct 7, 2003
  2. Mike Sampson [MSFT]

    [ANN]: NNTP Server slow downs.

    Mike Sampson [MSFT], Dec 6, 2003, in forum: ASP .Net
    Mike Sampson [MSFT]
    Dec 6, 2003
  3. Richard Grimes [MVP]

    ANN: Free .NET Workshops

    Richard Grimes [MVP], Jul 4, 2005, in forum: ASP .Net
    Richard Grimes [MVP]
    Jul 4, 2005
  4. Tom Hawkins

    [ANN] Confluence 0.7.1 Released

    Tom Hawkins, Oct 23, 2003, in forum: VHDL
    Tom Hawkins
    Oct 23, 2003
  5. Michael Livsey
    Michael Livsey
    May 27, 2004

Share This Page