def data(ws): """ws:导入excel中的工作簿""" global count # 全局变量 for row in ws.iter_rows(): count = count + 1 now = datetime.now() intime = str(now - START)[:-7] print('导入{}题,已经用时{}'.format(count, intime), end='\r', flush=True) tag = row[1].value kind = row[2].value txts = row[3].value.split('\n') name = txts[0] items = txts[1:] answer = row[4].value source = row[5].value if not tag: # 如果为空 tag = '' if not kind: # 如果为空 kind = '' # txts不可能为空 # answer不可能为空 if not source: # 如果为空 source = '' memo = row[6].value if not memo: # 如果为空 memo = '' repo = row[7].value if not repo: # 如果为空 repo = '' other = row[8].value if not other: # 如果为空 other = '' with db_session: if Quiz.get(id=count): print('id为{}的题目已存在'.format(count), end='\r', flush=True) continue quiz = Quiz(tag=tag, kind=kind, name=name, answer=answer, source=source, memo=memo, repo=repo, other=other) for i in items: if i: # 如果不是空行 item = Item(name=i, quiz=quiz)
def practise(sh, ch_mode, max_index, locale=1): sh.menu.dicts = QUIZMODES sh.header.mode = WELECOMEMODES[ch_mode] # 第二层循环 # 临时存放答案 answer = '' flag = False # 对于ch_mode为 U 和 R 时,当flag等于True时,表示状态需要重置,更新题目 while True: with db_session: if ch_mode == 'Q': now_index = Config.get(key='now_index') if not now_index: now_index = Config(key='now_index', value=1) index = now_index.value if ch_mode == 'U': mark_index = Config.get(key='mark_index') if not mark_index: mark_index = Config(key='mark_index', value=1) index = mark_index.value if flag == True: quiz = Quiz.select(lambda q: q.mark == True)[:][0] index = quiz.id mark_index.value = quiz.id flag = False sh.header.sums = count(q for q in Quiz if q.mark == True) if ch_mode == 'R': rand_index = Config.get(key='rand_index') if not rand_index: rand_index = Config(key='rand_index', value=1) index = rand_index.value if flag == True: rand_index.value = randint(1, max_index) index = rand_index.value flag = False if ch_mode == 'W': ch_mode = 'Q' now_index = Config.get(key='now_index') now_index.value = locale index = locale sh.header.index = index # 显示题目 quiz = Quiz.get(id=index) star = '' if quiz.mark: star = '★' sh.main.title = '{} {}\n\n{}'.format( star, quiz.name, '\n'.join(sorted([x.name for x in quiz.items]))) sh.header.kind = quiz.kind sh.show() # 用于暂停 ch = getwch().upper() # 正确答案 quiz_answer = quiz.answer if quiz_answer == '√': quiz_answer = 'A' if quiz_answer == '×': quiz_answer = 'B' if ch in 'ABCDEFGH': if ch not in answer: answer = '{}{}'.format(answer, ch) answer_sort = ''.join(sorted(answer)) if answer_sort == quiz_answer: sh.state.title = '{}答案[{}]正确{}'.format( Fore.GREEN, answer_sort, Fore.RESET) Done(quiz=quiz, answer=answer_sort, state=True, created=datetime.now()) # 记录做题 continue sh.state.title = '{}答案[{}]错误{}'.format( Fore.RED, answer_sort, Fore.RESET) Done(quiz=quiz, answer=answer_sort, created=datetime.now()) # 记录做题 continue if ch == 'Q': sh.state.title = '正确答案[{}]'.format(quiz_answer) if ch == 'S': answer_trans = lambda x: Fore.GREEN + '正确' + Fore.RESET if x else Fore.RED + '错误' + Fore.RESET result = [ '{}\t|\t{}\t|\t{}'.format( x.created.strftime('%Y-%m-%d %H:%M:%S'), x.answer, answer_trans(x.state)) for x in quiz.dones ] result_s = [x.state for x in quiz.dones] sh.quote.title = '[{}]\n\n{}\n\n<{}>\n\n[该题总计练习{}次,正确{}次,错误{}次]\n\n{}'.format( quiz.source, quiz.memo, Fore.YELLOW + quiz.other + Fore.RESET, len(result_s), result_s.count(True), result_s.count(False), '\n'.join(sorted([x for x in result]))) if ch == 'M': if quiz.mark: quiz.mark = False else: quiz.mark = True if ch == 'R': sh.state.title = '' sh.quote.title = '' answer = '' if ch in ' PL\r': ch = ch_mode sh.state.title = '' sh.quote.title = '' answer = '' if ch_mode == 'R': rand_index.value = randint(1, max_index) flag = True #重置状态 break if ch_mode == 'U': u_list = Quiz.select(lambda q: q.mark == True)[:] if len(u_list) >= 1: i = randint(0, len(u_list) - 1) mark_index.value = u_list[i].id flag = True # 重置状态 break # Q顺序模式 now_index.value = now_index.value + 1 if now_index.value > max_index: now_index.value = max_index break if ch in 'ZK': ch = ch_mode sh.state.title = '' sh.quote.title = '' answer = '' if ch_mode == 'R': rand_index.value = randint(1, max_index) flag = True # 重置状态 break if ch_mode == 'U': u_list = Quiz.select(lambda q: q.mark == True)[:] i = randint(0, len(u_list) - 1) mark_index.value = u_list[i].id flag = True # 重置状态 break # Q now_index.value = now_index.value - 1 if now_index.value < 1: now_index.value = 1 break if ch == 'T': sh.state.title = '' sh.quote.title = '' sh.header.sums = '' answer = '' break return ch