This commit is contained in:
2025-10-19 00:19:13 +08:00
parent ace6197231
commit 3fef587ff5
3 changed files with 21 additions and 11 deletions

View File

@@ -52,4 +52,4 @@ class Atom():
@staticmethod @staticmethod
def placeholder(): def placeholder():
return (Electron.placeholder(), Nucleon.placeholder(), {}) return (Electron.placeholder(), Nucleon.placeholder(), {})

View File

@@ -5,7 +5,7 @@ import pathlib
import toml import toml
import json import json
def loader_nucleon(path: pathlib.Path, fmt = "toml"): def load_nucleon(path: pathlib.Path, fmt = "toml"):
with open(path, "r") as f: with open(path, "r") as f:
dictdata = dict() dictdata = dict()
toml.load(f, dictdata) # type: ignore toml.load(f, dictdata) # type: ignore
@@ -13,4 +13,14 @@ def loader_nucleon(path: pathlib.Path, fmt = "toml"):
for item, attr in dictdata.items(): for item, attr in dictdata.items():
if item == "__metadata__": if item == "__metadata__":
continue continue
lst.append(Nucleon(hasher.hash(item), attr)) lst.append((Nucleon(hasher.hash(item), attr), dictdata["__metadata__"]["orbital"]))
return lst
def load_electron(path: pathlib.Path, fmt = "json"):
with open(path, "r") as f:
dictdata = dict()
json.load(f, dictdata) # type: ignore
lst = list()
for item, attr in dictdata.items():
lst.append(Electron(hasher.hash(item), attr))
return lst

View File

@@ -1,29 +1,29 @@
class Nucleon: class Nucleon:
"""原子核: 材料元数据""" """原子核: 材料元数据"""
def __init__(self, ident: str, metadata: dict): def __init__(self, ident: str, payload: dict):
"""初始化原子核 (记忆内容) """初始化原子核 (记忆内容)
Args: Args:
ident: 唯一标识符 ident: 唯一标识符
metadata: 记忆内容信息 payload: 记忆内容信息
""" """
self.metadata = metadata self.payload = payload
self.ident = ident self.ident = ident
def __getitem__(self, key): def __getitem__(self, key):
if key == "ident": if key == "ident":
return self.ident return self.ident
if key in self.metadata: if key in self.payload:
return self.metadata[key] return self.payload[key]
else: else:
raise KeyError(f"Key '{key}' not found in metadata.") raise KeyError(f"Key '{key}' not found in payload.")
def __iter__(self): def __iter__(self):
yield from self.metadata.keys() yield from self.payload.keys()
def __len__(self): def __len__(self):
return len(self.metadata) return len(self.payload)
def __hash__(self): def __hash__(self):
return hash(self.ident) return hash(self.ident)