From 8d5c578da82e8c2350ad6bb999bb9f328fffa3e2 Mon Sep 17 00:00:00 2001 From: zj3D Date: Fri, 22 Mar 2024 17:26:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=92=E4=BB=B6=E7=AE=80=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 11 概念认知/对象化/1 类对象.py | 8 ++++- .../5 插件/__pycache__/tf-20.cpython-38.pyc | Bin 797 -> 0 bytes 15 工程化/1 松耦合/5 插件/config.ini | 8 ++--- 15 工程化/1 松耦合/5 插件/plugin.py | 30 ++++++++++++++++++ .../5 插件/plugins-src/buildingPyc.py | 28 ++++++++++++++++ .../5 插件/plugins-src/compile.sh | 2 -- .../plugins-src/{frequencies1.py => f1.py} | 2 ++ .../plugins-src/{frequencies2.py => f2.py} | 4 ++- .../plugins/{frequencies1.pyc => f1.pyc} | Bin 393 -> 384 bytes .../1 松耦合/5 插件/plugins/f2.pyc | Bin 0 -> 285 bytes .../plugins/frequencies2.cpython-38.pyc | Bin 452 -> 0 bytes 15 工程化/1 松耦合/5 插件/tf-20.py | 19 ----------- 12 files changed, 74 insertions(+), 27 deletions(-) delete mode 100644 15 工程化/1 松耦合/5 插件/__pycache__/tf-20.cpython-38.pyc create mode 100644 15 工程化/1 松耦合/5 插件/plugin.py create mode 100644 15 工程化/1 松耦合/5 插件/plugins-src/buildingPyc.py delete mode 100644 15 工程化/1 松耦合/5 插件/plugins-src/compile.sh rename 15 工程化/1 松耦合/5 插件/plugins-src/{frequencies1.py => f1.py} (91%) rename 15 工程化/1 松耦合/5 插件/plugins-src/{frequencies2.py => f2.py} (53%) rename 15 工程化/1 松耦合/5 插件/plugins/{frequencies1.pyc => f1.pyc} (51%) create mode 100644 15 工程化/1 松耦合/5 插件/plugins/f2.pyc delete mode 100644 15 工程化/1 松耦合/5 插件/plugins/frequencies2.cpython-38.pyc delete mode 100644 15 工程化/1 松耦合/5 插件/tf-20.py 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 21826bc44979c5ee07a03a31f8955557c6933895..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 797 zcmYjQOKTKC5bmDGK4xb{Vh$d2-NS|$%q2(=BAz4!qnBo68K-MEt+O+e?p~6W%|Q}R z9s(YP;0}W5CC4B}{2M}F-ADWZUOd^;yM`20b=5c3RW)DHPwRCLX;EuGVb4P7R}own zR|ogCiYMAoL~)GB?8F3{*djKysC|rx1I`h`NV>z4EDtMC&2f&22i^npY%#D%jrdT9 z8g;-wvN76Qr+5c7+~2w~+KLMMUu^9-S~s# zThe)<`%`y-b8A@6QCOKHrGYg^subzI<*ip42esexPJDPs`G&%h+In75G=cBK0r|*w1&wgB<<-y9GrOC-VjRvyaTwPht`pP$d zFq=;l8x+1a2v@L&ee7EcnAdgu7dIO>lr8##7fIyS6-moi9PzULVzLbj#Fc5bx@Kr0 zbvTebjAU!JD_??;ZOvmA%3b0WV9TsY;eCZ8K@}3%9LG KwS;e4KK=*JsqhB? 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 73fae45a8d7f185770569a8de16d9642470d0555..31166b30b3ec3bf38629152dd1763cc1bb5d71a4 100644 GIT binary patch delta 56 zcmeBVZeZpO<>lpK00L3xzp0iRc|{lnSlpK0D|XYw^Ic+@`^A@3F^h96{QxIrsgGQrWPCO6;$40Ey*u1GG(4@ Q#OTb;2vl6m1SB|^03^2$eEXj^vOG)TOp~>h4mGttj&nM;p>lE7kPoG%rlB@d#pFcJ zl(zPv7-_NiUvMKH@EGntR)_DH20D~t8p_%M6W|nG%9<%B_w?^R^Fm{8#PqWvbF|f3 JY+mk9)ju|jIEDZK literal 0 HcmV?d00001 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 f6743e5332c81eb5401917cbb1df92bb11a77f96..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 452 zcmZWlJxc>Y5S`g1dPxYVh**gIHHQT3LJ(q|MzlDT5O!Ay?A=~=H$){?ruH`^rGIHH ztK@!JB-c`W~9iVAPyZ+9Lu10u`j|iYW>vDsxDg zzzc}0E82iQ$56f_u&b0kN_$M;V1a^obC|(7JmV6daE1%gS(Y0A=|RGV+#{I5O(PJl z0Ab7okS+FKA!eQ>CmyFJY?tu4n_S--IiARzkECrU#e=VR?qn|S3)2lZ*#K2w@ObES z(LPN&O7o$z-Q;&|75*l!_$Jp%N$y5Ew|={)C%KcxH#2SBfa@&NdCb!KR2wl+BkR&F z9#-t%HKs;FzE=8Fe!Kzq(KOc%kuIbeI&DlugL1mTE?-qjM4e2FiIUy$%Qnn+*caMs BTh0Ig 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