diff --git a/app/Login.py b/app/Login.py index 2e2cecd..d3d609e 100644 --- a/app/Login.py +++ b/app/Login.py @@ -5,11 +5,11 @@ from UseSqlite import InsertQuery, RecordQuery def md5(s): - ''' + """ MD5摘要 :param str: 字符串 :return: 经MD5以后的字符串 - ''' + """ h = hashlib.md5(s.encode(encoding='utf-8')) return h.hexdigest() @@ -46,17 +46,17 @@ def check_username_availability(username): def change_password(username, old_password, new_password): - ''' + """ 修改密码 :param username: 用户名 :param old_password: 旧的密码 :param new_password: 新密码 :return: 修改成功:True 否则:False - ''' + """ if not verify_user(username, old_password): # 旧密码错误 return False # 将用户名和密码一起加密,以免暴露不同用户的相同密码 - if verify_pass(new_password, old_password): #新旧密码一致 + if verify_pass(new_password, old_password): # 新旧密码一致 return False update_password_by_username(username, new_password) return True @@ -69,6 +69,7 @@ def get_expiry_date(username): else: return user.expiry_date + class UserName: def __init__(self, username): self.username = username diff --git a/app/UseSqlite.py b/app/UseSqlite.py index ea4baeb..90d5dae 100644 --- a/app/UseSqlite.py +++ b/app/UseSqlite.py @@ -9,6 +9,7 @@ import sqlite3 + class Sqlite3Template: def __init__(self, db_fname): self.db_fname = db_fname @@ -72,7 +73,6 @@ class RecordQuery(Sqlite3Template): return result - if __name__ == '__main__': #iq = InsertQuery('RiskDB.db') diff --git a/app/WordFreq.py b/app/WordFreq.py index 3620a41..ffdc246 100644 --- a/app/WordFreq.py +++ b/app/WordFreq.py @@ -6,6 +6,7 @@ from wordfreqCMD import remove_punctuation, freq, sort_in_descending_order import string + class WordFreq: def __init__(self, s): self.s = remove_punctuation(s) diff --git a/app/Yaml.py b/app/Yaml.py index 00974aa..01fb184 100644 --- a/app/Yaml.py +++ b/app/Yaml.py @@ -1,10 +1,10 @@ -''' +""" Yaml.py 配置文件包括: ./static/config.yml ./layout/partial/header.html ./layout/partial/footer.html -''' +""" import yaml as YAML import os @@ -15,7 +15,7 @@ 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文件 cont = f.read() # 以文本形式读取YAML yml = YAML.load(cont, Loader=YAML.FullLoader) # 加载YAML diff --git a/app/account_service.py b/app/account_service.py index 7259e15..eb396ca 100644 --- a/app/account_service.py +++ b/app/account_service.py @@ -8,10 +8,10 @@ accountService = Blueprint("accountService", __name__) ### Sign-up, login, logout ### @accountService.route("/signup", methods=['GET', 'POST']) def signup(): - ''' + """ 注册 :return: 根据注册是否成功返回不同界面 - ''' + """ if request.method == 'GET': # GET方法直接返回注册页面 return render_template('signup.html') @@ -19,12 +19,12 @@ def signup(): # POST方法需判断是否注册成功,再根据结果返回不同的内容 username = escape(request.form['username']) password = escape(request.form['password']) - - #! 添加如下代码为了过滤注册时的非法字符 + + # ! 添加如下代码为了过滤注册时的非法字符 warn = WarningMessage(username) if str(warn) != 'OK': return jsonify({'status': '3', 'warn': str(warn)}) - + available = check_username_availability(username) if not available: # 用户名不可用 return jsonify({'status': '0'}) @@ -45,10 +45,10 @@ def signup(): @accountService.route("/login", methods=['GET', 'POST']) def login(): - ''' + """ 登录 :return: 根据登录是否成功返回不同页面 - ''' + """ if request.method == 'GET': # GET请求 return render_template('login.html') @@ -73,10 +73,10 @@ def login(): @accountService.route("/logout", methods=['GET', 'POST']) def logout(): - ''' + """ 登出 :return: 重定位到主界面 - ''' + """ # 将session标记为登出状态 session['logged_in'] = False return redirect(url_for('mainpage')) @@ -84,10 +84,10 @@ def logout(): @accountService.route("/reset", methods=['GET', 'POST']) def reset(): - ''' + """ 重设密码 :return: 返回适当的页面 - ''' + """ # 下列方法用于防止未登录状态下的修改密码 if not session.get('logged_in'): return render_template('login.html') @@ -101,9 +101,9 @@ def reset(): # POST请求用于提交修改后信息 old_password = escape(request.form['old-password']) new_password = escape(request.form['new-password']) - flag = change_password(username, old_password, new_password) # flag表示是否修改成功 + flag = change_password(username, old_password, new_password) # flag表示是否修改成功 if flag: session['logged_in'] = False - return jsonify({'status':'1'}) # 修改成功 + return jsonify({'status': '1'}) # 修改成功 else: - return jsonify({'status':'2'}) # 修改失败 + return jsonify({'status': '2'}) # 修改失败 diff --git a/app/admin_service.py b/app/admin_service.py index a604b5e..959db6b 100644 --- a/app/admin_service.py +++ b/app/admin_service.py @@ -52,7 +52,7 @@ def article(): max(1, int(request.args.get("page", 1))), _article_number // _page_size + (_article_number % _page_size > 0) ) # 最小的page是1 except ValueError: - return "page parmas must be int!" + return "page params must be int!" _articles = get_page_articles(_cur_page, _page_size) for article in _articles: # 获取每篇文章的title diff --git a/app/difficulty.py b/app/difficulty.py index 03d3bfa..d500cca 100644 --- a/app/difficulty.py +++ b/app/difficulty.py @@ -65,10 +65,10 @@ def get_difficulty_level_for_user(d1, d2): def revert_dict(d): - ''' + """ In d, word is the key, and value is a list of dates. In d2 (the returned value of this function), time is the key, and the value is a list of words picked at that time. - ''' + """ d2 = {} for k in d: if type(d[k]) is list: # d[k] is a list of dates. @@ -80,7 +80,7 @@ def revert_dict(d): for time_info in lst: date = time_info[:10] # until hour - if not date in d2: + if date not in d2: d2[date] = [k] else: d2[date].append(k) @@ -105,7 +105,7 @@ def user_difficulty_level(d_user, d): word = t[0] hard = t[1] # print('WORD %s HARD %4.2f' % (word, hard)) - geometric = geometric * (hard) + geometric = geometric * hard count += 1 if count >= 10: return geometric ** (1 / count) @@ -131,7 +131,7 @@ def text_difficulty_level(s, d): for t in lst2: word = t[0] hard = t[1] - geometric = geometric * (hard) + geometric = geometric * hard count += 1 if count >= 20: # we look for n most difficult words return geometric ** (1 / count) diff --git a/app/main.py b/app/main.py index 6308f3d..2267dce 100644 --- a/app/main.py +++ b/app/main.py @@ -23,33 +23,34 @@ app.register_blueprint(adminService) path_prefix = '/var/www/wordfreq/wordfreq/' path_prefix = './' # comment this line in deployment + def get_random_image(path): - ''' + """ 返回随机图 :param path: 图片文件(JPEG格式),不包含后缀名 :return: - ''' + """ img_path = random.choice(glob.glob(os.path.join(path, '*.jpg'))) return img_path[img_path.rfind('/static'):] def get_random_ads(): - ''' + """ 返回随机广告 :return: 一个广告(包含HTML标签) - ''' + """ return random.choice(['个性化分析精准提升', '你的专有单词本', '智能捕捉阅读弱点,针对性提高你的阅读水平']) def appears_in_test(word, d): - ''' + """ 如果字符串里没有指定的单词,则返回逗号加单词 :param word: 指定单词 :param d: 字符串 :return: 逗号加单词 - ''' - if not word in d: + """ + if word not in d: return '' else: return ','.join(d[word]) @@ -57,10 +58,10 @@ def appears_in_test(word, d): @app.route("/mark", methods=['GET', 'POST']) def mark_word(): - ''' + """ 标记单词 :return: 重定位到主界面 - ''' + """ if request.method == 'POST': d = load_freq_history(path_prefix + 'static/frequency/frequency.p') lst_history = pickle_idea.dict_to_lst(d) @@ -70,16 +71,16 @@ def mark_word(): d = pickle_idea.merge_frequency(lst, lst_history) pickle_idea.save_frequency_to_pickle(d, path_prefix + 'static/frequency/frequency.p') return redirect(url_for('mainpage')) - else: # 不回应GET请求 + else: # 不回应GET请求 return 'Under construction' @app.route("/", methods=['GET', 'POST']) def mainpage(): - ''' + """ 根据GET或POST方法来返回不同的主界面 :return: 主界面 - ''' + """ if request.method == 'POST': # when we submit a form content = escape(request.form['content']) f = WordFreq(content) diff --git a/app/pickle_idea.py b/app/pickle_idea.py index 4f193ff..3e87796 100644 --- a/app/pickle_idea.py +++ b/app/pickle_idea.py @@ -11,15 +11,15 @@ from datetime import datetime def lst_to_dict(lst, d): - ''' + """ Store the information in list lst to dictionary d. Note: nothing is returned. - ''' + """ for x in lst: word = x[0] freq = x[1] - if not word in d: + if word not in d: d[word] = freq else: d[word] += freq @@ -73,15 +73,15 @@ def familiar(path, word): fp = open(path, "wb") pickle.dump(dic, fp) + if __name__ == '__main__': - lst1 = [('apple',2), ('banana',1)] + lst1 = [('apple', 2), ('banana', 1)] d = {} - lst_to_dict(lst1, d) # d will change - save_frequency_to_pickle(d, 'frequency.p') # frequency.p is our database + lst_to_dict(lst1, d) # d will change + save_frequency_to_pickle(d, 'frequency.p') # frequency.p is our database - - lst2 = [('banana',2), ('orange', 4)] + lst2 = [('banana', 2), ('orange', 4)] d = load_record('frequency.p') lst1 = dict_to_lst(d) d = merge_frequency(lst2, lst1) diff --git a/app/pickle_idea2.py b/app/pickle_idea2.py index f3104a5..3771bae 100644 --- a/app/pickle_idea2.py +++ b/app/pickle_idea2.py @@ -12,22 +12,22 @@ import pickle from datetime import datetime -def lst2dict(lst, d): - ''' +def lst_to_dict(lst, d): + """ Store the information in list lst to dictionary d. Note: nothing is returned. - ''' + """ for x in lst: word = x[0] dates = x[1] - if not word in d: + if word not in d: d[word] = dates else: d[word] += dates -def deleteRecord(path, word): +def delete_record(path, word): with open(path, 'rb') as f: db = pickle.load(f) try: @@ -35,9 +35,10 @@ def deleteRecord(path, word): except KeyError: print("sorry") with open(path, 'wb') as ff: - pickle.dump(db, ff) + pickle.dump(db, ff) -def dict2lst(d): + +def dict_to_lst(d): if len(d) > 0: keys = list(d.keys()) if isinstance(d[keys[0]], int): @@ -46,14 +47,15 @@ def dict2lst(d): lst.append((k, [datetime.now().strftime('%Y%m%d%H%M')])) return lst elif isinstance(d[keys[0]], list): - return list(d.items()) # a list of (key, value) pairs + return list(d.items()) # a list of (key, value) pairs return [] + def merge_frequency(lst1, lst2): d = {} - lst2dict(lst1, d) - lst2dict(lst2, d) + lst_to_dict(lst1, d) + lst_to_dict(lst2, d) return d @@ -69,23 +71,22 @@ def save_frequency_to_pickle(d, pickle_fname): 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: - if not k in exclusion_lst and not k.isnumeric() and not len(k) < 2: - d2[k] = list(sorted(d[k])) # 原先这里是d2[k] = list(sorted(set(d[k]))) + if k not in exclusion_lst and not k.isnumeric() and not len(k) < 2: + d2[k] = list(sorted(d[k])) # 原先这里是d2[k] = list(sorted(set(d[k]))) pickle.dump(d2, f) f.close() - if __name__ == '__main__': - lst1 = [('apple',['201910251437', '201910251438']), ('banana',['201910251439'])] + lst1 = [('apple',['201910251437', '201910251438']), ('banana', ['201910251439'])] d = {} - lst2dict(lst1, d) # d will change + lst_to_dict(lst1, d) # d will change save_frequency_to_pickle(d, 'frequency.p') # frequency.p is our database lst2 = [('banana',['201910251439']), ('orange', ['201910251440', '201910251439'])] d = load_record('frequency.p') - lst1 = dict2lst(d) + lst1 = dict_to_lst(d) d = merge_frequency(lst2, lst1) print(d) diff --git a/app/user_service.py b/app/user_service.py index 75971a6..a480247 100644 --- a/app/user_service.py +++ b/app/user_service.py @@ -21,6 +21,7 @@ userService = Blueprint("user_bp", __name__) path_prefix = '/var/www/wordfreq/wordfreq/' path_prefix = './' # comment this line in deployment + @userService.route("/get_next_article/",methods=['GET','POST']) def get_next_article(username): user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username) @@ -42,13 +43,14 @@ def get_next_article(username): return 'Under construction' return json.dumps(data) + @userService.route("/get_pre_article/",methods=['GET']) def get_pre_article(username): user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username) if request.method == 'GET': visited_articles = session.get("visited_articles") - if(visited_articles["index"]==0): - data='' + if visited_articles["index"] == 0: + data = '' else: visited_articles["index"] -= 1 # 上一篇,index-=1 if visited_articles['article_ids'][-1] == "null": # 如果当前还是“null”,则将“null”pop出来 @@ -58,19 +60,20 @@ def get_pre_article(username): data = { 'visited_articles': visited_articles, 'today_article': today_article, - 'result_of_generate_article':result_of_generate_article + 'result_of_generate_article': result_of_generate_article } return json.dumps(data) + @userService.route("///unfamiliar", methods=['GET', 'POST']) def unfamiliar(username, word): - ''' + """ :param username: :param word: :return: - ''' - user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username) + """ + user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % username pickle_idea.unfamiliar(user_freq_record, word) session['thisWord'] = word # 1. put a word into session session['time'] = 1 @@ -79,13 +82,13 @@ def unfamiliar(username, word): @userService.route("///familiar", methods=['GET', 'POST']) def familiar(username, word): - ''' + """ :param username: :param word: :return: - ''' - user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username) + """ + user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % username pickle_idea.familiar(user_freq_record, word) session['thisWord'] = word # 1. put a word into session session['time'] = 1 @@ -94,14 +97,14 @@ def familiar(username, word): @userService.route("///del", methods=['GET', 'POST']) def delete_word(username, word): - ''' + """ 删除单词 :param username: 用户名 :param word: 单词 :return: 重定位到用户界面 - ''' + """ user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username) - pickle_idea2.deleteRecord(user_freq_record, word) + pickle_idea2.delete_record(user_freq_record, word) # 模板userpage_get.html中删除单词是异步执行,而flash的信息后续是同步执行的,所以注释这段代码;同时如果这里使用flash但不提取信息,则会影响 signup.html的显示。bug复现:删除单词后,点击退出,点击注册,注册页面就会出现提示信息 # flash(f'{word} is no longer in your word list.') return "success" @@ -109,11 +112,11 @@ def delete_word(username, word): @userService.route("//userpage", methods=['GET', 'POST']) def userpage(username): - ''' + """ 用户界面 :param username: 用户名 :return: 返回用户界面 - ''' + """ # 未登录,跳转到未登录界面 if not session.get('logged_in'): return render_template('not_login.html') @@ -136,7 +139,7 @@ def userpage(username): elif request.method == 'GET': # when we load a html page d = load_freq_history(user_freq_record) - lst = pickle_idea2.dict2lst(d) + lst = pickle_idea2.dict_to_lst(d) lst2 = [] for t in lst: lst2.append((t[0], len(t[1]))) @@ -159,19 +162,20 @@ def userpage(username): yml=Yaml.yml, words=words) + @userService.route("//mark", methods=['GET', 'POST']) def user_mark_word(username): - ''' + """ 标记单词 :param username: 用户名 :return: 重定位到用户界面 - ''' + """ username = session[username] - user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username) + user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % username if request.method == 'POST': # 提交标记的单词 d = load_freq_history(user_freq_record) - lst_history = pickle_idea2.dict2lst(d) + lst_history = pickle_idea2.dict_to_lst(d) lst = [] for word in request.form.getlist('marked'): lst.append((word, [get_time()])) @@ -183,9 +187,9 @@ def user_mark_word(username): def get_time(): - ''' + """ 获取当前时间 :return: 当前时间 - ''' + """ return datetime.now().strftime('%Y%m%d%H%M') # upper to minutes diff --git a/app/wordfreqCMD.py b/app/wordfreqCMD.py index 69065d6..53ea7f8 100644 --- a/app/wordfreqCMD.py +++ b/app/wordfreqCMD.py @@ -11,12 +11,12 @@ import pickle_idea def freq(s): - ''' + """ 功能: 把字符串转成列表。 目的是得到每个单词的频率。 输入: 字符串 输出: 列表, 列表里包含一组元组,每个元组包含单词与单词的频率。 比如 [('apple', 2), ('banana', 1)] 注意事项: 首先要把字符串转成小写。原因是。。。 - ''' + """ result = [] s = s.lower() # 字母转小写 @@ -71,9 +71,9 @@ def sort_in_ascending_order(lst): # 单词按频率降序排列 def make_html_page(lst, fname): # 只是在wordfreqCMD.py中的main函数中调用,所以不做修改 - ''' + """ 功能:把lst的信息存到fname中,以html格式。 - ''' + """ s = '' count = 1 for x in lst: