From 415ac25a311fb5222afa26e886cb13c5be49ab89 Mon Sep 17 00:00:00 2001 From: torch <178428409@qq.com> Date: Sun, 20 Nov 2022 23:23:18 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E5=AE=8C=E6=88=90=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E7=9A=84=E5=88=9D=E5=A7=8B=E8=BF=90=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/account_service.py | 6 ++++-- app/user_service.py | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/account_service.py b/app/account_service.py index 09439d5..02394e7 100644 --- a/app/account_service.py +++ b/app/account_service.py @@ -1,4 +1,6 @@ from flask import * +from markupsafe import escape + from Login import check_username_availability, verify_user, add_user, get_expiry_date, change_password, WarningMessage @@ -20,12 +22,12 @@ def signup(): # POST方法需判断是否注册成功,再根据结果返回不同的内容 username = escape(request.form['username']) password = escape(request.form['password']) - + #! 添加如下代码为了过滤注册时的非法字符 warn = WarningMessage(username) if str(warn) != 'OK': return str(warn) - + available = check_username_availability(username) if not available: # 用户名不可用 flash('用户名 %s 已经被注册。' % (username)) diff --git a/app/user_service.py b/app/user_service.py index 79c7888..fbda760 100644 --- a/app/user_service.py +++ b/app/user_service.py @@ -1,6 +1,7 @@ from datetime import datetime from flask import * +from markupsafe import escape # from app import Yaml # from app.Article import get_today_article, load_freq_history -- 2.17.1 From b8560a34e6b08d175ca72c19726e8c43bef0f0db Mon Sep 17 00:00:00 2001 From: torch <178428409@qq.com> Date: Mon, 21 Nov 2022 20:00:30 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=AF=B9open=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E7=9A=84=E5=8C=85=E8=A3=85=EF=BC=8C=E4=B8=94=E5=B0=81?= =?UTF-8?q?=E8=A3=85=E6=88=90=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/file_open.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 app/file_open.py diff --git a/app/file_open.py b/app/file_open.py new file mode 100644 index 0000000..ee5512a --- /dev/null +++ b/app/file_open.py @@ -0,0 +1,46 @@ +import os + + +class ModeErrorException(Exception): + def __init__(self, error_info): + super().__init__(self) + self.error_info = error_info + + def __str__(self): + return self.error_info + + +class FileOpen: + def __init__(self): + pass + + @staticmethod + def read_only(path, mode='r'): + if mode not in ['r', 'r+', 'rb', 'rt', 'rb+', 'rt+']: + raise ModeErrorException('the input mode is wrong') + my_dir = os.path.dirname(path) # 获得路径的目录 + if not os.path.exists(my_dir): # 判断文件的所有父文件夹是否存在 + os.makedirs(my_dir) # 创建所有父文件夹 + # 读文件需要判断文件是否存在,不存在则创建 + if not os.path.exists(path): + open(path, mode) + return open(path, mode) + + @staticmethod + def write_able(path, mode='w'): + if mode not in ['w', 'w+', 'wb', 'wt', 'wb+', 'wt+']: + raise ModeErrorException('the input mode is wrong') + my_dir = os.path.dirname(path) # 获得路径的目录 + if not os.path.exists(my_dir): # 判断文件的所有父文件夹是否存在 + os.makedirs(my_dir) # 创建所有父文件夹 + return open(path, mode) + + +if __name__ == "__main__": + wf = FileOpen.write_able(r"D:\Workspace\gitcode\EnglishPal\app\static\test.txt") + wf.write("asdasdasd") + wf.close() + + rf = FileOpen.read_only(r"D:\Workspace\gitcode\EnglishPal\app\static\test.txt") + for line in rf.readlines(): + print(line) \ No newline at end of file -- 2.17.1 From ee6eb45d4b122b33dca01f549a3a8d49aa25e7dc Mon Sep 17 00:00:00 2001 From: torch <178428409@qq.com> Date: Mon, 21 Nov 2022 22:38:46 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E5=AF=B9FileOpen=E7=B1=BB=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=BA=86encoding=E7=9A=84=E9=80=89=E9=A1=B9=EF=BC=8C?= =?UTF-8?q?=E5=B9=B6=E5=AF=B9=E6=89=80=E6=9C=89open=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E4=BA=86=E6=9B=BF=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Yaml.py | 15 ++++++++++----- app/difficulty.py | 8 ++++++-- app/file_open.py | 10 +++++----- app/pickle_idea.py | 20 ++++++++++++++------ app/pickle_idea2.py | 17 ++++++++++++----- app/wordfreqCMD.py | 8 ++++++-- 6 files changed, 53 insertions(+), 25 deletions(-) diff --git a/app/Yaml.py b/app/Yaml.py index 00974aa..8823041 100644 --- a/app/Yaml.py +++ b/app/Yaml.py @@ -1,13 +1,15 @@ -''' +""" Yaml.py 配置文件包括: ./static/config.yml ./layout/partial/header.html ./layout/partial/footer.html -''' +""" import yaml as YAML import os +from app.file_open import FileOpen + path_prefix = './' # comment this line in deployment # YAML文件路径 @@ -15,13 +17,16 @@ ymlPath = path_prefix + 'static/config.yml' # 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 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标签内 -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内的文本会被直接添加到所有页面的最底部 diff --git a/app/difficulty.py b/app/difficulty.py index 50aa179..5eb12bd 100644 --- a/app/difficulty.py +++ b/app/difficulty.py @@ -7,11 +7,14 @@ import pickle import math + +from app.file_open import FileOpen from wordfreqCMD import remove_punctuation, freq, sort_in_descending_order, sort_in_ascending_order 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) f.close() 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('wordlist.txt') + # f = open('wordlist.txt') + f = FileOpen.read_only('wordlist.txt') s = f.read() f.close() diff --git a/app/file_open.py b/app/file_open.py index ee5512a..69d2f9d 100644 --- a/app/file_open.py +++ b/app/file_open.py @@ -15,7 +15,7 @@ class FileOpen: pass @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+']: raise ModeErrorException('the input mode is wrong') my_dir = os.path.dirname(path) # 获得路径的目录 @@ -23,17 +23,17 @@ class FileOpen: os.makedirs(my_dir) # 创建所有父文件夹 # 读文件需要判断文件是否存在,不存在则创建 if not os.path.exists(path): - open(path, mode) - return open(path, mode) + open(path, mode, encoding) + return open(path, mode, encoding) @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+']: raise ModeErrorException('the input mode is wrong') my_dir = os.path.dirname(path) # 获得路径的目录 if not os.path.exists(my_dir): # 判断文件的所有父文件夹是否存在 os.makedirs(my_dir) # 创建所有父文件夹 - return open(path, mode) + return open(path, mode, encoding) if __name__ == "__main__": diff --git a/app/pickle_idea.py b/app/pickle_idea.py index 45bd19a..3c0c853 100644 --- a/app/pickle_idea.py +++ b/app/pickle_idea.py @@ -9,6 +9,8 @@ import pickle from datetime import datetime +from app.file_open import FileOpen + def lst2dict(lst, d): ''' @@ -37,14 +39,16 @@ def merge_frequency(lst1, lst2): 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) f.close() return d 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 = [] d2 = {} @@ -55,20 +59,24 @@ def save_frequency_to_pickle(d, pickle_fname): f.close() def unfamiliar(path,word): - f = open(path,"rb") + # f = open(path,"rb") + f = FileOpen.read_only(path, 'rb') dic = pickle.load(f) 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) def familiar(path,word): - f = open(path,"rb") + # f = open(path,"rb") + f = FileOpen.read_only(path, 'rb') dic = pickle.load(f) if len(dic[word])>1: del dic[word][0] else: dic.pop(word) - fp = open(path,"wb") + # fp = open(path,"wb") + fp = FileOpen.write_able(path, 'wb') pickle.dump(dic,fp) if __name__ == '__main__': diff --git a/app/pickle_idea2.py b/app/pickle_idea2.py index 4055fc4..ab84901 100644 --- a/app/pickle_idea2.py +++ b/app/pickle_idea2.py @@ -11,6 +11,9 @@ import pickle from datetime import datetime +from app.file_open import FileOpen + + def lst2dict(lst, d): ''' Store the information in list lst to dictionary d. @@ -26,14 +29,16 @@ def lst2dict(lst, d): d[word] += dates 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) try: db.pop(word) except KeyError: print("sorry") - with open(path, 'wb') as ff: - pickle.dump(db, ff) + # with open(path, 'wb') as ff: + with FileOpen.write_able(path, 'wb') as ff: + pickle.dump(db, ff) def dict2lst(d): if len(d) > 0: @@ -56,14 +61,16 @@ def merge_frequency(lst1, lst2): 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) f.close() return d 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'] d2 = {} for k in d: diff --git a/app/wordfreqCMD.py b/app/wordfreqCMD.py index 9ee7e56..d421ef2 100644 --- a/app/wordfreqCMD.py +++ b/app/wordfreqCMD.py @@ -8,6 +8,8 @@ import string import operator import os, sys # 引入模块sys,因为我要用里面的sys.argv列表中的信息来读取命令行参数。 import pickle_idea +from app.file_open import FileOpen + def freq(fruit): ''' @@ -32,7 +34,8 @@ def youdao_link(s): # 有道链接 def file2str(fname):#文件转字符 - f = open(fname) #打开 + # f = open(fname) #打开 + f = FileOpen.read_only(fname) s = f.read() #读取 f.close() #关闭 return s @@ -80,7 +83,8 @@ def make_html_page(lst, fname): # word s += '

%d %s (%d)

' % (count, youdao_link(x[0]), x[0], x[1]) count += 1 - f = open(fname, 'w') + # f = open(fname, 'w') + f = FileOpen.write_able(fname) f.write(s) f.close() -- 2.17.1 From c14de06c863e744f0ff7bbe0f7f09e305f9572d5 Mon Sep 17 00:00:00 2001 From: torch <178428409@qq.com> Date: Tue, 22 Nov 2022 19:19:00 +0800 Subject: [PATCH 4/8] =?UTF-8?q?FileOpen=E5=B7=A5=E5=85=B7=E7=B1=BB?= =?UTF-8?q?=E5=AD=98=E5=9C=A8Bug=EF=BC=8C=E5=86=85=E9=83=A8=E6=8A=B5?= =?UTF-8?q?=E7=94=A8open=E5=87=BD=E6=95=B0=E7=9A=84=E6=97=B6=E5=80=99?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E6=8C=87=E5=AE=9A=E5=8F=82=E6=95=B0=EF=BC=8C?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E5=8F=82=E6=95=B0=E7=B1=BB=E5=9E=8B=E4=B8=8D?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E3=80=82=E8=BF=99=E9=87=8C=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E4=BA=86=E5=8F=82=E6=95=B0=E7=9A=84=E6=8C=87=E5=AE=9A=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E5=A4=8DBug=E3=80=82=E5=90=8C=E6=97=B6=E5=AF=B9read?= =?UTF-8?q?=E6=97=B6=E6=96=87=E4=BB=B6=E4=B8=8D=E5=AD=98=E5=9C=A8=EF=BC=8C?= =?UTF-8?q?=E5=88=9B=E9=80=A0=E6=96=87=E4=BB=B6=E5=AD=98=E5=9C=A8=E7=9A=84?= =?UTF-8?q?=E9=9A=90=E8=97=8F=E9=97=AE=E9=A2=98=E8=BF=9B=E8=A1=8C=E4=BA=86?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/file_open.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/file_open.py b/app/file_open.py index 69d2f9d..b726ed5 100644 --- a/app/file_open.py +++ b/app/file_open.py @@ -23,8 +23,9 @@ class FileOpen: os.makedirs(my_dir) # 创建所有父文件夹 # 读文件需要判断文件是否存在,不存在则创建 if not os.path.exists(path): - open(path, mode, encoding) - return open(path, mode, encoding) + f = open(path, 'w', encoding) + f.close() + return open(path, mode=mode, encoding=encoding) @staticmethod def write_able(path, mode='w', encoding=None): @@ -33,7 +34,7 @@ class FileOpen: my_dir = os.path.dirname(path) # 获得路径的目录 if not os.path.exists(my_dir): # 判断文件的所有父文件夹是否存在 os.makedirs(my_dir) # 创建所有父文件夹 - return open(path, mode, encoding) + return open(path, mode=mode, encoding=encoding) if __name__ == "__main__": -- 2.17.1 From cff0ce32b29a61de409e365b17a6a8216e680605 Mon Sep 17 00:00:00 2001 From: torch <178428409@qq.com> Date: Tue, 22 Nov 2022 20:57:29 +0800 Subject: [PATCH 5/8] test --- app/file_open.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/app/file_open.py b/app/file_open.py index b726ed5..1a2345a 100644 --- a/app/file_open.py +++ b/app/file_open.py @@ -38,10 +38,11 @@ class FileOpen: if __name__ == "__main__": - wf = FileOpen.write_able(r"D:\Workspace\gitcode\EnglishPal\app\static\test.txt") - wf.write("asdasdasd") - wf.close() - - rf = FileOpen.read_only(r"D:\Workspace\gitcode\EnglishPal\app\static\test.txt") - for line in rf.readlines(): - print(line) \ No newline at end of file + pass + # wf = FileOpen.write_able(r"D:\Workspace\gitcode\EnglishPal\app\static\test.txt") + # wf.write("asdasdasd") + # wf.close() + # + # rf = FileOpen.read_only(r"D:\Workspace\gitcode\EnglishPal\app\static\test.txt") + # for line in rf.readlines(): + # print(line) \ No newline at end of file -- 2.17.1 From 18f54e3505aba8913084609d014a021c7a80b50e Mon Sep 17 00:00:00 2001 From: nian <2286988932@qq.com> Date: Tue, 22 Nov 2022 21:20:52 +0800 Subject: [PATCH 6/8] testwyq --- app/file_open.py | 1 + 1 file changed, 1 insertion(+) diff --git a/app/file_open.py b/app/file_open.py index b726ed5..b511e83 100644 --- a/app/file_open.py +++ b/app/file_open.py @@ -39,6 +39,7 @@ class FileOpen: if __name__ == "__main__": wf = FileOpen.write_able(r"D:\Workspace\gitcode\EnglishPal\app\static\test.txt") + #print(1) wf.write("asdasdasd") wf.close() -- 2.17.1 From 3125af6846eacb8f1382afcf384416439c1c15cd Mon Sep 17 00:00:00 2001 From: nian <2286988932@qq.com> Date: Tue, 22 Nov 2022 21:23:20 +0800 Subject: [PATCH 7/8] testwyq --- app/file_open.py | 1 - 1 file changed, 1 deletion(-) diff --git a/app/file_open.py b/app/file_open.py index b511e83..b726ed5 100644 --- a/app/file_open.py +++ b/app/file_open.py @@ -39,7 +39,6 @@ class FileOpen: if __name__ == "__main__": wf = FileOpen.write_able(r"D:\Workspace\gitcode\EnglishPal\app\static\test.txt") - #print(1) wf.write("asdasdasd") wf.close() -- 2.17.1 From 348c37032b320a99ae46e23c0478ccc1f6f0a399 Mon Sep 17 00:00:00 2001 From: torch <178428409@qq.com> Date: Tue, 22 Nov 2022 21:26:06 +0800 Subject: [PATCH 8/8] test --- app/file_open.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/app/file_open.py b/app/file_open.py index 1a2345a..b726ed5 100644 --- a/app/file_open.py +++ b/app/file_open.py @@ -38,11 +38,10 @@ class FileOpen: if __name__ == "__main__": - pass - # wf = FileOpen.write_able(r"D:\Workspace\gitcode\EnglishPal\app\static\test.txt") - # wf.write("asdasdasd") - # wf.close() - # - # rf = FileOpen.read_only(r"D:\Workspace\gitcode\EnglishPal\app\static\test.txt") - # for line in rf.readlines(): - # print(line) \ No newline at end of file + wf = FileOpen.write_able(r"D:\Workspace\gitcode\EnglishPal\app\static\test.txt") + wf.write("asdasdasd") + wf.close() + + rf = FileOpen.read_only(r"D:\Workspace\gitcode\EnglishPal\app\static\test.txt") + for line in rf.readlines(): + print(line) \ No newline at end of file -- 2.17.1