【问题标题】:How to run certain function based on command line input given in python如何根据python中给出的命令行输入运行某些功能
【发布时间】:2019-07-11 04:14:27
【问题描述】:

我有我的主脚本,其中定义了两个函数。 or_search 将查找指定字符串的出现并将其添加到列表中找到它的索引位置。

第二个函数 and_search 查找指定字符串的出现次数,并使用一个计数器来增加它被找到的次数。在我的 main 函数中,如果我通过例如 python main.py andsearch Commission , item , sold ,它应该运行 and_search 函数并带回结果。它也应该使用 orsearch 来做到这一点。在命令行上运行时,它似乎不会在终端上打印任何内容。我不确定我做错了什么。我的脚本如下:

import argparse


def get_config():
    parser = argparse.ArgumentParser(description='Search your keyword ex: querycheck.py andsearch general,population,Alzheimer')
    parser.add_argument('searchtype', type=str, help='Search type orsearch and andsearch only ')
    parser.add_argument('Value', type=str, help='Parameter to search')
    args = parser.parse_args()
    return args.searchtype, args.Value

finallist = []
counter = 0


def or_search(get_config):
    search_type, value = get_config()

    if search_type == "orsearch":
        value_split = value.split(",")
        with open("hscic-news", "r") as file:
            file_content = file.readlines()
            for x in range(len(file_content)):
                for y in value_split:
                    if y in file_content[x]:
                        finallist.append(x)


        list_with_duplicates = list(set(finallist))
        final_list = list(set(finallist))
        result = final_list
        print(result)

    else:
            print ("Please enter only or/and for search type ")
            exit(1)


#

def and_search(get_config):
    search_type, value = get_config()
    if search_type == "andsearch" :
        value_split = value.split(",")
        with open("hscic-news", "r") as newsfile:
            ncontent = newsfile.readlines()
            for x in range(len(ncontent)):
                for y in value_split:
                    if y in ncontent[x]:
                        counter += 1
                    else:
                        counter = 0
                    if counter == len(value_split) :

                       finallist.append(x)

        final_list = list(set(finallist))
        result = final_list
        print(result)
    #
    #
    else:
            print ("Please enter only or/and for search type ")
            exit(1)



if __name__ == '__main__':

    search_type = get_config()
    if search_type == "orsearch":
        or_search(get_config())
    elif search_type == "andsearch":
        and_search(get_config())

【问题讨论】:

  • 您拨打get_config 的次数过多。在__main__ 中调用它一次,并使用它返回的 2 个值,传递它们,而不是 get_config 你的函数。
  • 你能提供一个你所说的例子吗?有点困惑对不起@hpaulj

标签: python python-2.7 command-line-arguments argparse


【解决方案1】:

您一共拨打了五次get_config,但您只需拨打一次。只需将结果传递给您调用的函数。可能是这样的:

def or_search(value):
    value_split = value.split(",")
    # ...

def and_search(value):
    value_split = value.split(",")
    # ...

if __name__ == '__main__':
    search_type, value = get_config()
    if search_type == "orsearch":
        or_search(value)
    elif search_type == "andsearch":
        and_search(value)

可能应该重构更多代码以避免重复。如果您发现了一个错误,您不必记住在代码中的两个或多个位置进行修复。另见DRY Principle.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 2020-06-12
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多