如何在python中将此代码翻译为更具可读性?

时间:2019-05-04 14:53:35

标签: python refactoring translate

我想将此代码翻译为更具可读性,但是当我尝试时,它不起作用。它可以在原始版本中使用,但不能在第二个版本中使用。

我以此方式尝试过。

down, up = [(+1, -1), (+1, +1)], [(-1, -1), (-1, +1)]
length = board.get_length()
piece = board.get(row, col)
if piece:
    for (x, y) in down:
        if (0 <= (row + x) < length) and (0 <= (col + y) < length) and board.is_free(row + x, col + y):
            bottom = [main.deindexify(row + x, col + y)]

    # bottom = [main.deindexify(row + x, col + y) for (x, y) in down \
    #           if (0 <= (row + x) < length)
    #           and (0 <= (col + y) < length) \
    #           and board.is_free(row + x, col + y)]

    for (x, y) in up:
        if (0 <= (row + x) < length) and (0 <= (col + y) < length) and board.is_free(row + x, col + y):
            top = [main.deindexify(row + x, col + y)]

    # top = [main.deindexify(row + x, col + y) for (x, y) in up \
    #        if (0 <= (row + x) < length) \
    #        and (0 <= (col + y) < length) \
    #        and board.is_free(row + x, col + y)]

            if piece.is_king():
        return sorted(bottom + top)
    else:
        if piece.is_black():
            return sorted(bottom)
        else:
            if is_sorted:
                return sorted(top)
            else:
                if piece.is_king():
                    return bottom + top
                else:
                    if piece.is_black():
                        return bottom
                    else:
                        return top

    # return (sorted(bottom + top) if piece.is_king() else \
    #             (sorted(bottom) if piece.is_black() else sorted(top))) \
    #     if is_sorted else (bottom + top if piece.is_king() else \
    #                            (bottom if piece.is_black() else top))
return []

这是一个错误:

line 61, in get_moves
return sorted(bottom)
UnboundLocalError: local variable 'bottom' referenced before assignment

这是原始版本。

 down, up = [(+1, -1), (+1, +1)], [(-1, -1), (-1, +1)]
length = board.get_length()
piece = board.get(row, col)
if piece:
    bottom = [main.deindexify(row + x, col + y) for (x, y) in down \
              if (0 <= (row + x) < length) \
              and (0 <= (col + y) < length) \
              and board.is_free(row + x, col + y)]
    top = [main.deindexify(row + x, col + y) for (x, y) in up \
           if (0 <= (row + x) < length) \
           and (0 <= (col + y) < length) \
           and board.is_free(row + x, col + y)]
    return (sorted(bottom + top) if piece.is_king() else \
                (sorted(bottom) if piece.is_black() else sorted(top))) \
        if is_sorted else (bottom + top if piece.is_king() else \
                               (bottom if piece.is_black() else top))
return []

有人可以解释我如何阅读和翻译代码,以便我可以重构其余代码吗?

1 个答案:

答案 0 :(得分:0)

如果xy满足某些条件,则下面的代码段(从原始代码开始)将创建一个列表。它被编写为列表理解。

....
    bottom = [main.deindexify(row + x, col + y) for (x, y) in down \
              if (0 <= (row + x) < length) \
              and (0 <= (col + y) < length) \
              and board.is_free(row + x, col + y)]

展开列表理解(重构)为for循环的一种方法可能是:

....
    bottom = []
    for (x,y) in down:
        x_length = (0 <= (row + x) < length)
        y_length = (0 <= (col + y) < length)
        cel_isfree = board.is_free(row + x, col + y)
        if x_length and ylength and cel_isfree:
            bottom.append(main.deindexify(row + x, col + y))

top也可以通过这种方式重构。

下面的原始代码段基于以下两个条件确定返回值: piece 的类型以及是否排序。它被写为嵌套的conditional expression

....
    return (sorted(bottom + top) if piece.is_king() else \
                (sorted(bottom) if piece.is_black() else sorted(top))) \
        if is_sorted else (bottom + top if piece.is_king() else \
                               (bottom if piece.is_black() else top))

可以这样重构:

....
    if is_sorted:
        if piece.is_king():
            return_value = sorted(bottom + top)
        elif piece.is_black():
            return_value = sorted(bottom)
        else:
            return_value = sorted(top)
    else:
        if piece.is_king():
            return_value = bottom + top
        elif piece.is_black():
            return_value = bottom
        else:
            return_value = top
    return return_value

当然,我没有一种简单的方法来进行测试。