import pathlib import toml import time import copy import datetime import json class Aux(): @staticmethod def get_daystamp(): return (time.time() // (24*3600)) class Atom(): """原子: 由电子(分析数据)和核子(材料元数据)组成的反应(运行时)中间对象""" class Electron(): """电子: 记忆分析数据及算法""" algorithm = "SM-2" """ origin = "陈情表" # 来源 content = "" # 内容 efactor = 2.5 # 易度系数, 越大越简单, 最大为5 real_rept = 0 # (实际)重复次数 rept = 0 # (有效)重复次数 interval = 0 # 最佳间隔 last_date = 0 # 上一次复习的时间戳 next_date = 0 # 将要复习的时间戳 is_activated = 0 # 激活状态 # *NOTE: 这里的"时间戳" 是以天为单位的整数, 即 UNIX 时间戳除以一天的秒数取整 last_modify = 0 # 最后修改时间戳(此处是UNIX时间戳) """ def __init__(self, content: str, data: dict): self.content = content self.efactor = data.get('efactor', 2.5) self.real_rept = data.get('real_rept', 0) self.rept = data.get('rept', 0) self.interval = data.get('interval', 0) self.last_date = data.get('last_date', 0) self.next_date = data.get('next_date', 0) self.is_activated = data.get('is_activated', 0) self.last_modify = time.time() def activate(self): self.is_activated = 1 def modify(self, var: str, value): setattr(self, var, value) self.last_modify = time.time() def update(self, quality): """ 根据 quality(0 ~ 5) 进行参数迭代 quality 由主程序评估 """ if quality == -1: return -1 self.efactor = self.efactor + (0.1 - (5 - quality) * (0.08 + (5 - quality) * 0.02)) self.efactor = max(1.3, self.efactor) if quality < 3: # 如果回忆质量低于 3,重置重复次数 self.rept = 0 self.interval = 0 # 设为0,以便下面重新计算I(1) else: self.rept += 1 self.real_rept += 1 if self.rept == 0: # 刚被重置或首次遇到 self.interval = 1 # I(1) elif self.rept == 1: self.interval = 6 # I(2) 经验公式 else: self.interval = round(self.interval * self.efactor) self.last_date = Aux.get_daystamp() self.next_date = self.last_date + self.interval def __str__(self): return (f"记忆单元预览 \n" f"内容: '{self.content}' \n" f"易度系数: {self.efactor:.2f} \n" f"已经重复的次数: {self.rept} \n" f"下次间隔: {self.interval} 天 \n" f"下次复习日期时间戳: {self.next_date}") def __eq__(self, other): if self.content == other.content: return 1 return 0 def __hash__(self): return hash(self.content) @staticmethod def import_from_file(path: pathlib.Path): name = path.name.replace(path.suffix, "") with open(path, 'r') as f: all = toml.load(f) lst = list() for i in all.keys(): lst.append(Electron(i, all[i])) return (name, lst) @staticmethod def save_to_file(electron_list, path: pathlib.Path): with open(path, 'w') as f: toml.dump(electron_list, f) class Nucleon(): """核子: 材料元数据""" def __init__(self, content: str, data: dict): self.metadata = data self.content = content @staticmethod def import_from_file(path: pathlib.Path): name = path.name.replace(path.suffix, "") with open(path, 'r') as f: all = toml.load(f) lst = list() for i in all.keys(): lst.append(Nucleon(i, all[i])) return (name, lst) @staticmethod def save_to_file(nucleon_list, path: pathlib.Path): with open(path, 'w') as f: toml.dump(nucleon_list, f) class AtomicFile(): def __init__(self, path, type_="unknown"): self.path = path self.type_ = type_ if type_ == "nucleon": self.name, self.datalist = Nucleon.import_from_file(pathlib.Path(path)) if type_ == "electron": self.name, self.datalist = Electron.import_from_file(pathlib.Path(path)) def save(self): if self.type_ == "nucleon": Nucleon.save_to_file(self.datalist, self.path) if self.type_ == "electron": Electron.save_to_file(self.datalist, self.path) def get_full_content(self): if self.type_ == "nucleon": text = "" for i in self.datalist: text += i.content return text return "" def get_len(self): return len(self.datalist)