feat: 一些改进
This commit is contained in:
@@ -11,7 +11,7 @@ class Lict(UserList): #TODO: 优化同步(惰性同步), 当前性能为 O(n)
|
||||
- 键名一定唯一, 且仅能为字符串
|
||||
- 值一定是引用对象
|
||||
- 不使用并发
|
||||
- 不在乎列表顺序语义和列表索引查找, 因此 sort, index 等功能不可用
|
||||
- 不在乎列表顺序语义(严格按键名字符序排列)和列表索引查找, 因此外部的 sort, index 等功能不可用
|
||||
- append 的元组中, 表示键名的元素不能重复, 否则会导致覆盖行为
|
||||
"""
|
||||
def __init__(self, initlist = None, initdict = None):
|
||||
@@ -20,9 +20,11 @@ class Lict(UserList): #TODO: 优化同步(惰性同步), 当前性能为 O(n)
|
||||
initlist = list(initdict.items())
|
||||
super().__init__(initlist=initlist)
|
||||
self._sync_based_on_list()
|
||||
self.data.sort()
|
||||
|
||||
def _sync_based_on_dict(self):
|
||||
self.data = list(self.dicted_data.items())
|
||||
self.data.sort()
|
||||
|
||||
def _sync_based_on_list(self):
|
||||
self.dicted_data = {}
|
||||
@@ -53,6 +55,9 @@ class Lict(UserList): #TODO: 优化同步(惰性同步), 当前性能为 O(n)
|
||||
super().__delitem__(i)
|
||||
self._sync_based_on_list()
|
||||
|
||||
def __contains__(self, item):
|
||||
return (item in self.data or item in self.keys() or item in self.values())
|
||||
|
||||
def append(self, item: Any) -> None:
|
||||
if item != (item[0], item[1]):
|
||||
raise NotImplementedError
|
||||
@@ -60,14 +65,15 @@ class Lict(UserList): #TODO: 优化同步(惰性同步), 当前性能为 O(n)
|
||||
self._sync_based_on_list()
|
||||
|
||||
def insert(self, i: int, item: Any) -> None:
|
||||
if item != (item[0], item[1]):
|
||||
if item != (item[0], item[1]): # 确保 item 是遵从限制的元组
|
||||
raise NotImplementedError
|
||||
super().insert(i, item)
|
||||
self._sync_based_on_list()
|
||||
|
||||
def pop(self, i: int = -1) -> Any:
|
||||
super().pop(i)
|
||||
res = super().pop(i)
|
||||
self._sync_based_on_list()
|
||||
return res
|
||||
|
||||
def remove(self, item: Any) -> None:
|
||||
if isinstance(item, str):
|
||||
@@ -93,3 +99,18 @@ class Lict(UserList): #TODO: 优化同步(惰性同步), 当前性能为 O(n)
|
||||
def reverse(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def keys(self):
|
||||
return self.dicted_data.keys()
|
||||
|
||||
def values(self):
|
||||
return self.dicted_data.values()
|
||||
|
||||
def items(self):
|
||||
return self.data
|
||||
|
||||
def keys_equal_with(self, other):
|
||||
return self.key_equality(self, other)
|
||||
|
||||
@classmethod
|
||||
def key_equality(cls, a, b):
|
||||
return a.keys() == b.keys()
|
||||
3
src/heurams/kernel/repomgr/navi.py
Normal file
3
src/heurams/kernel/repomgr/navi.py
Normal file
@@ -0,0 +1,3 @@
|
||||
class Navi():
|
||||
def __init__(self, init) -> None:
|
||||
pass
|
||||
241
src/heurams/kernel/repomgr/refvar.py
Normal file
241
src/heurams/kernel/repomgr/refvar.py
Normal file
@@ -0,0 +1,241 @@
|
||||
class RefVar:
|
||||
def __init__(self, initvalue) -> None:
|
||||
self.data = initvalue
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"RefVar({repr(self.data)})"
|
||||
|
||||
def __str__(self) -> str:
|
||||
return str(self.data)
|
||||
|
||||
def __add__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data + other.data)
|
||||
return RefVar(self.data + other)
|
||||
|
||||
def __radd__(self, other):
|
||||
return RefVar(other + self.data)
|
||||
|
||||
def __sub__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data - other.data)
|
||||
return RefVar(self.data - other)
|
||||
|
||||
def __rsub__(self, other):
|
||||
return RefVar(other - self.data)
|
||||
|
||||
def __mul__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data * other.data)
|
||||
return RefVar(self.data * other)
|
||||
|
||||
def __rmul__(self, other):
|
||||
return RefVar(other * self.data)
|
||||
|
||||
def __truediv__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data / other.data)
|
||||
return RefVar(self.data / other)
|
||||
|
||||
def __rtruediv__(self, other):
|
||||
return RefVar(other / self.data)
|
||||
|
||||
def __floordiv__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data // other.data)
|
||||
return RefVar(self.data // other)
|
||||
|
||||
def __rfloordiv__(self, other):
|
||||
return RefVar(other // self.data)
|
||||
|
||||
def __mod__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data % other.data)
|
||||
return RefVar(self.data % other)
|
||||
|
||||
def __rmod__(self, other):
|
||||
return RefVar(other % self.data)
|
||||
|
||||
def __pow__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data ** other.data)
|
||||
return RefVar(self.data ** other)
|
||||
|
||||
def __rpow__(self, other):
|
||||
return RefVar(other ** self.data)
|
||||
|
||||
def __neg__(self):
|
||||
return RefVar(-self.data)
|
||||
|
||||
def __pos__(self):
|
||||
return RefVar(+self.data)
|
||||
|
||||
def __abs__(self):
|
||||
return RefVar(abs(self.data))
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return self.data == other.data
|
||||
return self.data == other
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self.__eq__(other)
|
||||
|
||||
def __lt__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return self.data < other.data
|
||||
return self.data < other
|
||||
|
||||
def __le__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return self.data <= other.data
|
||||
return self.data <= other
|
||||
|
||||
def __gt__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return self.data > other.data
|
||||
return self.data > other
|
||||
|
||||
def __ge__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return self.data >= other.data
|
||||
return self.data >= other
|
||||
|
||||
# 位运算
|
||||
def __and__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data & other.data)
|
||||
return RefVar(self.data & other)
|
||||
|
||||
def __rand__(self, other):
|
||||
return RefVar(other & self.data)
|
||||
|
||||
def __or__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data | other.data)
|
||||
return RefVar(self.data | other)
|
||||
|
||||
def __ror__(self, other):
|
||||
return RefVar(other | self.data)
|
||||
|
||||
def __xor__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data ^ other.data)
|
||||
return RefVar(self.data ^ other)
|
||||
|
||||
def __rxor__(self, other):
|
||||
return RefVar(other ^ self.data)
|
||||
|
||||
def __lshift__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data << other.data)
|
||||
return RefVar(self.data << other)
|
||||
|
||||
def __rlshift__(self, other):
|
||||
return RefVar(other << self.data)
|
||||
|
||||
def __rshift__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data >> other.data)
|
||||
return RefVar(self.data >> other)
|
||||
|
||||
def __rrshift__(self, other):
|
||||
return RefVar(other >> self.data)
|
||||
|
||||
def __invert__(self):
|
||||
return RefVar(~self.data)
|
||||
|
||||
# 类型转换
|
||||
def __int__(self):
|
||||
return int(self.data)
|
||||
|
||||
def __float__(self):
|
||||
return float(self.data)
|
||||
|
||||
def __bool__(self):
|
||||
return bool(self.data)
|
||||
|
||||
def __complex__(self):
|
||||
return complex(self.data)
|
||||
|
||||
def __bytes__(self):
|
||||
return bytes(self.data)
|
||||
|
||||
def __hash__(self):
|
||||
return hash(self.data)
|
||||
|
||||
# 容器操作(如果底层数据支持)
|
||||
def __len__(self):
|
||||
return len(self.data)
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self.data[key]
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
self.data[key] = value
|
||||
|
||||
def __delitem__(self, key):
|
||||
del self.data[key]
|
||||
|
||||
def __contains__(self, item):
|
||||
return item in self.data
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self.data)
|
||||
|
||||
def __iadd__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
self.data += other.data
|
||||
else:
|
||||
self.data += other
|
||||
return self
|
||||
|
||||
def __isub__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
self.data -= other.data
|
||||
else:
|
||||
self.data -= other
|
||||
return self
|
||||
|
||||
def __imul__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
self.data *= other.data
|
||||
else:
|
||||
self.data *= other
|
||||
return self
|
||||
|
||||
def __itruediv__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
self.data /= other.data
|
||||
else:
|
||||
self.data /= other
|
||||
return self
|
||||
|
||||
def __ifloordiv__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
self.data //= other.data
|
||||
else:
|
||||
self.data //= other
|
||||
return self
|
||||
|
||||
def __imod__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
self.data %= other.data
|
||||
else:
|
||||
self.data %= other
|
||||
return self
|
||||
|
||||
def __ipow__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
self.data **= other.data
|
||||
else:
|
||||
self.data **= other
|
||||
return self
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
if callable(self.data):
|
||||
return self.data(*args, **kwargs)
|
||||
raise TypeError(f"'{type(self.data).__name__}' object is not callable")
|
||||
|
||||
def __getattr__(self, name):
|
||||
return getattr(self.data, name)
|
||||
@@ -3,6 +3,8 @@ import heurams.kernel.particles as pt
|
||||
import toml
|
||||
import json
|
||||
from .lict import Lict
|
||||
from .refvar import RefVar
|
||||
|
||||
class Repo():
|
||||
file_mapping = {
|
||||
"orbital": "orbital.toml",
|
||||
@@ -12,14 +14,22 @@ class Repo():
|
||||
"formation": "formation.toml",
|
||||
}
|
||||
|
||||
type_mapping = {
|
||||
"orbital": "dict",
|
||||
"payload": "lict",
|
||||
"algodata": "lict",
|
||||
"manifest": "dict",
|
||||
"formation": "dict",
|
||||
}
|
||||
|
||||
default_save_list = ["algodata"]
|
||||
|
||||
def __init__(self, orbital, payload, manifest, formation, algodata, source = None) -> None:
|
||||
self.orbital: pt.Orbital = orbital
|
||||
self.payload: dict = payload
|
||||
self.payload: Lict = payload
|
||||
self.manifest: dict = manifest
|
||||
self.formation: dict = formation
|
||||
self.algodata: dict = algodata
|
||||
self.algodata: Lict = algodata
|
||||
self.source: Path | None = source # 若存在, 指向 repo 所在 dir
|
||||
self.database = {
|
||||
"orbital": self.orbital,
|
||||
@@ -29,7 +39,6 @@ class Repo():
|
||||
"algodata": self.algodata,
|
||||
"source": self.source,
|
||||
}
|
||||
self.particles = dict()
|
||||
|
||||
def __len__(self):
|
||||
return len(self.payload)
|
||||
@@ -44,10 +53,14 @@ class Repo():
|
||||
for keyname in save_list:
|
||||
filename = self.file_mapping[keyname]
|
||||
with open(source / filename, 'w') as f:
|
||||
try:
|
||||
dict_data = self.database[keyname].dicted_data
|
||||
except:
|
||||
dict_data = dict(self.database[keyname])
|
||||
if filename.endswith("toml"):
|
||||
toml.dump(self.database[keyname], f)
|
||||
toml.dump(dict_data, f)
|
||||
elif filename.endswith("json"):
|
||||
json.dump(self.database[keyname], f)
|
||||
json.dump(dict_data, f)
|
||||
else:
|
||||
raise ValueError(f"不支持的文件类型: {filename}")
|
||||
|
||||
@@ -58,8 +71,8 @@ class Repo():
|
||||
def create_new_repo(cls, source = None):
|
||||
default_database = {
|
||||
"orbital": {"puzzles": {}, "schedule": {}},
|
||||
"payload": {},
|
||||
"algodata": {},
|
||||
"payload": [],
|
||||
"algodata": [],
|
||||
"manifest": {},
|
||||
"formation": {"annotation": {}, "unified": {}},
|
||||
"source": source
|
||||
@@ -71,12 +84,19 @@ class Repo():
|
||||
database = {}
|
||||
for keyname, filename in cls.file_mapping.items():
|
||||
with open(source / filename, "r") as f:
|
||||
loaded: dict
|
||||
if filename.endswith("toml"):
|
||||
database[keyname] = toml.load(f)
|
||||
loaded = toml.load(f)
|
||||
elif filename.endswith("json"):
|
||||
database[keyname] = json.load(f)
|
||||
loaded = json.load(f)
|
||||
else:
|
||||
raise ValueError(f"不支持的文件类型: {filename}")
|
||||
if cls.type_mapping[keyname] == "lict":
|
||||
database[keyname] = Lict(loaded.items())
|
||||
elif cls.type_mapping[keyname] == "dict":
|
||||
database[keyname] = loaded
|
||||
else:
|
||||
raise ValueError(f"不支持的数据容器: {cls.type_mapping[keyname]}")
|
||||
database["source"] = source
|
||||
return Repo(**database)
|
||||
|
||||
@@ -93,15 +113,3 @@ class Repo():
|
||||
return 1
|
||||
except:
|
||||
return 0
|
||||
|
||||
def _load(self):
|
||||
pass
|
||||
|
||||
def _load_nucleons(self):
|
||||
self.particles["nucleons"] = Lict(initdict = self.payload)
|
||||
self.particles["electrons"] = Lict(initdict = self.algodata)
|
||||
|
||||
mtx["list"] =
|
||||
for ident, payload in self.payload:
|
||||
res.append()
|
||||
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
from typing import Any, Iterable, TypedDict
|
||||
from typing_extensions import Self
|
||||
from .lict import Lict
|
||||
|
||||
class ParticalsContainer(TypedDict):
|
||||
nucleons: Lict
|
||||
electrons: Lict
|
||||
atoms: Lict
|
||||
Reference in New Issue
Block a user