如何基于python中给出的命令行输入运行某些功能 [英] How to run certain function based on command line input given in python
问题描述
我有我的主脚本,其中定义了两个函数. or_search将查找出现的指定字符串,并将找到该字符串的索引位置添加到列表中.
I have my main script where i have two functions defined. The or_search will find occurrences of a string specified and add to the list what index position it has been found within.
第二个函数and_search查找指定字符串的出现,并使用一个计数器来增加找到它的次数.在我的主要函数中,如果我传递了python main.py和search佣金,item,sold,则它应该运行and_search函数并返回结果.它也应该使用orsearch来做到这一点.在命令行上运行时,似乎在终端上什么也没打印出来.我不确定我做错了什么.我的脚本如下:
The second function and_search finds occurrences of a string specified and a counter is used to increment the amount of times it has been found. In my main function , if i pass for example python main.py andsearch commission , item , sold , it should run the and_search function and bring back the results. It should do this with orsearch as well. When running on the command line , it seems to print back nothing on the terminal. I am not sure what it is that i am doing wrong. My script is as follows :
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
五次,但只需调用一次即可.只需将结果传递给您调用的函数即可.也许像这样:
You are calling get_config
a total of five times, but you only need to call it once. Just pass on the result to the functions you call. Perhaps like this:
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原则.
Probably a lot more of your code should be refactored to avoid repetitions. If you find a bug, you don't want to have to remember to fix it in two or more places in your code. See also DRY Principle.
这篇关于如何基于python中给出的命令行输入运行某些功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!