继续丰富database_tools
This commit is contained in:
parent
595b941397
commit
c0a34416ed
|
|
@ -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用.")
|
||||
Reference in New Issue