forked from p46318075/CodePattern
parent
88606f2bce
commit
8d5c578da8
Binary file not shown.
@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
|
|
||||||
[Plugins]
|
[Plugins]
|
||||||
;; Options: plugins/words1.pyc, plugins/words2.pyc
|
;; Options: plugins/f1.pyc, plugins/f2.pyc
|
||||||
words = plugins/words1.pyc
|
frequencies = plugins/f2.pyc
|
||||||
;; Options: plugins/frequencies1.pyc, plugins/frequencies2.pyc
|
|
||||||
frequencies = plugins/frequencies1.pyc
|
|
@ -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) # 打印词频
|
@ -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.")
|
@ -1,2 +0,0 @@
|
|||||||
python -m compileall .
|
|
||||||
cp __pycache__/*.pyc ../plugins
|
|
@ -1,3 +1,5 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import operator
|
import operator
|
||||||
|
|
||||||
def top_word(word_list):
|
def top_word(word_list):
|
@ -1,6 +1,8 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import collections
|
import collections
|
||||||
|
|
||||||
def top_word(word_list):
|
def top_word(word_list):
|
||||||
counts = collections.Counter(w for w in word_list)
|
counts = collections.Counter( word_list )
|
||||||
return counts.most_common(10)
|
return counts.most_common(10)
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -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)
|
|
Loading…
Reference in new issue