简体   繁体   English

如何使此功能在Python上更有效?

[英]How can I make this function more Pythonically efficient?

I am working on a script for an automated workflow. 我正在为自动化工作流程编写脚本。 It expects a CSV and a disk image to be present in the directory that is supplied as args.input. 它期望在args.input提供的目录中存在CSV和磁盘映像。

I want to check for and handle every possible scenario: no CSV, no disk image, too many CSVs, too many disk images, and any possible combination thereto. 我想检查并处理所有可能的情况:没有CSV,没有磁盘映像,太多CSV,太多磁盘映像以及它们的任何可能组合。

I've written the below, which works, and seems to be human readable, but it just seems incredibly excessive and verbose – is there any way I can make this more compact, yet still retain its legibility? 我写了下面的文章,该文章行之有效,似乎易于理解,但似乎太过冗长和冗长了–我有什么办法可以使它更紧凑,同时又保持其可读性?

# CONFORMANCE CHECKS
def check_conformance():
    csv = glob.glob(args.input+'*.csv')
    disk_image = glob.glob(args.input+'*.E01')

    if len(csv) == 1:
        does_csv_exist = os.path.isfile(csv[0])
    elif len(csv) < 1:
        does_csv_exist = False
    elif len(csv) > 1:
        does_csv_exist = "too many CSVs!"
    if len(disk_image) == 1:
        does_E01_exist = os.path.isfile(disk_image[0])
    elif len(disk_image) < 1:
        does_E01_exist = False
    elif len(disk_image) > 1:
        does_E01_exist = "too many Disk Images!"
    if len(disk_image) > 1 and len(csv) > 1:
        does_csv_exist = "too many CSVs!"
        does_E01_exist = "too many disk images!"
        return (False, does_csv_exist, does_E01_exist,)
    if does_E01_exist is True and does_csv_exist is True:
        return True
    elif does_E01_exist is True and does_csv_exist is False:
        return (False, "CSV is missing")
    elif does_E01_exist is False and does_csv_exist is True:
        return (False, "E01 disk image is missing")
    elif does_E01_exist is False and does_csv_exist is False:
        return (False, "E01 disk image AND csv are missing")
    elif does_csv_exist is not True and does_csv_exist is not False:
        return (False, does_csv_exist)
    elif does_E01_exist is not True and does_E01_exist is not False:
        return (False, does_E01_exist)

I'm not sure exactly what the purpose of this function is, but here are a few tips: 我不确定该功能的目的是什么,但是这里有一些提示:

  • A function should have just one function. 一个功能应该只有一个功能。 Yours seems to have multiple - return whether or not the input conforms to your standards ( True/False ) and return some sort of error string ( str ). 您的似乎有多个-返回输入是否符合您的标准( True/False ), 返回某种错误字符串( str )。 You are returning tuples that combine these two things in unpredictable ways. 您将返回以不可预测的方式将这两件事结合在一起的元组。 Either pick one or the other, or standardize the tuple and always return the same exact one (ie (bool, str) ) 要么选择一个,要么标准化元组,并始终返回相同的精确元组(即(bool, str)

  • Even though you can set multiple different types to the same variable, you shouldn't. 即使您可以为同一变量设置多个不同的类型,也不应该这样做。 Don't set a Boolean in one condition, and then a string in another condition (see: does_csv_exist ) 不要在一个条件下设置布尔值,然后在另一个条件下设置字符串(请参阅: does_csv_exist

I would have done something like this: 我会做这样的事情:

# CONFORMANCE CHECKS
# Returns a list of error strings encountered, empty list if OK
def getConformanceErrors():
    csv = glob.glob(args.input+'*.csv')
    disk_image = glob.glob(args.input+'*.E01')

    msg = []

    if len(csv) < 1:
        msg.append("CSV is missing")
    elif len(csv) > 1:
        msg.append("Too many CSVs")  

    if len(disk_image) < 1:
        msg.append("Disk Image is missing")
    elif len(disk_image) > 1:
        msg.append("Too many Disk Images") 

    return msg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM