You've already forked HeurAMS-Legacy
基本对象系统移植
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
from electron import Electron
|
||||
from nucleon import Nucleon
|
||||
import pathlib
|
||||
import toml
|
||||
from typing import List
|
||||
import heurams.services.timer as timer
|
||||
|
||||
class NucleonUnion():
|
||||
"""
|
||||
替代原有 NucleonFile 类, 支持复杂逻辑
|
||||
|
||||
Attributes:
|
||||
path (Path): 对应于 NucleonUnion 实例的文件路径。
|
||||
name (str): 核联对象的显示名称,从文件名中派生。
|
||||
nucleons (list): 内部核子对象的列表。
|
||||
nucleons_dict (dict): 内部核子对象的字典,以核子内容作为键。
|
||||
keydata (dict): 核子对象字典键名的翻译。
|
||||
testdata (dict): 记忆测试项目的元数据。
|
||||
|
||||
Parameters:
|
||||
path (Path): 包含核子数据的文件路径。
|
||||
"""
|
||||
|
||||
def __init__(self, path: pathlib.Path):
|
||||
self.path = path
|
||||
self.name = path.name.replace(path.suffix, "")
|
||||
with open(path, 'r') as f:
|
||||
all = toml.load(f)
|
||||
lst = list()
|
||||
for i in all.keys():
|
||||
if "attr" in i:
|
||||
continue
|
||||
if "data" in i:
|
||||
continue
|
||||
lst.append(Nucleon(i, all[i]))
|
||||
self.keydata = all["keydata"]
|
||||
self.testdata = all["testdata"]
|
||||
self.nucleons: List[Nucleon] = lst
|
||||
self.nucleons_dict = {i.content: i for i in lst}
|
||||
|
||||
def __len__(self):
|
||||
return len(self.nucleons)
|
||||
|
||||
def linked_electron_union(self):
|
||||
if (self.path.parent / '..' / 'electron' / self.path.name).exists():
|
||||
return ElectronUnion(self.path.parent / '..' / 'electron' / self.path.name)
|
||||
else:
|
||||
return 0
|
||||
|
||||
def save(self):
|
||||
with open(self.path, 'w') as f:
|
||||
tmp = {i.content: i.metadata for i in self.nucleons}
|
||||
toml.dump(tmp, f)
|
||||
|
||||
|
||||
class ElectronUnion:
|
||||
"""取代原有 ElectronFile 类, 以支持复杂逻辑"""
|
||||
def __init__(self, path):
|
||||
self.path = path
|
||||
print(path)
|
||||
self.name = path.name.replace(path.suffix, "")
|
||||
with open(path, 'r') as f:
|
||||
all = toml.load(f)
|
||||
lst = list()
|
||||
for i in all.keys():
|
||||
if i != "total":
|
||||
lst.append(Electron(i, all[i]))
|
||||
self.total = all.get("total", {"last_date": 0})
|
||||
self.electrons = lst
|
||||
self.electrons_dict = {i.content: i for i in lst}
|
||||
|
||||
def sync(self):
|
||||
"""同步 electrons_dict 中新增对到 electrons 中, 仅用于缺省初始化不存在映射时调用"""
|
||||
self.electrons = self.electrons_dict.values()
|
||||
|
||||
def __len__(self):
|
||||
return len(self.electrons)
|
||||
|
||||
def linked_nucleon_union(self):
|
||||
return NucleonUnion(self.path.parent / '..' / 'nucleon' / self.path.name)
|
||||
|
||||
def save(self):
|
||||
# print(1)
|
||||
self.total["last_date"] = timer.get_daystamp()
|
||||
with open(self.path, 'w') as f:
|
||||
tmp = {i.content: i.metadata for i in self.electrons}
|
||||
tmp["total"] = self.total
|
||||
# print(tmp)
|
||||
toml.dump(tmp, f)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user