113 lines
3.9 KiB
Python
113 lines
3.9 KiB
Python
import typing
|
|
import particles as pt
|
|
import pathlib
|
|
import auxiliary as aux
|
|
class Parser():
|
|
"""轻量级版本文件解析器, 用于文件管理器的记忆状态解析"""
|
|
|
|
class Reactor():
|
|
"""反应堆对象, 用于全面解析文件, 并处理和分配一次文件记忆流程的资源与策略"""
|
|
def __init__(self, nucleon_file: pt.AtomicFile, electron_file: pt.AtomicFile, tasked_num):
|
|
# 导入原子对象
|
|
self.reported = set()
|
|
self.nucleon_file = nucleon_file
|
|
self.electron_file = electron_file
|
|
self.tasked_num = tasked_num
|
|
self.atoms_new = list()
|
|
self.atoms_review = list()
|
|
counter = self.tasked_num
|
|
|
|
self.electron_dict = {elect.content: elect for elect in electron_file.datalist}
|
|
|
|
def electron_dict_get_fallback(key) -> pt.Electron:
|
|
value = self.electron_dict.get(key)
|
|
|
|
# 如果值不存在,则设置默认值
|
|
if value is None:
|
|
value = pt.Electron(key, {}) # 获取默认值
|
|
self.electron_dict[key] = value # 将默认值存入字典
|
|
value = self.electron_dict[key]
|
|
|
|
return value # 返回获取的值(可能是默认值)
|
|
|
|
for nucleon in nucleon_file.datalist:
|
|
atom = (electron_dict_get_fallback(nucleon.content), nucleon)
|
|
if atom[0].is_activated == 0:
|
|
if counter > 0:
|
|
atom[0].is_activated = 1
|
|
self.atoms_new.append(atom)
|
|
counter -= 1
|
|
else:
|
|
if atom[0].next_date <= aux.get_daystamp():
|
|
atom[0].last_date = aux.get_daystamp()
|
|
self.atoms_review.append(atom)
|
|
# 设置运行时
|
|
self.index: int
|
|
self.procession: list
|
|
self.failed: list
|
|
self.round_title: str
|
|
self.reported: set
|
|
self.current_atom: typing.Tuple[pt.Electron, pt.Nucleon]
|
|
self.round_set = 0
|
|
self.current_atom = pt.Atom.placeholder()
|
|
|
|
def set_round(self, title, procession):
|
|
self.round_set = 1
|
|
self.round_title = title
|
|
self.procession = procession
|
|
self.failed = list()
|
|
self.index = -1
|
|
|
|
def set_round_templated(self, stage):
|
|
titles = {
|
|
1: "复习模式",
|
|
2: "新记忆模式",
|
|
3: "总复习模式"
|
|
}
|
|
processions = {
|
|
1: self.atoms_review,
|
|
2: self.atoms_new,
|
|
3: list(set(self.atoms_new + self.atoms_review))
|
|
}
|
|
ret = 1
|
|
if stage == 1 and len(processions[1]) == 0:
|
|
stage = 2
|
|
ret = 2
|
|
self.set_round(title=titles[stage], procession=processions[stage])
|
|
return ret
|
|
|
|
def forward(self, step = 1):
|
|
if self.index + step >= len(self.procession):
|
|
if len(self.failed) > 0:
|
|
self.procession = self.failed
|
|
self.index = -1
|
|
self.forward(step)
|
|
if "- 额外复习" not in self.round_title:
|
|
self.round_title += " - 额外复习"
|
|
self.failed = list()
|
|
return 1 # 自动重定向到 failed
|
|
else:
|
|
self.round_set = 0
|
|
return -1 # 此轮已完成
|
|
self.index += step
|
|
self.current_atom = self.procession[self.index]
|
|
return 0
|
|
|
|
def save(self):
|
|
print("Progress saved")
|
|
# self.nucleon_file.save()
|
|
temp = list()
|
|
for i in self.electron_dict.keys():
|
|
temp.append(self.electron_dict[i])
|
|
self.electron_file.datalist = temp
|
|
self.electron_file.save()
|
|
|
|
def report(self, atom, quality):
|
|
if atom[0] not in self.reported:
|
|
atom[0].revisor(quality)
|
|
self.reported.add(atom[0])
|
|
if quality <= 3:
|
|
self.failed.append(atom)
|
|
return 1
|
|
else:
|
|
return 0 |