diff --git a/docs/experiment.md b/docs/experiment.md index 3d6a538..aabda07 100644 --- a/docs/experiment.md +++ b/docs/experiment.md @@ -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`则会覆盖掉实验的所有信息 @@ -173,7 +173,7 @@ exp.save() exp.exit() ``` `Experiment.save`也有一些参数: -* `extra_filepath`: 将存档写入额外的路径 +* `target_path`: 将存档写入自己指定的路径 * `ln`: 输出存档的元件字符串是否换行 * `no_print_info`: 是否打印写入存档的元件数, 导线数(如果是电学实验的话) diff --git a/docs/log.md b/docs/log.md index 3c83a56..34bfa47 100644 --- a/docs/log.md +++ b/docs/log.md @@ -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` diff --git a/physicsLab/Experiment.py b/physicsLab/Experiment.py index b8fe5f7..89d9af8 100644 --- a/physicsLab/Experiment.py +++ b/physicsLab/Experiment.py @@ -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 @@ -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"] @@ -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) @@ -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: 是否打印写入存档的元件数, 导线数(如果是电学实验的话) ''' @@ -401,19 +404,19 @@ 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() @@ -421,13 +424,8 @@ def _format_StatusSave(json_str: str) -> str: 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: