1
0
Fork 0

Compare commits

...

9 Commits

8 changed files with 100 additions and 22 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

@ -1,4 +1,6 @@
from flask import * from flask import *
from markupsafe import escape
from Login import check_username_availability, verify_user, add_user, get_expiry_date, change_password, WarningMessage from Login import check_username_availability, verify_user, add_user, get_expiry_date, change_password, WarningMessage
@ -20,12 +22,12 @@ def signup():
# POST方法需判断是否注册成功再根据结果返回不同的内容 # POST方法需判断是否注册成功再根据结果返回不同的内容
username = escape(request.form['username']) username = escape(request.form['username'])
password = escape(request.form['password']) password = escape(request.form['password'])
#! 添加如下代码为了过滤注册时的非法字符 #! 添加如下代码为了过滤注册时的非法字符
warn = WarningMessage(username) warn = WarningMessage(username)
if str(warn) != 'OK': if str(warn) != 'OK':
return str(warn) return str(warn)
available = check_username_availability(username) available = check_username_availability(username)
if not available: # 用户名不可用 if not available: # 用户名不可用
flash('用户名 %s 已经被注册。' % (username)) flash('用户名 %s 已经被注册。' % (username))

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()

47
app/file_open.py Normal file
View File

@ -0,0 +1,47 @@
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', 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) # 获得路径的目录
if not os.path.exists(my_dir): # 判断文件的所有父文件夹是否存在
os.makedirs(my_dir) # 创建所有父文件夹
# 读文件需要判断文件是否存在,不存在则创建
if not os.path.exists(path):
f = open(path, 'w', encoding)
f.close()
return open(path, mode=mode, encoding=encoding)
@staticmethod
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=mode, encoding=encoding)
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)

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,14 +29,16 @@ 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:
pickle.dump(db, ff) with FileOpen.write_able(path, 'wb') as ff:
pickle.dump(db, ff)
def dict2lst(d): def dict2lst(d):
if len(d) > 0: if len(d) > 0:
@ -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

@ -1,6 +1,7 @@
from datetime import datetime from datetime import datetime
from flask import * from flask import *
from markupsafe import escape
# from app import Yaml # from app import Yaml
# from app.Article import get_today_article, load_freq_history # from app.Article import get_today_article, load_freq_history

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()