重构更长的“一行” if-condition-assignment(三元If)的Python方法

时间:2019-03-01 12:30:03

标签: python if-statement refactoring

我当前的代码使用时间分配One line if-condition-assignment,但是使用更多详细的标识符,它很容易通过行长限制。

由于我还没有进行pythonic编码,所以很高兴看到一些重构建议。

for label in range(num_labels):
    d_tresh = drop_treshold[label] if type(drop_treshold) == numpy.ndarray) else drop_treshold     
    r_tresh1 = relabel_treshold1[label] if type(relabel_treshold1) == numpy.ndarray else relabel_treshold1
    r_tresh2 = relabel_treshold2[label] if type(relabel_treshold2) == numpy.ndarray else relabel_treshold2

使用局部短变量名似乎是解决方案的一部分,但我希望有更多说明性的函数参数。嗯因此,大幅缩短函数参数名称会导致(对我而言)无法读取的代码。

for l in range(n_labels):
    t0 = d_t[l] if type(d_t) == numpy.ndarray) else d_t     
    t1 = r_t1[l] if type(r_t1) == numpy.ndarray else r_t1
    t2 = r_t2[l] if type(r_t2) == numpy.ndarray else r_t1

那么,如果要分配其他行,我应该求助于多行吗?它将大大扩展和膨胀简单的逻辑。

for label in range(num_labels):
    if type(drop_treshold) == numpy.ndarray):
        d_tresh = drop_treshold[label]
    else:
        d_tresh = drop_treshold     

    if type(relabel_treshold1) == numpy.ndarray:
        r_tresh1 = relabel_treshold1[label] 
    else:
        d_tresh = relabel_treshold1

    if type(relabel_treshold2) == numpy.ndarray:
        r_tresh2 = relabel_treshold2[label] 
    else:
        d_tresh = relabel_treshold2

(当然,我(sh /)也可以重构所示示例的整个代码...此示例代码段来自带有参数的函数,可以是标量float / int或一维numpy.array。如果它是数组)会将每个项目应用于每个标签,只是简单的标量,它将全局应用于所有标签) 但是,这又是 pythonic方式的方式吗?什么时候开始更彻底地重构,什么时候呆在原地-因为它起作用了?

1 个答案:

答案 0 :(得分:1)

这里的问题是您要为每个变量重复一次;如果对象是数组,则建立索引,否则直接使用对象。我会在这里使用实用程序功能:

def threshold_label(ob, i):
    # the threshold label can be a scalar or an array
    return ob[i] if isinstance(ob, numpy.ndarray) else ob

for label in range(num_labels):
    d_tresh = threshold(drop_treshold, label)
    r_tresh1 = threshold(relabel_treshold1, label)
    r_tresh2 = threshold(relabel_threshold2, label)

请注意,我使用isinstance()测试每个对象的类型,请参见What are the differences between type() and isinstance()?

另一个选择是不每次迭代测试数组。测试一次,如果它不是数组,则将标量float或int值转换为预期长度的序列:

# turn scalar labels into sequences for easy iteration
if not isinstance(drop_threshold, numpy.ndarray):
    drop_threshold = [drop_threshold] * num_labels
if not isinstance(relabel_treshold1, numpy.ndarray):
    relabel_treshold1 = [relabel_treshold1] * num_labels
if not isinstance(drop_threshold, numpy.ndarray):
    relabel_treshold2 = [relabel_treshold2] * num_labels

,或者再次使用辅助功能:

def ensure_sequence(ob, cnt):
    # turn a scalar label value into a sequence if needed
    return [ob] * cnt if not isinstance(ob, numpy.ndarray) else ob

drop_threshold = ensure_sequence(drop_threshold, num_labels)
relabel_treshold1 = ensure_sequence(relabel_treshold1, num_labels)
relabel_treshold2 = ensure_sequence(relabel_treshold2, num_labels)

此时可以使用zip()进行迭代:

labels = zip(drop_threshold, relabel_threshold1, relabel_threshold2)
for d_thresh, r_thresh1, r_thresh2 in labels:
    # ...