Python try-except块的DRY方法?

时间:2016-07-20 18:45:32

标签: python python-2.7 try-except

目标:我有几行代码,每行都能产生相同类型的错误,并保证同样的响应。如何使用try-except块防止“不要重复”问题。

背景

我使用ReGex从文本文件中抓取格式不佳的数据,并将其输入到自定义对象的字段中。代码工作得很好,除非字段留空,在这种情况下它会抛出错误。

我在try-except块中处理此错误。如果出错,请在对象的字段中插入一个空白(即'')。

问题是它变得易于阅读,很好,Python代码变成了一堆乱七八糟的块,每个块都做同样的事情。这是一个'不要重复自己'(a.k.a.DRY)违规行为。

守则:

之前:

sample.thickness = find_field('Thickness', sample_datum)[0]
sample.max_tension = find_field('Maximum Load', sample_datum)[0]
sample.max_length = find_field('Maximum Extension', sample_datum)[0]
sample.test_type = sample_test

后:

try:
    sample.thickness = find_field('Thickness', sample_datum)[0]
except:
    sample.thickness = ''

try:
    sample.max_tension = find_field('Maximum Load', sample_datum)[0]
except:
    sample.max_tension = ''

try:
    sample.max_length = find_field('Maximum Extension', sample_datum)[0]
except: 
    sample.max_length = ''

try:    
    sample.test_type = sample_test
except:
    sample.test_type = ''

我需要的是什么:

是否有一些Pythonic方式来写这个?我可以说,如果这些行中有任何索引超出范围错误(表示该字段为空,ReGex未能返回任何内容),则会在示例字段中插入一个空白。

5 个答案:

答案 0 :(得分:6)

如何重构函数呢?

def maybe_find_field(name, datum):
    try:
        return find_field(name, datum)[0]
    except IndexError: # Example of specific exception to catch
        return ''

sample.thickness = maybe_find_field('Thickness', sample_datum)
sample.max_tension = maybe_find_field('Maximum Load', sample_datum)
sample.max_length = maybe_find_field('Maximum Extension', sample_datum)
sample.test_type = sample_test
顺便说一句,不要只用except:捕捉所有可能的例外情况,除非这真的是你想做的事情。捕获所有内容可能会隐藏一些实现错误,以后很难调试。只要有可能,就将except案例绑定到您需要的特定例外。

答案 1 :(得分:1)

当您发现自己重复代码时,将其封装在一个函数中。在这种情况下,创建一个为您处理异常的函数。

def try_find_field(field_name, datum, default_value):
    try:
        return find_field(field_name, datum)[0]
    except:
        return default_value

答案 2 :(得分:0)

您可以反复使用任意数量的except块,处理不同类型的异常。在同一个try / catch块中有多个语句也没什么问题。

try:
    doMyDangerousThing()
except ValueError:
    print "ValueError!"
except HurrDurrError:
    print "hurr durr, there's an error"

try:
    doMyDangerousThing()
    doMySecondDangerousThing()
except:
    print "Something went wrong!"

答案 3 :(得分:0)

那这样的事情呢?

def exception_handler(exception_class):
    logger = logging.getLogger('app_error')
    logger.error(exception_class)

    exception_name = exception_class.__name__

    if exception_name == 'AuthenticationError':
        raise AuthenticationError
    elif exception_name == 'AuthorizationError':
        raise AuthorizationError
    elif exception_name == 'ConnectionError':
        raise ConnectionError
    else:
        raise GenericError

def call_external_api()
    try:
        result = http_request()
    except Exception as e:
        exception_handler(exception_class=type(e))

答案 4 :(得分:0)

这个问题不太符合要求,但是Google将我发送到这里,所以其他人可能会来。

我在两个单独的Django视图中有post个函数,它们调用相似的后端函数,并且需要相同的异常处理。

我通过提取整个except:-树并将其粘贴到装饰器中来解决了这个问题。

之前:

# twice this
def post(request):
    try:
        return backend_function(request.post)
    except ProblemA as e:
        return Response("Problem A has occurred, try this to fix it.", status=400)
    except ProblemB as e:
        return Response("Problem B has occurred, try this to fix it.", status=400)
    except ProblemC as e:
        return Response("Problem C has occurred, try this to fix it.", status=400)
    except ProblemD as e:
        return Response("Problem D has occurred, try this to fix it.", status=400)

之后:

# once this
def catch_all_those_pesky_exceptions(original_function):
    def decorated(*args, **kwargs):
        try:
            return original_function(*args, **kwargs)
        except ProblemA as e:
            return Response("Problem A has occurred, try this to fix it.", status=400)
        except ProblemB as e:
            return Response("Problem B has occurred, try this to fix it.", status=400)
        except ProblemC as e:
            return Response("Problem C has occurred, try this to fix it.", status=400)
        except ProblemD as e:
            return Response("Problem D has occurred, try this to fix it.", status=400)

    return decorated

# twice this - note the @decorator matching the above function.
@catch_all_those_pesky_exceptions
def post(request):
    return backend_function(request.post)

现在我可以在一个地方添加更多的异常处理。