You've already forked HeurAMS-legacy
style: 代码格式化
This commit is contained in:
@@ -9,6 +9,7 @@ import json
|
||||
import bidict
|
||||
from heurams.context import config_var
|
||||
|
||||
|
||||
class AtomRegister(TypedDict):
|
||||
nucleon: Nucleon
|
||||
nucleon_path: pathlib.Path
|
||||
@@ -21,7 +22,8 @@ class AtomRegister(TypedDict):
|
||||
orbital_fmt: str
|
||||
runtime: dict
|
||||
|
||||
class Atom():
|
||||
|
||||
class Atom:
|
||||
"""
|
||||
统一处理一系列对象的所有信息与持久化:
|
||||
关联电子 (算法数据)
|
||||
@@ -30,11 +32,11 @@ class Atom():
|
||||
以及关联路径
|
||||
"""
|
||||
|
||||
def __init__(self, ident = ""):
|
||||
def __init__(self, ident=""):
|
||||
self.ident = ident
|
||||
atom_registry[ident] = self
|
||||
# self.is_evaled = False
|
||||
self.registry: AtomRegister = { # type: ignore
|
||||
self.registry: AtomRegister = { # type: ignore
|
||||
"nucleon": None,
|
||||
"nucleon_path": None,
|
||||
"nucleon_fmt": "toml",
|
||||
@@ -42,7 +44,7 @@ class Atom():
|
||||
"electron_path": None,
|
||||
"electron_fmt": "json",
|
||||
"orbital": None,
|
||||
"orbital_path": None, # 允许设置为 None, 此时使用 nucleon 文件内的推荐配置
|
||||
"orbital_path": None, # 允许设置为 None, 此时使用 nucleon 文件内的推荐配置
|
||||
"orbital_fmt": "toml",
|
||||
}
|
||||
self.do_eval()
|
||||
@@ -53,16 +55,17 @@ class Atom():
|
||||
self.do_eval()
|
||||
else:
|
||||
raise ValueError("不受支持的原子元数据链接操作")
|
||||
|
||||
|
||||
def do_eval(self):
|
||||
"""
|
||||
执行并以结果替换当前单元的所有 eval 语句
|
||||
TODO: 带有限制的 eval, 异步/多线程执行避免堵塞
|
||||
"""
|
||||
|
||||
# eval 环境设置
|
||||
def eval_with_env(s: str):
|
||||
try:
|
||||
nucleon = self.registry['nucleon']
|
||||
nucleon = self.registry["nucleon"]
|
||||
default = config_var.get()["puzzles"]
|
||||
metadata = nucleon.metadata
|
||||
except:
|
||||
@@ -72,7 +75,7 @@ class Atom():
|
||||
except Exception as e:
|
||||
ret = f"此 eval 实例发生错误: {e}"
|
||||
return ret
|
||||
|
||||
|
||||
def traverse(data, modifier):
|
||||
if isinstance(data, dict):
|
||||
for key, value in data.items():
|
||||
@@ -89,10 +92,9 @@ class Atom():
|
||||
if data.startswith("eval:"):
|
||||
return modifier(data[5:])
|
||||
return data
|
||||
|
||||
|
||||
traverse(self.registry["nucleon"], eval_with_env)
|
||||
traverse(self.registry["orbital"], eval_with_env)
|
||||
|
||||
|
||||
def persist(self, key):
|
||||
path: pathlib.Path | None = self.registry[key + "_path"]
|
||||
@@ -109,7 +111,7 @@ class Atom():
|
||||
raise KeyError("不受支持的持久化格式")
|
||||
else:
|
||||
raise TypeError("对未初始化的路径对象操作")
|
||||
|
||||
|
||||
def __getitem__(self, key):
|
||||
if key in self.registry:
|
||||
return self.registry[key]
|
||||
@@ -124,5 +126,6 @@ class Atom():
|
||||
@staticmethod
|
||||
def placeholder():
|
||||
return (Electron.placeholder(), Nucleon.placeholder(), {})
|
||||
|
||||
|
||||
|
||||
atom_registry: bidict.bidict[str, Atom] = bidict.bidict()
|
||||
|
||||
Reference in New Issue
Block a user