On Mar 8, 10:31 am, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Thu, 08 Mar 2007 06:17:37 -0300, Gerard Flanagan
> <[EMAIL PROTECTED]> escribió:
>
> > @onfail(False)
> > def a(x):
> >     if x == 1:
> >         return 'function a succeeded'
> >     else:
> >         raise
>
> I know it's irrelevant, as you use a bare except, but such raise looks a
> bit ugly...
>
> --
> Gabriel Genellina

Agreed. I thought a 'gentle reader' could have filled in the blanks,
but I suppose I should have taken the time to put in a custom
exception.

Another version:

import exceptions

class ABCException(exceptions.Exception):
    pass

class DoItException(exceptions.Exception):
    pass

def onfailFalse(fn):
    def inner(*args, **kwargs):
        try:
            return fn(*args, **kwargs)
        except ABCException:
            return False
    return inner

@onfailFalse
def a(x):
    if x == 1:
        return 'function a succeeded'
    else:
        raise ABCException()

@onfailFalse
def b(x):
    if x == 2:
        return 'function b succeeded'
    else:
        raise ABCException()

@onfailFalse
def c(x):
    if x == 3:
        return 'function c succeeded'
    else:
        raise ABCException()

def doit(x):
    for f in [a, b, c]:
        result = f(x)
        if result:
            return result
    raise DoItException()

print doit(1)
print doit(2)
print doit(3)
print doit(4)

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to