对FileOpen类添加了encoding的选项,并对所有open函数进行了替换

Bug499-WangZiming
王梓铭 2022-11-21 22:38:46 +08:00
parent b8560a34e6
commit ee6eb45d4b
6 changed files with 53 additions and 25 deletions

View File

@ -1,13 +1,15 @@
''' """
Yaml.py Yaml.py
配置文件包括: 配置文件包括:
./static/config.yml ./static/config.yml
./layout/partial/header.html ./layout/partial/header.html
./layout/partial/footer.html ./layout/partial/footer.html
''' """
import yaml as YAML import yaml as YAML
import os import os
from app.file_open import FileOpen
path_prefix = './' # comment this line in deployment path_prefix = './' # comment this line in deployment
# YAML文件路径 # YAML文件路径
@ -15,13 +17,16 @@ ymlPath = path_prefix + 'static/config.yml'
# partial文件夹路径 # partial文件夹路径
partialPath = path_prefix + 'layout/partial/' partialPath = path_prefix + 'layout/partial/'
f = open(ymlPath, 'r', encoding='utf-8') # 以'UTF-8'格式打开YAML文件 # f = open(ymlPath, 'r', encoding='utf-8') # 以'UTF-8'格式打开YAML文件
f = FileOpen.read_only(ymlPath, 'r', encoding='utf-8') # 以'UTF-8'格式打开YAML文件
cont = f.read() # 以文本形式读取YAML cont = f.read() # 以文本形式读取YAML
yml = YAML.load(cont, Loader=YAML.FullLoader) # 加载YAML yml = YAML.load(cont, Loader=YAML.FullLoader) # 加载YAML
with open(partialPath + 'header.html', 'r', encoding='utf-8') as f: # with open(partialPath + 'header.html', 'r', encoding='utf-8') as f:
with FileOpen.read_only(partialPath + 'header.html', 'r', encoding='utf-8') as f:
yml['header'] = f.read() # header内的文本会被直接添加到所有页面的head标签内 yml['header'] = f.read() # header内的文本会被直接添加到所有页面的head标签内
with open(partialPath + 'footer.html', 'r', encoding='utf-8') as f: # with open(partialPath + 'footer.html', 'r', encoding='utf-8') as f:
with FileOpen.read_only(partialPath + 'footer.html', 'r', encoding='utf-8') as f:
yml['footer'] = f.read() # footer内的文本会被直接添加到所有页面的最底部 yml['footer'] = f.read() # footer内的文本会被直接添加到所有页面的最底部

View File

@ -7,11 +7,14 @@
import pickle import pickle
import math import math
from app.file_open import FileOpen
from wordfreqCMD import remove_punctuation, freq, sort_in_descending_order, sort_in_ascending_order from wordfreqCMD import remove_punctuation, freq, sort_in_descending_order, sort_in_ascending_order
def load_record(pickle_fname): def load_record(pickle_fname):
f = open(pickle_fname, 'rb') # f = open(pickle_fname, 'rb')
f = FileOpen.read_only(pickle_fname, 'rb')
d = pickle.load(f) d = pickle.load(f)
f.close() f.close()
return d return d
@ -237,7 +240,8 @@ We need — for our farmers, our manufacturers, for, frankly, unions and non-uni
#f = open('bbc-fulltext/bbc/entertainment/001.txt') #f = open('bbc-fulltext/bbc/entertainment/001.txt')
f = open('wordlist.txt') # f = open('wordlist.txt')
f = FileOpen.read_only('wordlist.txt')
s = f.read() s = f.read()
f.close() f.close()

View File

@ -15,7 +15,7 @@ class FileOpen:
pass pass
@staticmethod @staticmethod
def read_only(path, mode='r'): def read_only(path, mode='r', encoding=None):
if mode not in ['r', 'r+', 'rb', 'rt', 'rb+', 'rt+']: if mode not in ['r', 'r+', 'rb', 'rt', 'rb+', 'rt+']:
raise ModeErrorException('the input mode is wrong') raise ModeErrorException('the input mode is wrong')
my_dir = os.path.dirname(path) # 获得路径的目录 my_dir = os.path.dirname(path) # 获得路径的目录
@ -23,17 +23,17 @@ class FileOpen:
os.makedirs(my_dir) # 创建所有父文件夹 os.makedirs(my_dir) # 创建所有父文件夹
# 读文件需要判断文件是否存在,不存在则创建 # 读文件需要判断文件是否存在,不存在则创建
if not os.path.exists(path): if not os.path.exists(path):
open(path, mode) open(path, mode, encoding)
return open(path, mode) return open(path, mode, encoding)
@staticmethod @staticmethod
def write_able(path, mode='w'): def write_able(path, mode='w', encoding=None):
if mode not in ['w', 'w+', 'wb', 'wt', 'wb+', 'wt+']: if mode not in ['w', 'w+', 'wb', 'wt', 'wb+', 'wt+']:
raise ModeErrorException('the input mode is wrong') raise ModeErrorException('the input mode is wrong')
my_dir = os.path.dirname(path) # 获得路径的目录 my_dir = os.path.dirname(path) # 获得路径的目录
if not os.path.exists(my_dir): # 判断文件的所有父文件夹是否存在 if not os.path.exists(my_dir): # 判断文件的所有父文件夹是否存在
os.makedirs(my_dir) # 创建所有父文件夹 os.makedirs(my_dir) # 创建所有父文件夹
return open(path, mode) return open(path, mode, encoding)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -9,6 +9,8 @@
import pickle import pickle
from datetime import datetime from datetime import datetime
from app.file_open import FileOpen
def lst2dict(lst, d): def lst2dict(lst, d):
''' '''
@ -37,14 +39,16 @@ def merge_frequency(lst1, lst2):
def load_record(pickle_fname): def load_record(pickle_fname):
f = open(pickle_fname, 'rb') # f = open(pickle_fname, 'rb')
f = FileOpen.read_only(pickle_fname, 'rb')
d = pickle.load(f) d = pickle.load(f)
f.close() f.close()
return d return d
def save_frequency_to_pickle(d, pickle_fname): def save_frequency_to_pickle(d, pickle_fname):
f = open(pickle_fname, 'wb') # f = open(pickle_fname, 'wb')
f = FileOpen.write_able(pickle_fname, 'wb')
#exclusion_lst = ['one', 'no', 'has', 'had', 'do', 'that', 'have', 'by', 'not', 'but', 'we', 'this', 'my', 'him', 'so', 'or', 'as', 'are', 'it', 'from', 'with', 'be', 'can', 'for', 'an', 'if', 'who', 'whom', 'whose', 'which', 'the', 'to', 'a', 'of', 'and', 'you', 'i', 'he', 'she', 'they', 'me', 'was', 'were', 'is', 'in', 'at', 'on', 'their', 'his', 'her', 's', 'said', 'all', 'did', 'been', 'w'] #exclusion_lst = ['one', 'no', 'has', 'had', 'do', 'that', 'have', 'by', 'not', 'but', 'we', 'this', 'my', 'him', 'so', 'or', 'as', 'are', 'it', 'from', 'with', 'be', 'can', 'for', 'an', 'if', 'who', 'whom', 'whose', 'which', 'the', 'to', 'a', 'of', 'and', 'you', 'i', 'he', 'she', 'they', 'me', 'was', 'were', 'is', 'in', 'at', 'on', 'their', 'his', 'her', 's', 'said', 'all', 'did', 'been', 'w']
exclusion_lst = [] exclusion_lst = []
d2 = {} d2 = {}
@ -55,20 +59,24 @@ def save_frequency_to_pickle(d, pickle_fname):
f.close() f.close()
def unfamiliar(path,word): def unfamiliar(path,word):
f = open(path,"rb") # f = open(path,"rb")
f = FileOpen.read_only(path, 'rb')
dic = pickle.load(f) dic = pickle.load(f)
dic[word] += [datetime.now().strftime('%Y%m%d%H%M')] dic[word] += [datetime.now().strftime('%Y%m%d%H%M')]
fp = open(path,"wb") # fp = open(path,"wb")
fp = FileOpen.write_able(path, 'wb')
pickle.dump(dic,fp) pickle.dump(dic,fp)
def familiar(path,word): def familiar(path,word):
f = open(path,"rb") # f = open(path,"rb")
f = FileOpen.read_only(path, 'rb')
dic = pickle.load(f) dic = pickle.load(f)
if len(dic[word])>1: if len(dic[word])>1:
del dic[word][0] del dic[word][0]
else: else:
dic.pop(word) dic.pop(word)
fp = open(path,"wb") # fp = open(path,"wb")
fp = FileOpen.write_able(path, 'wb')
pickle.dump(dic,fp) pickle.dump(dic,fp)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -11,6 +11,9 @@
import pickle import pickle
from datetime import datetime from datetime import datetime
from app.file_open import FileOpen
def lst2dict(lst, d): def lst2dict(lst, d):
''' '''
Store the information in list lst to dictionary d. Store the information in list lst to dictionary d.
@ -26,13 +29,15 @@ def lst2dict(lst, d):
d[word] += dates d[word] += dates
def deleteRecord(path,word): def deleteRecord(path,word):
with open(path, 'rb') as f: # with open(path, 'rb') as f:
with FileOpen.read_only(path, 'rb') as f:
db = pickle.load(f) db = pickle.load(f)
try: try:
db.pop(word) db.pop(word)
except KeyError: except KeyError:
print("sorry") print("sorry")
with open(path, 'wb') as ff: # with open(path, 'wb') as ff:
with FileOpen.write_able(path, 'wb') as ff:
pickle.dump(db, ff) pickle.dump(db, ff)
def dict2lst(d): def dict2lst(d):
@ -56,14 +61,16 @@ def merge_frequency(lst1, lst2):
def load_record(pickle_fname): def load_record(pickle_fname):
f = open(pickle_fname, 'rb') # f = open(pickle_fname, 'rb')
f = FileOpen.read_only(pickle_fname, 'rb')
d = pickle.load(f) d = pickle.load(f)
f.close() f.close()
return d return d
def save_frequency_to_pickle(d, pickle_fname): def save_frequency_to_pickle(d, pickle_fname):
f = open(pickle_fname, 'wb') # f = open(pickle_fname, 'wb')
f = FileOpen.write_able(pickle_fname, 'wb')
exclusion_lst = ['one', 'no', 'has', 'had', 'do', 'that', 'have', 'by', 'not', 'but', 'we', 'this', 'my', 'him', 'so', 'or', 'as', 'are', 'it', 'from', 'with', 'be', 'can', 'for', 'an', 'if', 'who', 'whom', 'whose', 'which', 'the', 'to', 'a', 'of', 'and', 'you', 'i', 'he', 'she', 'they', 'me', 'was', 'were', 'is', 'in', 'at', 'on', 'their', 'his', 'her', 's', 'said', 'all', 'did', 'been', 'w'] exclusion_lst = ['one', 'no', 'has', 'had', 'do', 'that', 'have', 'by', 'not', 'but', 'we', 'this', 'my', 'him', 'so', 'or', 'as', 'are', 'it', 'from', 'with', 'be', 'can', 'for', 'an', 'if', 'who', 'whom', 'whose', 'which', 'the', 'to', 'a', 'of', 'and', 'you', 'i', 'he', 'she', 'they', 'me', 'was', 'were', 'is', 'in', 'at', 'on', 'their', 'his', 'her', 's', 'said', 'all', 'did', 'been', 'w']
d2 = {} d2 = {}
for k in d: for k in d:

View File

@ -8,6 +8,8 @@ import string
import operator import operator
import os, sys # 引入模块sys因为我要用里面的sys.argv列表中的信息来读取命令行参数。 import os, sys # 引入模块sys因为我要用里面的sys.argv列表中的信息来读取命令行参数。
import pickle_idea import pickle_idea
from app.file_open import FileOpen
def freq(fruit): def freq(fruit):
''' '''
@ -32,7 +34,8 @@ def youdao_link(s): # 有道链接
def file2str(fname):#文件转字符 def file2str(fname):#文件转字符
f = open(fname) #打开 # f = open(fname) #打开
f = FileOpen.read_only(fname)
s = f.read() #读取 s = f.read() #读取
f.close() #关闭 f.close() #关闭
return s return s
@ -80,7 +83,8 @@ def make_html_page(lst, fname):
# <a href="">word</a> # <a href="">word</a>
s += '<p>%d <a href="%s">%s</a> (%d)</p>' % (count, youdao_link(x[0]), x[0], x[1]) s += '<p>%d <a href="%s">%s</a> (%d)</p>' % (count, youdao_link(x[0]), x[0], x[1])
count += 1 count += 1
f = open(fname, 'w') # f = open(fname, 'w')
f = FileOpen.write_able(fname)
f.write(s) f.write(s)
f.close() f.close()