From c0a34416edb4137af92abd19df7545278926e0e2 Mon Sep 17 00:00:00 2001 From: "weiye.wang" Date: Fri, 23 Jun 2023 20:54:56 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=A7=E7=BB=AD=E4=B8=B0=E5=AF=8Cdatabase=5F?= =?UTF-8?q?tools?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 工具/database_tools.py | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/工具/database_tools.py b/工具/database_tools.py index 95bcdcf6..7ace4184 100644 --- a/工具/database_tools.py +++ b/工具/database_tools.py @@ -1,4 +1,8 @@ -import json,re,os,Levenshtein,fitz +import json,re,os,Levenshtein,fitz,time + +def GetDate(): #获得当前日期 + currentdate = str(time.localtime().tm_year)+str(time.localtime().tm_mon).zfill(2)+str(time.localtime().tm_mday).zfill(2) + return currentdate #返回当前日期yyyymmdd #读取存储json数据库相关(不限于题号数据库) @@ -203,5 +207,41 @@ def CreateIDLinks(old_id_list,new_id_list,*thedict): #建立已有id和新id之 return id_links # 返回id联系, 每个元组表示一对id, 前者是旧id, 后者是新id +def CreateRelatedProblems(links,thedict,filepath): # 根据links关联生成待编辑的新题目字典, 等待编辑修改 + try: + new_dict = {} + for item in links: + old_id,new_id = item + new_dict[old_id] = thedict[old_id].copy() + new_dict[old_id]["id"] = new_id + "待替换" + new_dict[old_id]["content"] = "(待编辑)" + new_dict[old_id]["content"] + new_dict[old_id]["usages"] = [] + new_dict[old_id]["same"] = [] + new_dict[old_id]["unrelated"] = [] + new_dict[old_id]["edit"] = new_dict[old_id]["edit"].copy() + [GetDate()+"\t"] + new_dict[old_id]["origin"] += "-" + GetDate() + "修改" + save_dict(new_dict,filepath) + except: + return 1 #异常返回1 + return 0 #正常返回0 + +def ImportRelatedProblems(new_json,main_json): # 导入编辑过的关联题目json文件到主数据库 + pro_dict = load_dict(main_json) + new_dict = load_dict(new_json) + for id in new_dict: + new_id = new_dict[id]["id"].replace("待替换","") #新题号后需要跟"待替换"字样 + if new_id in pro_dict: + print("题号有重复") + return 1 + else: + pro_dict[new_id] = new_dict[id].copy() + pro_dict[new_id]["id"] = new_id + pro_dict[id]["related"] += [new_id] + pro_dict[new_id]["related"] += [id] + print("导入关联题目 %s -> %s 信息成功."%(id,new_id)) + save_dict(pro_dict,main_json) #保存至目标pro_dict文件 + return 0 #正常返回0 + + if __name__ == "__main__": print("数据库工具, import用.") \ No newline at end of file