Alf*_*lfe 17 python nested exception try-except
我有几个可能保存我的数据的文件; 它们可以以不同的方式压缩,因此要打开它们我需要使用file(),gzip.GzipFile()而其他也返回文件对象(支持with接口).
我想尝试每一个,直到一个成功开放,所以我可以做类似的事情
try:
with gzip.GzipFile(fn + '.gz') as f:
result = process(f)
except (IOError, MaybeSomeGzipExceptions):
try:
with xCompressLib.xCompressFile(fn + '.x') as f:
result = process(f)
except (IOError, MaybeSomeXCompressExceptions):
try:
with file(fn) as f:
result = process(f)
except IOError:
result = "some default value"
Run Code Online (Sandbox Code Playgroud)
如果我有几十个可能的压缩变体,这显然是不可行的.(嵌套将越来越深,代码看起来总是非常相似.)
有没有更好的拼写方法?
编辑:如果可能的话,我希望process(f)除了try/except以外,以避免意外捕获在中提出的异常process(f).
是的,您可以将所有变体放在列表中并尝试它们直到其中一个工作,从而取消嵌套代码:
def process_gzip(fn):
with gzip.GzipFile(fn + '.gz') as f:
return process(f)
def process_xlib(fn):
with xCompressLib.xCompressFile(fn + '.x') as f:
return process(f)
def process_builtin(fn):
with file(fn) as f:
return process(f)
process_funcs = [process_gzip, process_xlib, process_builtin]
#processing code:
for process_f in process_funcs:
try:
result = process_f(fn)
break
except IOError:
#error reading the file, keep going
continue
except:
#processing error, re-raise the exception
raise
Run Code Online (Sandbox Code Playgroud)
或者,为了减少代码量,您可以创建一个process_func工厂,因为它们都具有相同的形式:
def make_process_func(constructor, filename_transform):
with constructor(filename_transform) as f:
return process(f)
process_funcs = [
make_process_func(gzip.GzipFile, lambda fn: fn + '.gz'),
make_process_func(xCompressLib.xCompressFile, lambda fn: fn + '.x'),
make_process_func(file, lambda fn: fn),
]
Run Code Online (Sandbox Code Playgroud)
我写了一个自定义上下文管理器:
from contextlib import contextmanager
filetypes = [('.gz', gzip.GzipFile, (IOError, MaybeSomeGzipExceptions)),
('.x', xCompressLib.xCompressFile, (IOError, MaybeSomeXCompressExceptions))]
@contextmanager
def open_compressed(fn):
f = None
try:
for ext, cls, exs in filetypes:
try:
f = cls(fn + ext)
except exs:
pass
else:
break
yield f
finally:
if f is not None:
f.close()
with open_compressed(fn) as f:
result = "some default value" if f is None else process(f)
Run Code Online (Sandbox Code Playgroud)
或者可能只是一个返回上下文管理器的函数:
filetypes = [('.gz', gzip.GzipFile, (IOError, MaybeSomeGzipExceptions)),
('.x', xCompressLib.xCompressFile, (IOError, MaybeSomeXCompressExceptions))]
class UnknownCompressionFormat(Exception):
pass
def open_compressed(fn):
for ext, cls, exs in filetypes:
try:
return cls(fn + ext)
except exs:
pass
raise UnknownCompressionFormat
try:
with open_compressed(fn) as f:
result = process(f)
except UnknownCompressionFormat:
result = "some default value"
Run Code Online (Sandbox Code Playgroud)
这会工作:
extensions = [('.gz', gzip.GzipFile, (IOError, MaybeSomeGzipExceptions)),
('.x', xCompressLib.xCompressFile, (IOError, MaybeSomeXCompressExceptions))] # and other such entries
processed = False
for ext, (compressor, errors) in extensions.iteritems():
try:
with compressor(fn+ext) as f:
try:
result = process(f)
processed = True
break
except:
raise
except errors:
pass
if not processed:
result = "some default value"
Run Code Online (Sandbox Code Playgroud)
希望有所帮助
| 归档时间: |
|
| 查看次数: |
299 次 |
| 最近记录: |