Контроль исключений Python / Выпуск потока - PullRequest
3 голосов
/ 15 ноября 2011

Я работал в Python и столкнулся с чем-то, что должно быть обычным явлением. У меня есть пять утверждений, которые все попадают в общую ловушку FooException и BarException. Я хочу запустить каждого из них, защищая от эти исключения, но продолжают обрабатывать, даже если исключение возникает после некоторая обработка сделана. Теперь я мог бы сделать это так:

try:
    foo()
except (FooException, BarException):
    pass
try:
    bar()
except (FooException, BarException):
    pass
try:
    baz()
except (FooException, BarException):
    pass
try:
    spam()
except (FooException, BarException):
    pass
try:
    eggs()
except (FooException, BarException):
    pass

но это действительно многословно и в крайнем нарушении СУХОГО. Довольно грубая сила и очевидным решением является что-то вроде этого:

def wish_i_had_macros_for_this(statements, exceptions, gd, ld):                
    """ execute statements inside try/except handling exceptions with gd and ld
    as global dictionary and local dictionary

    statements is a list of strings to be executed as statements
    exceptions is a list of strings that resolve to Exceptions
    gd is a globals() context dictionary
    ld is a locals() context dictionary

    a list containing None or an Exception if an exception that wasn't 
    guarded against was raised during execution of the statement for each
    statement is returned
    """
    s = """ 
try:    
    $STATEMENT
except (%s):
    pass
""" % ','.join(exceptions)                                                     
    t = string.Template(s)
    code = [t.substitute({'STATEMENT': s}) for s in statements]                   
    elist = list() 
    for c in code:
        try:
            exec c in gd, ld
            elist.append(None)
        except Exception, e:
            elist.append(e)
    return elist

С использованием в соответствии с:

>>> results = wish_i_had_macros_for_this(
                ['foo()','bar()','baz','spam()','eggs()'],
                ['FooException','BarException'],
                globals(),
                locals())
[None,None,None,SpamException,None]

Есть ли лучший способ?

Ответы [ 3 ]

4 голосов
/ 15 ноября 2011
def execute_silently(fn, exceptions = (FooException, BarException)):
    try:
        fn()
    except Exception as e:
        if not isinstance(e, exceptions):
            raise

execute_silently(foo)
execute_silently(bar)
# ...
# or even:
for fn in (foo, bar, ...):
    execute_silently(fn)
3 голосов
/ 15 ноября 2011

Как насчет этого?

 #!/usr/bin/env python

 def foo():
     print "foo"

 def bar():
     print "bar"

 def baz():
     print "baz"

 for f in [foo, bar, baz]:
     try:
         f()
     except (FooException, BarException):
         pass 
0 голосов
/ 15 ноября 2011

Эта версия также позволяет выполнять операторы:

from contextlib import contextmanager
from functools import partial

@contextmanager
def exec_silent(exc=(StandardError,)):
    try:
        yield
    except exc:
        pass

silent_foobar = partial(exec_silent, (FooException, BarException))

with silent_foobar():
    print 'foo'
    foo()
with silent_foobar():
    print 'bar'
    bar()
...