Python 中 CLI 脚本中的选项不起作用
Options in CLI script in Python don't work
我编写了以下命令行程序,以便提供一种自动测试我的主脚本的方法 - 一个在模块 (gui.py) 中实现的 gui 应用程序,我将其导入 CLI "testing suite"。
import argparse
from data import Document
from gui import View
parser = argparse.ArgumentParser()
parser.add_argument("-f", type=str, help="Path of the JSON file to be used.")
parser.add_argument("-u", type=str, help="User UUID.")
parser.add_argument("-d", type=str, help="Document UUID.")
parser.add_argument("-t", type=int, help="Task ID. ID's range from 2 to 5, as per coursework instructions.")
args = parser.parse_args()
doc = Document(args.f)
view = View()
if args.t == 1:
view.display_views_by_country(doc, args.d)
elif args.t == 2:
view.display_views_by_cnt(doc, args.d)
elif args.t == 3:
view.display_browser_hist(doc)
elif args.t == 4:
view.top10_window(doc)
elif args.t == 5:
view.also_likes_most_viewers_window(doc, args.u, args.d)
elif args.t == 6:
view.also_likes_timeread_window(doc, args.u, args.d)
对于 -t 参数,1 到 3 将产生所需的输出,这是每个任务 ID 的不同图形,通过 pandas 和 matplot 库实现。但是当我提供必要的参数(例如 -t = 4 的文件路径)以及其余可用选项(4 到 6)的 -t 选项时,没有任何效果。应该发生的是在新 window 上显示列表。
下面是我正在导入的视图 class 的代码以及应该实现所需任务的方法。
class View(tk.Frame):
def __init__(self, *args, **kwargs):
"""Constructor of the Frame widget, representing the initial window.
Main objective is to add the widgets through which the user will initialize
the application by entering the path of the json file to be processed.
:param args: Parent widget
:param kwargs: Config options
:return: None
"""
tk.Frame.__init__(self)
self.master.title("Document Analytics")
self.master.rowconfigure(0, weight=1)
self.master.columnconfigure(0, weight=1)
self.grid(sticky=W+E+N+S)
self.text_filepath = tk.Text(self, width=45, height=1)
self.text_filepath.grid(sticky=W+E+N+S)
self.text_filepath.insert(tk.INSERT, "Insert the path of the file to be processed")
self.text_filepath.config(state=tk.DISABLED)
self.entry_filepath = tk.Entry(self, width=20)
self.entry_filepath.grid(sticky=W+E+N+S, row=0, column=1, columnspan=3)
self.button_filepath = tk.Button(self, text="Initialize", command=self.new_window, width=25)
self.button_filepath.grid(row=1, column=1, sticky=W+E+N+S)
self.rowconfigure(1, weight=1)
self.columnconfigure(1, weight=1)
def top10_window(self, data):
"""Method which displays a list of the top 10 readers
based on reading time.
:param data: Reference to a Document object.
:return: None
"""
window = tk.Toplevel(self)
list_box = tk.Listbox(window)
list_box.pack()
for item in data.top10_readers():
list_box.insert(tk.END, item)
def also_likes_most_viewers_window(self, data, visitor_id, doc_id):
"""Method which implements the 'also likes' functionality.
Creates a new window which displays a list of top 10 relevant documents.
Sorting is based on number of viewers of each document document.
:param data: Reference to a Document object.
:param visitor_id: The visitor UUID.
:param doc_id: The document UUID.
:return: None
"""
window = tk.Toplevel(self)
list_box = tk.Listbox(window)
list_box.pack()
for item in data.also_likes_most_viewers(visitor_id, doc_id):
list_box.insert(tk.END, item)
def also_likes_timeread_window(self, data, visitor_id, doc_id):
"""Method which implements the 'also likes' functionality.
Creates a new window which displays a list of top 10 relevant documents.
Sorting is based on reading time spent on each document.
:param data: Reference to a Document object.
:param visitor_id: The visitor UUID.
:param doc_id: The document UUID.
:return: None
"""
window = tk.Toplevel(self)
list_box = tk.Listbox(window)
list_box.pack()
for item in data.also_likes_reader_profile(visitor_id, doc_id):
list_box.insert(tk.END, item)
我还应该提到,这些方法通过 GUI 可以正常工作。
任何帮助将不胜感激!
您需要调用 tkinter 主循环才能让 windows 显示操作。
我编写了以下命令行程序,以便提供一种自动测试我的主脚本的方法 - 一个在模块 (gui.py) 中实现的 gui 应用程序,我将其导入 CLI "testing suite"。
import argparse
from data import Document
from gui import View
parser = argparse.ArgumentParser()
parser.add_argument("-f", type=str, help="Path of the JSON file to be used.")
parser.add_argument("-u", type=str, help="User UUID.")
parser.add_argument("-d", type=str, help="Document UUID.")
parser.add_argument("-t", type=int, help="Task ID. ID's range from 2 to 5, as per coursework instructions.")
args = parser.parse_args()
doc = Document(args.f)
view = View()
if args.t == 1:
view.display_views_by_country(doc, args.d)
elif args.t == 2:
view.display_views_by_cnt(doc, args.d)
elif args.t == 3:
view.display_browser_hist(doc)
elif args.t == 4:
view.top10_window(doc)
elif args.t == 5:
view.also_likes_most_viewers_window(doc, args.u, args.d)
elif args.t == 6:
view.also_likes_timeread_window(doc, args.u, args.d)
对于 -t 参数,1 到 3 将产生所需的输出,这是每个任务 ID 的不同图形,通过 pandas 和 matplot 库实现。但是当我提供必要的参数(例如 -t = 4 的文件路径)以及其余可用选项(4 到 6)的 -t 选项时,没有任何效果。应该发生的是在新 window 上显示列表。 下面是我正在导入的视图 class 的代码以及应该实现所需任务的方法。
class View(tk.Frame):
def __init__(self, *args, **kwargs):
"""Constructor of the Frame widget, representing the initial window.
Main objective is to add the widgets through which the user will initialize
the application by entering the path of the json file to be processed.
:param args: Parent widget
:param kwargs: Config options
:return: None
"""
tk.Frame.__init__(self)
self.master.title("Document Analytics")
self.master.rowconfigure(0, weight=1)
self.master.columnconfigure(0, weight=1)
self.grid(sticky=W+E+N+S)
self.text_filepath = tk.Text(self, width=45, height=1)
self.text_filepath.grid(sticky=W+E+N+S)
self.text_filepath.insert(tk.INSERT, "Insert the path of the file to be processed")
self.text_filepath.config(state=tk.DISABLED)
self.entry_filepath = tk.Entry(self, width=20)
self.entry_filepath.grid(sticky=W+E+N+S, row=0, column=1, columnspan=3)
self.button_filepath = tk.Button(self, text="Initialize", command=self.new_window, width=25)
self.button_filepath.grid(row=1, column=1, sticky=W+E+N+S)
self.rowconfigure(1, weight=1)
self.columnconfigure(1, weight=1)
def top10_window(self, data):
"""Method which displays a list of the top 10 readers
based on reading time.
:param data: Reference to a Document object.
:return: None
"""
window = tk.Toplevel(self)
list_box = tk.Listbox(window)
list_box.pack()
for item in data.top10_readers():
list_box.insert(tk.END, item)
def also_likes_most_viewers_window(self, data, visitor_id, doc_id):
"""Method which implements the 'also likes' functionality.
Creates a new window which displays a list of top 10 relevant documents.
Sorting is based on number of viewers of each document document.
:param data: Reference to a Document object.
:param visitor_id: The visitor UUID.
:param doc_id: The document UUID.
:return: None
"""
window = tk.Toplevel(self)
list_box = tk.Listbox(window)
list_box.pack()
for item in data.also_likes_most_viewers(visitor_id, doc_id):
list_box.insert(tk.END, item)
def also_likes_timeread_window(self, data, visitor_id, doc_id):
"""Method which implements the 'also likes' functionality.
Creates a new window which displays a list of top 10 relevant documents.
Sorting is based on reading time spent on each document.
:param data: Reference to a Document object.
:param visitor_id: The visitor UUID.
:param doc_id: The document UUID.
:return: None
"""
window = tk.Toplevel(self)
list_box = tk.Listbox(window)
list_box.pack()
for item in data.also_likes_reader_profile(visitor_id, doc_id):
list_box.insert(tk.END, item)
我还应该提到,这些方法通过 GUI 可以正常工作。
任何帮助将不胜感激!
您需要调用 tkinter 主循环才能让 windows 显示操作。