diff --git a/11 概念认知/对象化/1 类对象.py b/11 概念认知/对象化/1 类对象.py index b9d5c94..013e743 100644 --- a/11 概念认知/对象化/1 类对象.py +++ b/11 概念认知/对象化/1 类对象.py @@ -49,4 +49,10 @@ class WordFrequencyController: if __name__ == '__main__': - WordFrequencyController(testfilepath).run() \ No newline at end of file + WordFrequencyController(testfilepath).run() + + +''' +函数输入参数调用后,你的马上接住返回值 +类输入参数后实例化后,你可以需要的时候去访问你需要的数据(实例属性) +''' \ No newline at end of file diff --git a/15 工程化/1 松耦合/5 插件/__pycache__/tf-20.cpython-38.pyc b/15 工程化/1 松耦合/5 插件/__pycache__/tf-20.cpython-38.pyc deleted file mode 100644 index 21826bc..0000000 Binary files a/15 工程化/1 松耦合/5 插件/__pycache__/tf-20.cpython-38.pyc and /dev/null differ diff --git a/15 工程化/1 松耦合/5 插件/config.ini b/15 工程化/1 松耦合/5 插件/config.ini index 5da4aea..b70f4c9 100644 --- a/15 工程化/1 松耦合/5 插件/config.ini +++ b/15 工程化/1 松耦合/5 插件/config.ini @@ -1,5 +1,5 @@ + + [Plugins] -;; Options: plugins/words1.pyc, plugins/words2.pyc -words = plugins/words1.pyc -;; Options: plugins/frequencies1.pyc, plugins/frequencies2.pyc -frequencies = plugins/frequencies1.pyc \ No newline at end of file +;; Options: plugins/f1.pyc, plugins/f2.pyc +frequencies = plugins/f2.pyc \ No newline at end of file diff --git a/15 工程化/1 松耦合/5 插件/plugin.py b/15 工程化/1 松耦合/5 插件/plugin.py new file mode 100644 index 0000000..d724647 --- /dev/null +++ b/15 工程化/1 松耦合/5 插件/plugin.py @@ -0,0 +1,30 @@ +import configparser, importlib.machinery +from cppy.cp_util import * + +class PluginManager: + def __init__(self): + self.plugins = {} + + def load_plugins(self): + _dir = os.path.dirname(os.path.abspath(__file__)) + os.chdir(_dir) + + config = configparser.ConfigParser() + config.read("config.ini") + + frequencies_plugin = config.get("Plugins", "frequencies") + + # 加载插件 + self.plugins['word_freqs'] = importlib.machinery.SourcelessFileLoader('', frequencies_plugin).load_module() + + def get_plugin(self, name): + return self.plugins.get(name) + + +# 创建 PluginManager 实例 +plugin_manager = PluginManager() +plugin_manager.load_plugins() + +wordlist = extract_file_words(testfilepath) # 提取文件中的单词 +word_freqs = plugin_manager.get_plugin('word_freqs').top_word(wordlist) # 调用实例方法 +print_word_freqs(word_freqs) # 打印词频 \ No newline at end of file diff --git a/15 工程化/1 松耦合/5 插件/plugins-src/buildingPyc.py b/15 工程化/1 松耦合/5 插件/plugins-src/buildingPyc.py new file mode 100644 index 0000000..2295d4c --- /dev/null +++ b/15 工程化/1 松耦合/5 插件/plugins-src/buildingPyc.py @@ -0,0 +1,28 @@ +import py_compile + +py_compile.compile('f1.py') +py_compile.compile('f2.py') + +import os +import shutil + +# 设置源目录和目标目录 +source_dir = os.path.join(os.path.dirname(__file__), '__pycache__') # 当前目录下的 __pycache__ 目录 +target_dir = os.path.join(os.path.dirname(__file__), '..', 'plugins') # 上一级目录下的 plugins 目录 + +# 确保目标目录存在 +os.makedirs(target_dir, exist_ok=True) + +# 遍历源目录中的所有 .pyc 文件 +for filename in os.listdir(source_dir): + if filename.endswith('.pyc'): + # 提取文件名的前两个字符 + new_filename = filename[:2] + # 构建源文件和目标文件的完整路径 + source_file = os.path.join(source_dir, filename) + target_file = os.path.join(target_dir, new_filename + '.pyc') + # 拷贝文件 + shutil.copyfile(source_file, target_file) + # 删除原始文件 + os.remove(source_file) + print(f"Copied {filename} to {target_file} and removed original file.") \ No newline at end of file diff --git a/15 工程化/1 松耦合/5 插件/plugins-src/compile.sh b/15 工程化/1 松耦合/5 插件/plugins-src/compile.sh deleted file mode 100644 index 7a4750e..0000000 --- a/15 工程化/1 松耦合/5 插件/plugins-src/compile.sh +++ /dev/null @@ -1,2 +0,0 @@ -python -m compileall . -cp __pycache__/*.pyc ../plugins diff --git a/15 工程化/1 松耦合/5 插件/plugins-src/frequencies1.py b/15 工程化/1 松耦合/5 插件/plugins-src/f1.py similarity index 91% rename from 15 工程化/1 松耦合/5 插件/plugins-src/frequencies1.py rename to 15 工程化/1 松耦合/5 插件/plugins-src/f1.py index ba452f7..c4d51c7 100644 --- a/15 工程化/1 松耦合/5 插件/plugins-src/frequencies1.py +++ b/15 工程化/1 松耦合/5 插件/plugins-src/f1.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + import operator def top_word(word_list): diff --git a/15 工程化/1 松耦合/5 插件/plugins-src/frequencies2.py b/15 工程化/1 松耦合/5 插件/plugins-src/f2.py similarity index 53% rename from 15 工程化/1 松耦合/5 插件/plugins-src/frequencies2.py rename to 15 工程化/1 松耦合/5 插件/plugins-src/f2.py index 4457270..5d97aba 100644 --- a/15 工程化/1 松耦合/5 插件/plugins-src/frequencies2.py +++ b/15 工程化/1 松耦合/5 插件/plugins-src/f2.py @@ -1,6 +1,8 @@ +# -*- coding: utf-8 -*- + import collections def top_word(word_list): - counts = collections.Counter(w for w in word_list) + counts = collections.Counter( word_list ) return counts.most_common(10) diff --git a/15 工程化/1 松耦合/5 插件/plugins/frequencies1.pyc b/15 工程化/1 松耦合/5 插件/plugins/f1.pyc similarity index 51% rename from 15 工程化/1 松耦合/5 插件/plugins/frequencies1.pyc rename to 15 工程化/1 松耦合/5 插件/plugins/f1.pyc index 73fae45..31166b3 100644 Binary files a/15 工程化/1 松耦合/5 插件/plugins/frequencies1.pyc and b/15 工程化/1 松耦合/5 插件/plugins/f1.pyc differ diff --git a/15 工程化/1 松耦合/5 插件/plugins/f2.pyc b/15 工程化/1 松耦合/5 插件/plugins/f2.pyc new file mode 100644 index 0000000..f045d42 Binary files /dev/null and b/15 工程化/1 松耦合/5 插件/plugins/f2.pyc differ diff --git a/15 工程化/1 松耦合/5 插件/plugins/frequencies2.cpython-38.pyc b/15 工程化/1 松耦合/5 插件/plugins/frequencies2.cpython-38.pyc deleted file mode 100644 index f6743e5..0000000 Binary files a/15 工程化/1 松耦合/5 插件/plugins/frequencies2.cpython-38.pyc and /dev/null differ diff --git a/15 工程化/1 松耦合/5 插件/tf-20.py b/15 工程化/1 松耦合/5 插件/tf-20.py deleted file mode 100644 index cebe2d2..0000000 --- a/15 工程化/1 松耦合/5 插件/tf-20.py +++ /dev/null @@ -1,19 +0,0 @@ -import configparser, importlib.machinery -from cppy.cp_util import * - -def load_plugins(): - config = configparser.ConfigParser() - script_dir = os.path.dirname(os.path.abspath(__file__)) - os.chdir(script_dir) - config.read("config.ini") - frequencies_plugin = config.get("Plugins", "frequencies") - - global get_frequencies - get_frequencies = importlib.machinery.SourcelessFileLoader('tffreqs', frequencies_plugin).load_module() - - -load_plugins() - -wordlist = extract_file_words( testfilepath ) -word_freqs = get_frequencies.top_word( wordlist ) -print_word_freqs(word_freqs) \ No newline at end of file