修改metadata施工中, 尚缺tags,usages,remarks
This commit is contained in:
parent
03f0d20163
commit
af1c27b4b4
|
|
@ -652,7 +652,7 @@ def SeperateFirstLine(string): # 切割一个含有换行的字符串
|
|||
contents = "\n".join([lines.strip() for lines in thelist[1:]])
|
||||
return (firstline,contents) # 返回第一行, 其余行形成的二元组, 每一行前后的空格都被删去
|
||||
|
||||
def ObtainDatatoModify(metadatafilepath,fields_dict): #从metadata文件中获取需要修改的综合信息, metadata文件用双回车区分项, 单行的项表示此后的字段, 不小于两行的项中第一行是题目id, 第二行起是要修改的内容.
|
||||
def ObtainDatatoModify(metadatafilepath,fields_dict,idlist): #从metadata文件中获取需要修改的综合信息, metadata文件用双回车区分项, 单行的项表示此后的字段, 不小于两行的项中第一行是题目id, 第二行起是要修改的内容.
|
||||
#fieldsdictpath是字段信息数据库文件路径
|
||||
data = ReadTextFile(metadatafilepath)
|
||||
datalines = data.split("\n")
|
||||
|
|
@ -673,7 +673,10 @@ def ObtainDatatoModify(metadatafilepath,fields_dict): #从metadata文件中获
|
|||
currentfield = "NotAField"
|
||||
else:
|
||||
id,content = SeperateFirstLine(line)
|
||||
to_modify_list.append((currentfield,str(id).zfill(6),content))
|
||||
if str(id).zfill(6) in idlist:
|
||||
to_modify_list.append((currentfield,str(id).zfill(6),content))
|
||||
else:
|
||||
print(f"其中 ID {id} 有误, 已忽略!!")
|
||||
return to_modify_list #返回一个列表, 每一项是一个三元组, 三项依次为字段, 题号, 要修改的内容
|
||||
|
||||
def FloatToInt(string): #从字符串返回浮点数,如果值非常接近整数则返回该整数
|
||||
|
|
@ -832,6 +835,13 @@ def AppendUsageData2024(prodict,field_id_and_content):
|
|||
return (field,id,content,output) #返回四元组: 题号, 字段, 内容, 待确定是否要添加的字符串(不含FORCE字样的行为旧结果,含FORCE字样的行为新结果,FORCE是运行后强制添加)
|
||||
|
||||
|
||||
def generateUsageList(db,id):
|
||||
mycursor = db.cursor()
|
||||
mycursor.execute("SELECT date,classname,diff FROM usages WHERE ID = %s;",(id,))
|
||||
tuplelist = mycursor.fetchall()
|
||||
return tuplelist
|
||||
|
||||
|
||||
def ImportMetadata(db,metadatafilepath): #metadata自动修改, 根据字段自适应修改, 参数为题库字典, 目标字典, 字段字典, metadata文本文件路径, 待确定是否替换的内容的存放路径
|
||||
fieldsdict = {"objs":"objappend",
|
||||
"tags":"tagappend",
|
||||
|
|
@ -844,7 +854,12 @@ def ImportMetadata(db,metadatafilepath): #metadata自动修改, 根据字段自
|
|||
"space":"overwrite",
|
||||
"remark":"remarkappend"
|
||||
}
|
||||
data_to_modify = ObtainDatatoModify(metadatafilepath,fieldsdict)
|
||||
mycursor = db.cursor()
|
||||
mycursor.execute("SELECT ID FROM problems;")
|
||||
id_list_in_DB = [ret[0] for ret in mycursor.fetchall()]
|
||||
mycursor.execute("SELECT objid FROM lessonobj;")
|
||||
obj_list_in_DB = [ret[0] for ret in mycursor.fetchall()]
|
||||
data_to_modify = ObtainDatatoModify(metadatafilepath,fieldsdict,id_list_in_DB)
|
||||
print(data_to_modify)
|
||||
for item in data_to_modify:
|
||||
field = item[0]
|
||||
|
|
@ -855,20 +870,68 @@ def ImportMetadata(db,metadatafilepath): #metadata自动修改, 根据字段自
|
|||
return 1
|
||||
else:
|
||||
method = fieldsdict[field]
|
||||
print(method)
|
||||
if method == "overwrite":
|
||||
overwriteinDB(db,field,item[1],item[2])
|
||||
elif method == "mutualappend":
|
||||
mutualappendinDB(db,field,item[1],item[2],id_list_in_DB)
|
||||
elif method == "objappend":
|
||||
objappendinDB(db,item[1],item[2],obj_list_in_DB.append("KNONE"))
|
||||
|
||||
|
||||
return 0 # 已在数据库中修改, 之后需要将数据库写入一次, 返回1表示字段名有误, 返回其他表示成功进行了修改
|
||||
|
||||
def overwriteinDB(mydb,field,id,content_string):
|
||||
def overwriteinDB(mydb,field,id,content_string): #覆盖ans,solution,space字段的内容并在logs中记录
|
||||
mycursor = mydb.cursor()
|
||||
sql = f"SELECT {field} FROM problems WHERE ID = %s;"
|
||||
val = (id,)
|
||||
mycursor.execute(sql,val)
|
||||
original_string = mycursor.fetchall()[0][0]
|
||||
sql = f"UPDATE problems SET {field} = %s WHERE ID = %s;"
|
||||
val = (content_string,id)
|
||||
mycursor.execute(sql,val)
|
||||
sql = "INSERT INTO logs (DATE,TIME,username,ID,action,db_content) VALUE (%s,%s,%s,%s,%s,%s);"
|
||||
val = (GetDate(),GetTime(),get_git_username(),id,f"更改 {field} 数据",content_string)
|
||||
val = (GetDate(),GetTime(),get_git_username(),id,f"更改 {field} 数据",f"{original_string} -> {content_string}")
|
||||
mycursor.execute(sql,val)
|
||||
|
||||
def mutualappendinDB(mydb,field,id,content_string,idlist): #新增same,related,unrelated数据
|
||||
mycursor = mydb.cursor()
|
||||
content_list = [item.strip().zfill(6) for item in content_string.split("\n") if not item.strip() == ""]
|
||||
for id2 in content_list:
|
||||
smallid = min(id,id2)
|
||||
bigid = max(id,id2)
|
||||
if smallid in idlist and bigid in idlist:
|
||||
sql = f"SELECT ID, {field.upper()}_ID from {field} WHERE ID = %s AND {field.upper()}_ID = %s;"
|
||||
val = (smallid,bigid)
|
||||
mycursor.execute(sql,val)
|
||||
ret_list = mycursor.fetchall()
|
||||
if len(ret_list) == 0:
|
||||
sql = f"INSERT INTO {field} SET ID = %s, {field.upper()}_ID = %s;"
|
||||
mycursor.execute(sql,val)
|
||||
sql = "INSERT INTO logs (DATE,TIME,username,ID,action,db_content) VALUE (%s,%s,%s,%s,%s,%s);"
|
||||
val = (GetDate(),GetTime(),get_git_username(),id,f"新增 {field} 配对",f"{smallid} <-> {bigid}")
|
||||
mycursor.execute(sql,val)
|
||||
else:
|
||||
print(f"{smallid} 或 {bigid} 中有错误, 请检查")
|
||||
|
||||
def objappendinDB(mydb,id,content_string,objlist): #新增obj对应
|
||||
mycursor = mydb.cursor()
|
||||
content_list = [item.strip() for item in content_string.split("\n") if not item.strip() == ""]
|
||||
for objid in content_list:
|
||||
objid = objid.upper()
|
||||
if not objid in objlist:
|
||||
print(f"{objid} 有误, 请检查!!!")
|
||||
else:
|
||||
sql = f"SELECT ID, obj_ID FROM objcorresp WHERE ID = %s AND obj_ID = %s;"
|
||||
val = (id,objid)
|
||||
mycursor.execute(sql,val)
|
||||
ret_list = mycursor.fetchall()
|
||||
if len(ret_list) == 0:
|
||||
sql = f"INSERT INTO objcorresp SET ID = %s, obj_ID = %s;"
|
||||
mycursor.execute(sql,val)
|
||||
sql = "INSERT INTO logs (DATE,TIME,username,ID,action,db_content) VALUE (%s,%s,%s,%s,%s,%s);"
|
||||
val = (GetDate(),GetTime(),get_git_username(),id,f"新增目标对应",f"{id}: {objid}")
|
||||
mycursor.execute(sql,val)
|
||||
|
||||
def parseUsage(usagestring): #对单行usage信息进行分词
|
||||
usagedict = {}
|
||||
datalist = re.sub(r"\s+",r"\t",usagestring.strip()).split("\t")
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ mydb = connect(hostname = "wwylss.synology.me", port = "13306", username="tikuus
|
|||
|
||||
ImportMetadata(mydb,metadatafilepath)
|
||||
|
||||
mydb.commit()
|
||||
mydb.close()
|
||||
# save_dict(prodict,"../题库0.3/Problems.json")
|
||||
|
||||
|
|
|
|||
Reference in New Issue