From ea16ea66735936d398612557ce557a8ca66d5e07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=92=B1=E9=AA=8F=E7=90=AA?= <512860335@qq.com> Date: Fri, 26 May 2023 22:18:11 +0800 Subject: [PATCH] =?UTF-8?q?Pylint=E9=83=A8=E5=88=86=EF=BC=9A=201.=20?= =?UTF-8?q?=E8=A1=A5=E5=85=85module=20docstring=EF=BC=8C=E5=8D=B3=E6=96=87?= =?UTF-8?q?=E6=A1=A3=E9=A1=B6=E9=83=A8=E7=9A=84=E7=AE=80=E8=A6=81=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20=EF=BC=88=E6=88=91=E5=81=9A=E7=9A=84=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=E5=BE=88=E7=AE=80=E9=99=8B=EF=BC=8C=E5=8F=AA=E6=98=AF?= =?UTF-8?q?=E4=B8=BA=E4=BA=86=E9=80=9A=E8=BF=87pylint=E7=9A=84=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E8=80=8C=E4=B8=BA=E4=B9=8B=EF=BC=89=202.=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E4=BA=86if-else-return=EF=BC=8C=E5=8D=B3=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E4=BA=86=E4=B8=8D=E5=BF=85=E8=A6=81=E7=9A=84elif?= =?UTF-8?q?=E6=88=96else=203.=20=E6=8B=86=E5=88=86=E4=BA=86=E6=89=80?= =?UTF-8?q?=E6=9C=89=E5=9C=A8=E5=90=8C=E4=B8=80=E8=A1=8C=E7=9A=84=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E5=BC=95=E7=94=A8=EF=BC=88=E4=B8=8D=E5=90=8C=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E7=9A=84=E5=BC=95=E7=94=A8=E9=9C=80=E8=A6=81=E6=94=BE?= =?UTF-8?q?=E5=9C=A8=E4=B8=8D=E5=90=8C=E8=A1=8C=EF=BC=8C=E4=BB=A5=E4=BE=BF?= =?UTF-8?q?=E7=90=86=E8=A7=A3=E5=92=8C=E9=98=85=E8=AF=BB=EF=BC=89=204.=20?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=BA=86=E9=83=A8=E5=88=86=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E8=A2=AB=E4=BD=BF=E7=94=A8=E7=9A=84=E5=BC=95=E7=94=A8=EF=BC=88?= =?UTF-8?q?=E6=9C=89=E7=9A=84=E5=BC=95=E7=94=A8=E5=9C=A8=E6=96=87=E4=BB=B6?= =?UTF-8?q?A=E4=B8=AD=E8=A2=AB=E4=BD=BF=E7=94=A8=E4=BA=86=EF=BC=8C?= =?UTF-8?q?=E4=BD=86import=E5=8D=B4=E5=86=99=E5=9C=A8=E4=BA=86=E6=96=87?= =?UTF-8?q?=E4=BB=B6B=EF=BC=8C=E4=B8=94=E6=96=87=E4=BB=B6B=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E4=BD=BF=E7=94=A8=E8=BF=99=E4=BA=9B=E5=BC=95=E7=94=A8?= =?UTF-8?q?=EF=BC=8C=E8=80=8C=E6=96=87=E4=BB=B6A=E5=BC=95=E7=94=A8?= =?UTF-8?q?=E4=BA=86=E6=96=87=E4=BB=B6B=EF=BC=8C=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E6=96=87=E4=BB=B6B=E7=9A=84=E6=89=80=E6=9C=89=E5=BC=95?= =?UTF-8?q?=E7=94=A8=E4=B9=9F=E8=A2=ABA=E5=BC=95=E7=94=A8=EF=BC=8C?= =?UTF-8?q?=E8=BF=99=E4=BC=BC=E4=B9=8E=E5=B9=B6=E4=B8=8D=E5=90=88=E7=90=86?= =?UTF-8?q?=EF=BC=89=205.=20=E4=BF=AE=E6=94=B9=E4=BA=86=E5=BC=95=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E9=A1=BA=E5=BA=8F=EF=BC=88os=E3=80=81random=E7=AD=89?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E5=BC=95=E7=94=A8=E5=BA=94=E6=94=BE=E5=9C=A8?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E6=A8=A1=E5=9D=97=E5=BC=95=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E4=B8=8A=E6=96=B9=EF=BC=89=206.=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=BA=86=E8=BF=87=E9=95=BF=E7=9A=84=E4=BB=A3=E7=A0=81=EF=BC=8C?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=E9=99=A4=E5=A4=96=EF=BC=88=E6=AF=8F=E8=A1=8C?= =?UTF-8?q?=E9=99=90=E5=88=B6=E5=9C=A8100=E4=B8=AA=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=BB=A5=E5=86=85=EF=BC=89=207.=20=E4=BC=98=E5=8C=96=E4=BA=86?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6=E5=88=A4=E6=96=AD=E7=9A=84=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E7=AC=A6=E5=8F=B7=EF=BC=9A=20is=E5=92=8Cis=20not=20=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E5=88=A4=E6=96=AD=E4=B8=A4=E4=B8=AA=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E6=8C=87=E5=90=91=E5=90=8C=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE=20=3D=3D=20=E5=92=8C=20!=3D=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E6=AF=94=E8=BE=83=E4=B8=A4=E4=B8=AA=E5=8F=98=E9=87=8F?= =?UTF-8?q?=208.=20=E4=B8=BA=E6=AF=8F=E4=B8=AApy=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86"last=20new=20line"=EF=BC=8C?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=BA=86=E5=A4=9A=E4=BD=99=E7=9A=84=E5=B0=BE?= =?UTF-8?q?=E9=83=A8=E7=A9=BA=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pylint提示了但是没有进行的操作: 1. 将字符串改为f-string的格式 因为f-string需要python3.6以上的版本,为了防止出现版本问题,故不做修改 2. 将所有函数内的return内容改为一致的类型 因为原有的代码逻辑已经确定,修改return的类型会导致方法的无法使用,故不做修改 3. 将标识符的格式改为"snake_case naming style" 标识符的修改内容过于庞大,且可能出现程序不可预见的错误,在pylint中重要等级为0,故不做修改 JSLint Part: 1. 提高代码的可读性:将部分超过80个字符的代码行拆解开(由于文字注释的存在,很多语句仅注释就超过了80个字符,此类型的语句行不做修改) 2. 用===与!==代替==与!=的判断 3. 将i++改为i+=1 4. 分离for循环中的let与var到其他行中,以适应JSLint在循环语句中对var和let的偏好 5. 给三元表达式外围添加圆括号,避免出现优先级的问题 6. 将正则表达式修改为JSLint要求的多行模式 疑问: 形如: for (let i = 0; i < list.length; i+=1) { 的语句,JSLint报错: [JSLint was unable to finish] Unexpected 'let'. 但是网上搜索到的结果是ES6是支持let的使用的,暂且不做修改。 --- app/Article.py | 6 ++- app/Login.py | 29 +++++++---- app/WordFreq.py | 10 ++-- app/account_service.py | 86 +++++++++++++++----------------- app/difficulty.py | 87 ++++++++++++++------------------- app/main.py | 10 ++-- app/pickle_idea.py | 15 +++--- app/pickle_idea2.py | 2 +- app/static/js/fillword.js | 25 ++++++---- app/static/js/highlight.js | 78 ++++++++++++++++++----------- app/static/js/word_operation.js | 59 +++++++++++++--------- app/user_service.py | 9 ++-- app/wordfreqCMD.py | 65 +++++++++++------------- 13 files changed, 254 insertions(+), 227 deletions(-) diff --git a/app/Article.py b/app/Article.py index 04a32ea..824df0c 100644 --- a/app/Article.py +++ b/app/Article.py @@ -1,3 +1,6 @@ +""" +This module provides functions about article +""" from WordFreq import WordFreq from wordfreqCMD import youdao_link, sort_in_descending_order from UseSqlite import InsertQuery, RecordQuery @@ -46,7 +49,6 @@ def get_today_article(user_word_list, articleID): d1 = load_freq_history(path_prefix + 'static/frequency/frequency.p') d2 = load_freq_history(path_prefix + 'static/words_and_tests.p') d3 = get_difficulty_level(d1, d2) - d = {} d_user = load_freq_history(user_word_list) user_level = user_difficulty_level(d_user, d3) # more consideration as user's behaviour is dynamic. Time factor should be considered. @@ -133,4 +135,4 @@ def get_answer_part(s): html_code += '\n' html_code += '\n' html_code += '\n' % ('\n'.join(result)) - return html_code \ No newline at end of file + return html_code diff --git a/app/Login.py b/app/Login.py index 8e0030b..5ada69f 100644 --- a/app/Login.py +++ b/app/Login.py @@ -1,3 +1,6 @@ +""" +This module provides methods for Login +""" import hashlib import string from datetime import datetime, timedelta @@ -7,15 +10,17 @@ path_prefix = '/var/www/wordfreq/wordfreq/' path_prefix = './' # comment this line in deployment def verify_pass(newpass,oldpass): - if(newpass==oldpass): + if newpass==oldpass: return True - def verify_user(username, password): rq = RecordQuery(path_prefix + 'static/wordfreqapp.db') password = md5(username + password) - rq.instructions_with_parameters("SELECT * FROM user WHERE name=:username AND password=:password", dict( - username=username, password=password)) # the named style https://docs.python.org/3/library/sqlite3.html + rq.instructions_with_parameters( + "SELECT * FROM user WHERE name=:username AND password=:password", + dict( # the named style https://docs.python.org/3/library/sqlite3.html + username=username, + password=password)) rq.do_with_parameters() result = rq.get_results() return result != [] @@ -23,12 +28,19 @@ def verify_user(username, password): def add_user(username, password): start_date = datetime.now().strftime('%Y%m%d') - expiry_date = (datetime.now() + timedelta(days=30)).strftime('%Y%m%d') # will expire after 30 days + # will expire after 30 days + expiry_date = (datetime.now() + timedelta(days=30)).strftime('%Y%m%d') # 将用户名和密码一起加密,以免暴露不同用户的相同密码 password = md5(username + password) rq = InsertQuery(path_prefix + 'static/wordfreqapp.db') - rq.instructions_with_parameters("INSERT INTO user VALUES (:username, :password, :start_date, :expiry_date)", dict( - username=username, password=password, start_date=start_date, expiry_date=expiry_date)) + rq.instructions_with_parameters( + "INSERT INTO user VALUES (:username, :password, :start_date, :expiry_date)", + dict( + username=username, + password=password, + start_date=start_date, + expiry_date=expiry_date + )) rq.do_with_parameters() @@ -96,7 +108,7 @@ class UserName: if ' ' in self.username: # a user name must not include a whitespace return 'Whitespace is not allowed in the user name.' for c in self.username: # a user name must not include special characters, except non-leading periods or underscores - if c in string.punctuation and c is not '.' and c is not '_': + if c in string.punctuation and c != '.' and c != '_': return f'{c} is not allowed in the user name.' if self.username in ['signup', 'login', 'logout', 'reset', 'mark', 'back', 'unfamiliar', 'familiar', 'del']: return 'You used a restricted word as your user name. Please come up with a better one.' @@ -110,4 +122,3 @@ class WarningMessage: def __str__(self): return UserName(self.s).validate() - diff --git a/app/WordFreq.py b/app/WordFreq.py index 3620a41..766ecaf 100644 --- a/app/WordFreq.py +++ b/app/WordFreq.py @@ -2,9 +2,12 @@ # Copyright 2019 (C) Hui Lan # Written permission must be obtained from the author for commercial uses. ########################################################################### - -from wordfreqCMD import remove_punctuation, freq, sort_in_descending_order +""" +This module produces word frequency +""" import string +from wordfreqCMD import remove_punctuation, freq, sort_in_descending_order + class WordFreq: def __init__(self, s): @@ -17,9 +20,8 @@ class WordFreq: if len(word) > 0 and word[0] in string.ascii_letters: lst.append(t) return sort_in_descending_order(lst) - + if __name__ == '__main__': f = WordFreq('BANANA; Banana, apple ORANGE Banana banana.') print(f.get_freq()) - diff --git a/app/account_service.py b/app/account_service.py index 9b1c46b..eace163 100644 --- a/app/account_service.py +++ b/app/account_service.py @@ -1,54 +1,54 @@ +""" +This module provides services about account. +""" from flask import * -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 # 初始化蓝图 accountService = Blueprint("accountService", __name__) -### Sign-up, login, logout ### + +# Sign-up, login, logout @accountService.route("/signup", methods=['GET', 'POST']) def signup(): ''' 注册 :return: 根据注册是否成功返回不同界面 ''' + # GET方法直接返回注册页面 if request.method == 'GET': - # GET方法直接返回注册页面 return render_template('signup.html') - elif request.method == 'POST': + if request.method == 'POST': # POST方法需判断是否注册成功,再根据结果返回不同的内容 username = escape(request.form['username']) password = escape(request.form['password']) password2 = escape(request.form['password2']) - #! 添加如下代码为了过滤注册时的非法字符 warn = WarningMessage(username) if str(warn) != 'OK': return str(warn) - available = check_username_availability(username) if not available: # 用户名不可用 flash('用户名 %s 已经被注册。' % (username)) return render_template('signup.html') - elif len(password.strip()) < 4: # 密码过短 + if len(password.strip()) < 4: # 密码过短 return '密码过于简单。' - elif password != password2: + if password != password2: return '确认密码与输入密码不一致!' - else: # 添加账户信息 - add_user(username, password) - verified = verify_user(username, password) - if verified: - # 写入session - session['logged_in'] = True - session[username] = username - session['username'] = username - session['expiry_date'] = get_expiry_date(username) - session['articleID'] = None - return '

恭喜,你已成功注册, 你的用户名是 %s

\ -

开始使用 返回首页

' % (username, username, username) - else: - return '用户名密码验证失败。' - + add_user(username, password)# 添加账户信息 + verified = verify_user(username, password) + if verified: + # 写入session + session['logged_in'] = True + session[username] = username + session['username'] = username + session['expiry_date'] = get_expiry_date(username) + session['articleID'] = None + return '

恭喜,你已成功注册, 你的用户名是 %s

\ +

开始使用 返回首页

' % (username, username, username) + return '用户名密码验证失败。' @accountService.route("/login", methods=['GET', 'POST']) @@ -62,11 +62,10 @@ def login(): if not session.get('logged_in'): # 未登录,返回登录页面 return render_template('login.html') - else: - # 已登录,提示信息并显示登出按钮 - return '你已登录 %s。 登出点击这里。' % ( + # 已登录,提示信息并显示登出按钮 + return '你已登录 %s。 登出点击这里。' % ( session['username'], session['username']) - elif request.method == 'POST': + if request.method == 'POST': # POST方法用于判断登录是否成功 # check database and verify user username = escape(request.form['username']) @@ -81,8 +80,7 @@ def login(): session['expiry_date'] = user_expiry_date session['articleID'] = None return redirect(url_for('user_bp.userpage', username=username)) - else: - return '无法通过验证。' + return '无法通过验证。' @accountService.route("/logout", methods=['GET', 'POST']) @@ -111,21 +109,18 @@ def reset(): if request.method == 'GET': # GET请求返回修改密码页面 return render_template('reset.html', username=session['username'], state='wait') - else: - # POST请求用于提交修改后信息 - old_password = escape(request.form['old-password']) - new_password = escape(request.form['new-password']) - - re_new_password = escape(request.form['re-new-password']) # 确认新密码 - if re_new_password != new_password: #验证新密码两次输入是否相同 - return '新密码不匹配,请重新输入' - if len(new_password) < 4: #验证新密码长度,原则参照注册模块 - return '密码过于简单。(密码长度至少4位)' - - flag = change_password(username, old_password, new_password) # flag表示是否修改成功 - if flag: - session['logged_in'] = False - return \ + # POST请求用于提交修改后信息 + old_password = escape(request.form['old-password']) + new_password = escape(request.form['new-password']) + re_new_password = escape(request.form['re-new-password']) # 确认新密码 + if re_new_password != new_password: #验证新密码两次输入是否相同 + return '新密码不匹配,请重新输入' + if len(new_password) < 4: #验证新密码长度,原则参照注册模块 + return '密码过于简单。(密码长度至少4位)' + flag = change_password(username, old_password, new_password) # flag表示是否修改成功 + if flag: + session['logged_in'] = False + return \ '''