优化批量收录新题的流程, 终端会显示存在错误的地方
This commit is contained in:
parent
5459ec264e
commit
eaa944e7eb
|
|
@ -438,6 +438,7 @@ def GenerateProblemListFromString2024(data): #从来自.tex文件的字符串生
|
|||
data = re.findall(r"\\begin\{document\}([\s\S]*?)\\end\{document\}",data)[0]
|
||||
except:
|
||||
pass
|
||||
data = data + "\n\\end{enumerate}"
|
||||
data = re.sub(r"\n{2,}","\n",data)
|
||||
data = re.sub(r"\\definecolor[^\n]*\n","\n",data)
|
||||
data = re.sub(r"\\begin\{tcolorbox\}[\s\S]*?\\end\{tcolorbox\}","\n",data)
|
||||
|
|
@ -545,63 +546,69 @@ def AddProblemstoDict2024nocommit(startingid,raworigin,problems,editor,indexed,c
|
|||
currentsuffix = problems[0][1]
|
||||
problemindex = 0
|
||||
for p_and_suffix_and_meta in problems:
|
||||
p, suffix, meta = p_and_suffix_and_meta
|
||||
pid = str(id).zfill(6)
|
||||
if suffix == currentsuffix:
|
||||
problemindex += 1
|
||||
else:
|
||||
problemindex = 1
|
||||
currentsuffix = suffix
|
||||
if indexed:
|
||||
origin = {"来源": raworigin + suffix, "题号": problemindex}
|
||||
else:
|
||||
origin = {"来源": raworigin + suffix}
|
||||
if not "rep" in meta:
|
||||
if "blank" in p:
|
||||
genre = "填空题"
|
||||
space = ""
|
||||
elif "bracket" in p:
|
||||
genre = "选择题"
|
||||
space = ""
|
||||
try:
|
||||
p, suffix, meta = p_and_suffix_and_meta
|
||||
pid = str(id).zfill(6)
|
||||
if suffix == currentsuffix:
|
||||
problemindex += 1
|
||||
else:
|
||||
genre = "解答题"
|
||||
space = "4em"
|
||||
sql = "INSERT INTO problems (ID,content,genre,origin,space) VALUE (%s,%s,%s,%s,%s);"
|
||||
val = (pid,p.strip(),genre,json.dumps(origin,ensure_ascii=False),space)
|
||||
mycursor.execute(sql,val)
|
||||
sql = "INSERT INTO edit_history (ID,date,editor) VALUE (%s,%s,%s);"
|
||||
val = (pid,GetDate(),editor.strip())
|
||||
mycursor.execute(sql,val)
|
||||
sql = "INSERT INTO logs (DATE,TIME,username,action,id,db_content) VALUE (%s,%s,%s,%s,%s,%s);"
|
||||
val = (GetDate(),GetTime(),get_git_username(),"新增题目",pid,p.strip())
|
||||
mycursor.execute(sql,val)
|
||||
if "same" in meta:
|
||||
sql = "INSERT INTO same (ID,SAME_ID) VALUE (%s,%s);"
|
||||
for sid in meta["same"]:
|
||||
val = (pid, sid)
|
||||
if pid > sid:
|
||||
val = (sid, pid)
|
||||
mycursor.execute(sql,val)
|
||||
if "related" in meta:
|
||||
sql = "INSERT INTO related (ID,RELATED_ID) VALUE (%s,%s);"
|
||||
for rid in meta["related"]:
|
||||
val = (pid, rid)
|
||||
if pid > rid:
|
||||
val = (rid, pid)
|
||||
mycursor.execute(sql,val)
|
||||
if "unrelated" in meta:
|
||||
sql = "INSERT INTO unrelated (ID,UNRELATED_ID) VALUE (%s,%s);"
|
||||
for uid in meta["unrelated"]:
|
||||
val = (pid, uid)
|
||||
if pid > uid:
|
||||
val = (uid, pid)
|
||||
mycursor.execute(sql,val)
|
||||
print(f"已收录题号: {pid}, 题目类型: {genre}, 题目来源: {origin['来源'] + (('试题'+str(origin['题号'])) if '题号' in origin else '')}, 题目内容: {p.strip()}")
|
||||
id += 1
|
||||
idlist.append(pid)
|
||||
else:
|
||||
idlist.append(meta["rep"])
|
||||
print(f"该题 {idlist[-1]} {p} 已在题库中, 不必收录.")
|
||||
problemindex = 1
|
||||
currentsuffix = suffix
|
||||
if indexed:
|
||||
origin = {"来源": raworigin + suffix, "题号": problemindex}
|
||||
else:
|
||||
origin = {"来源": raworigin + suffix}
|
||||
if not "rep" in meta:
|
||||
if "blank" in p:
|
||||
genre = "填空题"
|
||||
space = ""
|
||||
elif "bracket" in p:
|
||||
genre = "选择题"
|
||||
space = ""
|
||||
else:
|
||||
genre = "解答题"
|
||||
space = "4em"
|
||||
sql = "INSERT INTO problems (ID,content,genre,origin,space) VALUE (%s,%s,%s,%s,%s);"
|
||||
val = (pid,p.strip(),genre,json.dumps(origin,ensure_ascii=False),space)
|
||||
mycursor.execute(sql,val)
|
||||
sql = "INSERT INTO edit_history (ID,date,editor) VALUE (%s,%s,%s);"
|
||||
val = (pid,GetDate(),editor.strip())
|
||||
mycursor.execute(sql,val)
|
||||
sql = "INSERT INTO logs (DATE,TIME,username,action,id,db_content) VALUE (%s,%s,%s,%s,%s,%s);"
|
||||
val = (GetDate(),GetTime(),get_git_username(),"新增题目",pid,p.strip())
|
||||
mycursor.execute(sql,val)
|
||||
if "same" in meta:
|
||||
sql = "INSERT INTO same (ID,SAME_ID) VALUE (%s,%s);"
|
||||
for sid in meta["same"]:
|
||||
val = (pid, sid)
|
||||
if pid > sid:
|
||||
val = (sid, pid)
|
||||
mycursor.execute(sql,val)
|
||||
if "related" in meta:
|
||||
sql = "INSERT INTO related (ID,RELATED_ID) VALUE (%s,%s);"
|
||||
for rid in meta["related"]:
|
||||
val = (pid, rid)
|
||||
if pid > rid:
|
||||
val = (rid, pid)
|
||||
mycursor.execute(sql,val)
|
||||
if "unrelated" in meta:
|
||||
sql = "INSERT INTO unrelated (ID,UNRELATED_ID) VALUE (%s,%s);"
|
||||
for uid in meta["unrelated"]:
|
||||
val = (pid, uid)
|
||||
if pid > uid:
|
||||
val = (uid, pid)
|
||||
mycursor.execute(sql,val)
|
||||
print(f"已收录题号: {pid}, 题目类型: {genre}, 题目来源: {origin['来源'] + (('试题'+str(origin['题号'])) if '题号' in origin else '')}, 题目内容: {p.strip()}")
|
||||
id += 1
|
||||
idlist.append(pid)
|
||||
else:
|
||||
idlist.append(meta["rep"])
|
||||
print(f"该题 {idlist[-1]} {p} 已在题库中, 不必收录.")
|
||||
except Exception as e:
|
||||
print(f"错误类型: {type(e).__name__}")
|
||||
print(f"错误的题目: {p_and_suffix_and_meta}")
|
||||
idlist = []
|
||||
break
|
||||
return idlist
|
||||
|
||||
def CreateIDLinks(old_id_list,new_id_list): #建立已有id和新id之间的联系, thedict为可选, 选中的话即为当前字典, 会从new_id_list中排除当前字典中有的项
|
||||
|
|
|
|||
|
|
@ -15,10 +15,12 @@ class MyWindow_bdsl(QWidget,Ui_Form):
|
|||
|
||||
def bind(self):
|
||||
self.suffix_checked = True
|
||||
self.pushButton_commit.setDisabled(True)
|
||||
self.checkBox_suffix.toggled.connect(self.togglesuffix)
|
||||
self.pushButton_step1.clicked.connect(self.execstep1)
|
||||
self.pushButton_step2.clicked.connect(self.execstep2)
|
||||
self.pushButton_commit.clicked.connect(self.commitchange)
|
||||
self.plainTextEdit_compared.textChanged.connect(self.commitdisable)
|
||||
def togglesuffix(self):
|
||||
self.suffix_checked = self.checkBox_suffix.isChecked()
|
||||
if self.suffix_checked:
|
||||
|
|
@ -81,6 +83,7 @@ class MyWindow_bdsl(QWidget,Ui_Form):
|
|||
self.plainTextEdit_compared.setPlainText(texdata)
|
||||
|
||||
def execstep2(self):
|
||||
idlist = []
|
||||
starting_id = self.lineEdit_startingid.text().strip().zfill(6)
|
||||
raworigin = self.lineEdit_origin.text().strip()
|
||||
data = self.plainTextEdit_compared.toPlainText()
|
||||
|
|
@ -95,9 +98,18 @@ class MyWindow_bdsl(QWidget,Ui_Form):
|
|||
nextspareid = NextSpareID(starting_id,self.database_name)
|
||||
self.db = connect(hostname = db_host, port= db_port, username= db_user, pwd = db_pwd, db= self.database_name)
|
||||
self.cursor = self.db.cursor()
|
||||
rolled_back = False
|
||||
idlist = AddProblemstoDict2024nocommit(nextspareid,raworigin,problems,editor,Indexed,self.cursor)
|
||||
AppendTextFile(f"{GetDate()}-{GetTime()}\n{generate_exp(idlist)}",idlistpath)
|
||||
os.system(f"code {idlistpath}")
|
||||
if idlist == []:
|
||||
print("有错误, 改变已回滚.")
|
||||
self.db.rollback()
|
||||
rolled_back = True
|
||||
if not rolled_back:
|
||||
AppendTextFile(f"{GetDate()}-{GetTime()}\n{generate_exp(idlist)}",idlistpath)
|
||||
os.system(f"code {idlistpath}")
|
||||
self.pushButton_commit.setEnabled(True)
|
||||
def commitdisable(self):
|
||||
self.pushButton_commit.setDisabled(True)
|
||||
|
||||
def commitchange(self):
|
||||
self.db.commit()
|
||||
|
|
|
|||
Reference in New Issue