在python函数中实现不同模式的最佳方法

2024-04-25 15:29:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道实现一个可以在不同模式下运行的函数的最python的方法,即根据调用它的模式执行稍微不同的任务。你知道吗

例如,我有以下步骤可以基于一组输入索引提取numpy数组的子集。通常我希望以[xMin, xMax, yMin, yMax]形式的列表或元组形式传入这些索引,但是有时我可能希望提供一个中心点和一个宽度/高度,如([xCoord, yCoord], [width, height])。(请注意,这是空间数据xMinyMin等。请参阅数组边界的空间坐标。)

def get_subset(array, *args):
    ## If window extents are given
    if len(args) == 1:
        [(xMin,xMax,yMin,yMax)] = args

        ## Set NULL extents to array bounds
        if xMin is None: xMin = array.xMin
        if xMax is None: xMax = array.xMax
        if yMin is None: yMin = array.yMin
        if yMax is None: yMax = array.yMax

        ## Convert window extents to array indices
        winLx = int(xMin - array.xMin)
        winRx = int(xMax - array.xMin)
        winBy = int(array.yMax - yMin)
        winTy = int(array.yMax - yMax)

    ## If window centroid and dimensions are given
    elif len(args) == 2:
        [(easting,northing),(width,height)] = args

        # Convert input coordinates into array indices
        xCell = int(easting - array.xMin)
        yCell = int(array.yMax - northing)

        # Generate L(eft), R(ight), T(op) and B(ottom) window extents
        winLx = xCell - width//2
        winRx = xCell + width//2
        winBy = yCell + height//2
        winTy = yCell - height//2

    subset = array.data[winTy:winBy, winLx:winRx, :]

有没有一个更好的,更简洁或更python的方法来做到这一点?在过去,我曾尝试对我的函数使用mode参数,然后使用if循环来获得我想要的功能(类似于get_subset(array, window, mode='extent')),结果与我上面所展示的没有太大区别。不过,我想知道是否有一种很好的方法可以使用decorator或其他一些python功能来实现这一点。你知道吗


Tags: 方法noneifisargswindowwidtharray
1条回答
网友
1楼 · 发布于 2024-04-25 15:29:47

选项A) 你把函数分成两个函数。你知道吗

get_subset_using_extents(array, extents)
get_subset_using_centroid_and_dimension(array, centroid, dimension)

好处:

  • 直截了当
  • 似乎是你的问题评论中建议的最流行的解决方案

选项B) 你提出的选择。你知道吗

def get_subset(array, *args):

    if len(args) == 1:
        return getsubset_using_extents(array, args[0])
        #Or just lay out all of the code here like you had done

    elif len(args) == 2:
        return get_subset_using_centroid_and_dimension(array, arg[0], arg[1])
        #Or just lay out all of the code here like you had done

    else:
        raise TypeError("get_subset() takes either 2 or 3 arguments")

好处:

  • get\u子集比两个长的命名函数更容易记住。你知道吗

缺点:

  • 无法使用关键字参数。你知道吗
  • 当读取调用此函数的代码时,并不清楚使用的是哪种模式。你知道吗

选项C) 特定于模式的项存储在字典中。你知道吗

def get_subset(array, mode, options):

    if mode == "extent":
        return getsubset_using_extents(array, options["extent"])

    elif mode == "centroid_dimensions":
        return get_subset_using_centroid_and_dimension(array, options["centroid"], 
                   options["dimensions"])

    else:
        raise SubsetException("Invalid Mode: " + mode)

好处

  • 函数名很容易记住,而且因为调用者需要声明所使用的模式,所以如何获得子集仍然是显而易见的。

  • 允许您轻松添加/更改/删除选项,而无需更改函数签名。

  • 强制人们在使用您的函数时显式命名选项键。在您的案例中,这一点并不重要,但是这种技术可以用来防止类似于some_function(true, true, false, true)的函数调用。(编辑:刚刚发现你也可以做this。)


思想

方案A

因为有两种不同的模式,它们没有任何重叠的选项,所以我会选择这个选项。你知道吗

方案B

我永远不会使用这个选项。Psuedo重载这样的函数并不是一个好办法。你知道吗

方案C

在你的情况下,这并不需要,但是如果这两种模式有很多选项,其中一些在各种模式之间共享,那么这将是一个很好的模式来考虑。你知道吗

相关问题 更多 >