这个问题来自 catch errors within generator and continue afterwards
我有大约 50 个类似(但不同)的函数,它们试图从网站中提取 URL 等。因为每个网站都不同,每个功能都不同,而且因为网站往往会随着时间的推移而改变,所以这个代码是困惑的并且不可信任。
这里有一个简化的例子,还是看第一题的例子
def _get_units(self):
for list1 in self.get_list1():
for list2 in self.get_list2(list1):
for unit in list2:
yield unit
我想用这个函数做的基本上是改变行为来匹配这个:
def _get_units(self):
for list1 in self.get_list1():
try:
for list2 in self.get_list2(list1):
try:
for unit in list2:
try:
yield unit
except Exception as e:
log_exception(e)
except Exception as e:
log_exception(e)
except Exception as e:
log_exception(e)
总之,我要转这个
for x in list:
do_stuff(x)
为此:
for x in list:
try:
do_stuff(x)
except Exception as e:
log_exception(e)
对于我函数中的每个 for
。
但我想用 pythonic 的方式来做。我不希望 try:except
block 分散在我需要更改的 50 个函数中。这可能吗?如果是这样,我怎样才能以最 DRY 的方式做到这一点,我能否通过在一个地方进行错误处理来做到这一点?
更新:这个问题以前包含一个continue
语句和日志记录,但正如 mgilson 指出的那样,这不是必需的。
UPDATE 2 使用 georgesl 的回答函数变成如下:
from contextlib import contextmanager
@contextmanager
def ErrorManaged():
try:
yield
except Exception as e:
log_exception(e)
def _get_units(self):
for list1 in self.get_list1():
with ErrorManaged():
for list2 in self.get_list2(list1):
with ErrorManaged():
for unit in list2:
with ErrorManaged():
yield unit
这确实干净多了。不过,仅仅装饰器会更好。谁能告诉我这是否可能?如果没有,我会接受 georgesl 的回答。
最佳答案
你可能想使用装饰器或更好的,the context manager :
from contextlib import contextmanager
def HandleError(func):
def wrapped(*args, **kwargs):
try:
func(*args, **kwargs)
except Exception:
print "Bug on node #", args[0]
return wrapped
@contextmanager
def ErrorManaged():
try:
yield
except Exception:
print "Oh noes, the loop crashed"
@HandleError
def do_something(x):
print x
if x==5:
raise('Boom !')
with ErrorManaged():
for x in range(10):
do_something(x)
if x == 7 :
raise('aaaah !')
关于python - 更改函数中的每个 for 循环,以便在每次失败的迭代后自动执行错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13648200/