You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.2 KiB

9 months ago
import cppy.cp_util as util
##########################################
# 工具类
##########################################
class wordsTaskHandler:
def handle(self,path_to_file):
return util.extract_file_words(path_to_file)
class frequenciesTaskHandler:
def handle(self,word_list):
return util.get_frequencies(word_list)
class sortTaskHandler:
def handle(self,word_freq):
return util.sort_dict(word_freq)
##########################################
# 应用框架
##########################################
def handle_task(task_type,*args):
handler_class_name = f"{task_type}TaskHandler" # 构建处理器类名
# 使用globals()获取当前全局符号表
handler_class = globals().get(handler_class_name)
if handler_class:
handler = handler_class() # 实例化处理器类
return handler.handle(*args) # 调用处理方法
else:
8 months ago
print(f"No handler found for task type: {task_type}")
9 months ago
if __name__ == '__main__':
word_list = handle_task("words",util.testfilepath)
word_freq = handle_task("frequencies",word_list)
word_sort = handle_task("sort",word_freq)
util.print_word_freqs(word_sort)