Skip to content

Commit

Permalink
change param extra_filepath of Experiment.save to target_path
Browse files Browse the repository at this point in the history
  • Loading branch information
GoodenoughPhysicsLab committed Jan 1, 2025
1 parent c99f8e1 commit eaa630d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
4 changes: 2 additions & 2 deletions docs/experiment.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ with experiment('example') as exp:
* `experiment_type`: 若创建实验,支持传入指定实验类型
* `extra_filepath`: 将存档写入额外的路径
* `force_crt`: 强制创建一个实验, 若已存在则覆盖已有实验
* `is_exit`: 是否调用`Experiment.exit`退出实验而不是调用`Experiment.save`或者`Experiment.delete`
* `is_exit`: 若为True, 则不保存实验

> Note: 当你使用`Experiment`导入一个实验而不调用`read`时,你仅仅只会损失实验所有原件的信息,而`force_crt`则会覆盖掉实验的所有信息
Expand Down Expand Up @@ -173,7 +173,7 @@ exp.save()
exp.exit()
```
`Experiment.save`也有一些参数:
* `extra_filepath`: 将存档写入额外的路径
* `target_path`: 将存档写入自己指定的路径
* `ln`: 输出存档的元件字符串是否换行
* `no_print_info`: 是否打印写入存档的元件数, 导线数(如果是电学实验的话)

Expand Down
1 change: 1 addition & 0 deletions docs/log.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,4 @@
6. 移除`Experiment.save``no_pop`参数
7. 移除`Experiment.delete`, `Experiment.exit`增加`delete`参数
8. 移除`Experiment.get_element_from_identifier`,新增`get_element_from_identifier`函数
9.`Experiment.save``extra_filepath`改为`target_path`
26 changes: 12 additions & 14 deletions physicsLab/Experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ def __init__(self, open_mode: OpenMode, content_id: str, category: Category, use
def __init__(self, open_mode: OpenMode, sav_name: str, experiment_type: ExperimentType, force_crt: bool) -> None:
''' 创建一个新实验
@open_mode = OpenMode.crt
@sav_name: 存档的名字
@experiment_type: 实验类型
@force_crt: 强制创建一个实验, 若已存在则覆盖已有实验
'''

#TODO support **kwargs
Expand Down Expand Up @@ -235,7 +238,7 @@ def __init__(self, open_mode: OpenMode, *args) -> None:
else:
raise errors.InternalError

self._read_CameraSave(self.PlSav["Experiment"]["CameraSave"])
self._load_CameraSave(self.PlSav["Experiment"]["CameraSave"])

if self.PlSav["Summary"] is None:
self.PlSav["Summary"] = savTemplate.Circuit["Summary"]
Expand Down Expand Up @@ -341,7 +344,7 @@ def __init__(self, open_mode: OpenMode, *args) -> None:
assert isinstance(self.is_elementXYZ, bool)
assert isinstance(self.elementXYZ_origin_position, _tools.position)

def _read_CameraSave(self, camera_save: str) -> None:
def _load_CameraSave(self, camera_save: str) -> None:
assert isinstance(camera_save, str)

self.CameraSave = json.loads(camera_save)
Expand Down Expand Up @@ -375,12 +378,12 @@ def __write(self) -> None:
@_check_method
def save(
self,
extra_filepath: Optional[str] = None, # 改为target_output_path: str | List[str], 默认是SAV_PATH_ROOT
target_path: Optional[str] = None,
ln: bool = False,
no_print_info: bool = False,
) -> Self:
''' 以物实存档的格式导出实验
@param extra_filepath: 自定义保存存档的路径, 但仍会在 SAV_PATH_ROOT 下保存存档
@param target_path: 将存档保存在此路径 (要求必须是file), 默认为 SAV_PATH
@param ln: 是否将StatusSave字符串换行 (便于查看存档, 但会导致不符合标准json的格式, 虽然物实可以读取)
@param no_print_info: 是否打印写入存档的元件数, 导线数(如果是电学实验的话)
'''
Expand All @@ -401,33 +404,28 @@ def _format_StatusSave(json_str: str) -> str:
json_str = json_str.replace("色导线\\\"}]}", "色导线\\\"}\n ]}")
return json_str

if not isinstance(extra_filepath, (str, type(None))) or \
if not isinstance(target_path, (str, type(None))) or \
not isinstance(ln, bool) or \
not isinstance(no_print_info, bool):
raise TypeError
if target_path is None:
target_path = self.SAV_PATH

if self.open_mode in (OpenMode.load_by_sav_name, OpenMode.load_by_filepath, OpenMode.load_by_plar_app):
status: str = "update"
elif self.open_mode == OpenMode.crt:
status: str = "create"
else:
raise errors.InternalError

assert self.SAV_PATH is not None
assert False

self.__write()

context: str = json.dumps(self.PlSav, indent=2, ensure_ascii=False, separators=(',', ':'))
if ln:
context = _format_StatusSave(context)

with open(self.SAV_PATH, "w", encoding="utf-8") as f:
with open(target_path, "w", encoding="utf-8") as f:
f.write(context)
if extra_filepath is not None:
if not extra_filepath.endswith(".sav"):
extra_filepath += ".sav"
with open(extra_filepath, "w", encoding="utf-8") as f:
f.write(context)

if not no_print_info:
if self.experiment_type == ExperimentType.Circuit:
Expand Down

0 comments on commit eaa630d

Please sign in to comment.