diff --git a/工具v2/database_tools.py b/工具v2/database_tools.py index 8898197f..0a4007dd 100644 --- a/工具v2/database_tools.py +++ b/工具v2/database_tools.py @@ -448,7 +448,7 @@ def AppendUsageData(prodict,field_id_and_content): #添加使用记录数据 output += "\n".join(oldlist)+"\n\n" return (field,id,content,output) #返回四元组: 题号, 字段, 内容, 待确定是否要添加的字符串(不含FORCE字样的行为旧结果,含FORCE字样的行为新结果,FORCE是运行后强制添加) -def ModifyMetadata(prodict,objdict,fieldsdict,metadatafilepath,pendingdatafilepath): #metadata自动修改, 根据字段自适应修改, 参数为题库字典, 目标字典, 字段字典, metadata文本文件路径, 待确定是否替换的内容的存放路径 +def ImportMetadata(prodict,objdict,fieldsdict,metadatafilepath,pendingdatafilepath): #metadata自动修改, 根据字段自适应修改, 参数为题库字典, 目标字典, 字段字典, metadata文本文件路径, 待确定是否替换的内容的存放路径 data_to_modify = ObtainDatatoModify(metadatafilepath,fieldsdict) for item in data_to_modify: field = item[0] @@ -516,5 +516,52 @@ def GenerateTexDataforEditing(id_string,prodict,templatefilepath,editor): # 根 return texdata # 返回Tex文件的字符串, 待保存至tex文件 +def GetEditedProblems(string): #从.tex文件所读取的字符串中取出正文内容, 并切割成以题号为key的字典, 内容为[题目内容, 答案, 解答与提示]三元数组 + bodydata = re.findall(r"\\begin\{enumerate\}([\s\S]*)\\end\{enumerate\}",string)[0]+r"\item" + problems = re.findall(r"\((\d{6})\)([\s\S]*?)\\item",bodydata) + edited_dict = {} + for problem in problems: + id, pro_string = problem + edited_dict[id] = parseProblem(pro_string) + return edited_dict # 返回以题号为key, 内容为[题目内容, 答案, 解答与提示]三元数组的字典 + +def parseProblem(string): # 对以不小于两个回车切分的三段式字符串进行分词(通常第二段有"答案:", 第三段有"解答与提示:") + data = string.strip() + data = re.sub(r"\n{2,}","\n\n",data) + content,ans,solution = data.split("\n\n") + content = content.strip() + ans = re.sub("答案:","",ans).strip() + solution = re.sub("解答与提示:","",solution).strip() + return (content,ans,solution) # 返回三元组(题目内容, 答案, 解答与提示) + +def ModifyProblembyTeX(id_string,prodict,toeditfilepath,editor): # vscode打开.tex文件修改题目的内容, 答案与解答 + + # 读取题号列表并生成待编辑的.tex文件 + texdata = GenerateTexDataforEditing(id_string,prodict,"模板文件/题目编辑.txt",editor) + SaveTextFile(texdata,toeditfilepath) + + # 打开待编辑的.tex文件 + os.system("code "+toeditfilepath) + a = input("编辑完成后保存文件, 按回车继续...") + + # 生成修改后的题号与内容, 答案, 解答与提示的对应 + editor = GetDate() + "\t" + editor + editedtexdata = ReadTextFile(toeditfilepath) + edited_dict = GetEditedProblems(editedtexdata) + editedIDList = [] + + # 将.tex文件中的修改反映到原题库字典, 并保存 + for id in edited_dict: + content, ans, solution = edited_dict[id] + if not (content == prodict[id]["content"] and ans == prodict[id]["ans"] and solution == prodict[id]["solution"]): + prodict[id]["content"] = content + prodict[id]["ans"] = ans + prodict[id]["solution"] = solution + prodict[id]["edit"] = prodict[id]["edit"] + [editor] + editedIDList.append(id) + + save_dict(prodict,"../题库0.3/problems.json") + return generate_exp(editedIDList) # 返回编辑过的字典 + if __name__ == "__main__": print("数据库工具, import用.") \ No newline at end of file diff --git a/工具v2/vscode编辑题目及答案.py b/工具v2/vscode编辑题目及答案.py new file mode 100644 index 00000000..98d01804 --- /dev/null +++ b/工具v2/vscode编辑题目及答案.py @@ -0,0 +1,11 @@ +id_string = "1,5,10000" +editor = "王伟叶" + + + +from database_tools import * +pro_dict = load_dict("../题库0.3/problems.json") +tempfilepath = "临时文件/problem_edit.tex" + +edited = ModifyProblembyTeX(id_string,pro_dict,tempfilepath,editor) +print("编辑过的题号: %s"%edited) \ No newline at end of file