diff --git a/工具/database_tools.py b/工具/database_tools.py index 2220932f..95bcdcf6 100644 --- a/工具/database_tools.py +++ b/工具/database_tools.py @@ -53,7 +53,8 @@ def generate_problem_series(startingid,length,adict): #在adict字典里返回 return ",".join(excludelist) #返回按顺序的题号列表 -def generate_number_set(string): #根据可能含有":"和","的题号字符串生成一个用逗号分隔的六位题号列表, 例如"1:3,5"会生成["000001","000002","000003","000005"] +def generate_number_set(string,*thedict): #根据可能含有":"和","的题号字符串生成一个用逗号分隔的六位题号列表, 例如"1:3,5"会生成["000001","000002","000003","000005"] +#可变参数*dict如果存在, 将只生成dict的keys中包含的题号列表 string = re.sub(r"[\n\s]","",string) string_list = string.split(",") numbers_list = [] @@ -64,7 +65,13 @@ def generate_number_set(string): #根据可能含有":"和","的题号字符串 start,end = s.split(":") for ind in range(int(start),int(end)+1): numbers_list.append(str(ind).zfill(6)) - return numbers_list #返回六位题号列表 + if len(thedict) == 0: + return numbers_list #返回六位题号列表 + elif len(thedict) == 1 and type(thedict[0]) == dict: + numbers_list = [id for id in numbers_list if id in thedict[0]] + return numbers_list #返回字典中存在的六位题号列表 + else: + return "输入参数有误" def generate_exp(id_list): #根据题号列表生成字符串式的含":"和","的题号字符串, 例如["000001","000002","000003","000005"]生成"000001:000003,000005", 若列表为空则生成"无有效题号" if not len(id_list) == 0: @@ -184,5 +191,17 @@ def CreateNewProblem(id,content,origin,dict,editor): # 构建一道新题目的 NewProblem["edit"] = [editor] return NewProblem # 返回一道新题目的字典, 已赋新的ID, 内容, 来源和编辑者 +def CreateIDLinks(old_id_list,new_id_list,*thedict): #建立已有id和新id之间的联系, thedict为可选, 选中的话即为当前字典, 会从new_id_list中排除当前字典中有的项 + if len(thedict) == 1 and type(thedict[0]) == dict: + new_id_list = [id for id in new_id_list if not id in thedict[0]] + if len(old_id_list)>len(new_id_list): + return "新ID个数不足." + else: + id_links = [] + for i in range(len(old_id_list)): + id_links.append((old_id_list[i],new_id_list[i])) + return id_links # 返回id联系, 每个元组表示一对id, 前者是旧id, 后者是新id + + if __name__ == "__main__": print("数据库工具, import用.") \ No newline at end of file