From 677b88fa1de8428d24643a3d01b5bc0e0184fef9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Schm=C3=B6lder?= Date: Wed, 4 Dec 2024 10:31:48 +0100 Subject: [PATCH 1/9] Add method to delete h5 files --- cadet/cadet.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cadet/cadet.py b/cadet/cadet.py index b749fc6..ea0eec2 100644 --- a/cadet/cadet.py +++ b/cadet/cadet.py @@ -523,6 +523,13 @@ def clear(self) -> None: if runner is not None: runner.clear() + def delete_file(self) -> None: + if self.filename is not None: + try: + os.remove(self.filename) + except FileNotFoundError: + pass + def __del__(self): self.clear() del self._cadet_dll_runner From c2a27b34d0575f3002c576813f71f6b1f6019c70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Schm=C3=B6lder?= Date: Wed, 4 Dec 2024 10:32:33 +0100 Subject: [PATCH 2/9] Delete files after tests --- tests/test_parallelization.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_parallelization.py b/tests/test_parallelization.py index a0506c6..88317de 100644 --- a/tests/test_parallelization.py +++ b/tests/test_parallelization.py @@ -8,6 +8,8 @@ def run_simulation(model): model.save() data = model.run_load() + model.delete_file() + return data From f54dfcc2feacc95deb675f79d8333b7953c1e403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Schm=C3=B6lder?= Date: Wed, 4 Dec 2024 10:32:11 +0100 Subject: [PATCH 3/9] Fix SyntaxWarning --- tests/test_install_path_settings.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_install_path_settings.py b/tests/test_install_path_settings.py index 99db92a..92bf006 100644 --- a/tests/test_install_path_settings.py +++ b/tests/test_install_path_settings.py @@ -47,7 +47,7 @@ def test_install_path(): def test_dll_runner_attrs(): cadet = Cadet(full_path_dll.parent.parent) cadet_runner = cadet._cadet_dll_runner - assert re.match("\d\.\d\.\d", cadet_runner.cadet_version) + assert re.match(r"\d\.\d\.\d", cadet_runner.cadet_version) assert isinstance(cadet_runner.cadet_branch, str) assert isinstance(cadet_runner.cadet_build_type, str | None) assert isinstance(cadet_runner.cadet_commit_hash, str) @@ -58,7 +58,7 @@ def test_dll_runner_attrs(): def test_cli_runner_attrs(): cadet = Cadet(full_path_dll.parent.parent) cadet_runner = cadet._cadet_cli_runner - assert re.match("\d\.\d\.\d", cadet_runner.cadet_version) + assert re.match(r"\d\.\d\.\d", cadet_runner.cadet_version) assert isinstance(cadet_runner.cadet_branch, str) assert isinstance(cadet_runner.cadet_build_type, str | None) assert isinstance(cadet_runner.cadet_commit_hash, str) From 83a7d239fcbef05bb47b08cd4cf62a6292f2c5f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Schm=C3=B6lder?= Date: Wed, 4 Dec 2024 10:33:02 +0100 Subject: [PATCH 4/9] Execute pytest if __name__ == "__main__" --- tests/test_parallelization.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_parallelization.py b/tests/test_parallelization.py index 88317de..7cc3c6a 100644 --- a/tests/test_parallelization.py +++ b/tests/test_parallelization.py @@ -40,3 +40,7 @@ def test_parallelization_simulation(): delayed(run_simulation)(model, ) for model in models ) assert results_sequential == results_parallel + + +if __name__ == "__main__": + pytest.main([__file__]) From 2233229e62af132dabb6175047e89dd7c6703d8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Schm=C3=B6lder?= Date: Wed, 4 Dec 2024 10:31:17 +0100 Subject: [PATCH 5/9] Allow resetting cadet_path in CadetMeta --- cadet/cadet.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/cadet/cadet.py b/cadet/cadet.py index ea0eec2..c6101f8 100644 --- a/cadet/cadet.py +++ b/cadet/cadet.py @@ -112,6 +112,10 @@ class CadetMeta(type): This meta class allows setting the `cadet_path` attribute for all instances of the `Cadet` class. """ + use_dll = False + cadet_cli_path = None + cadet_dll_path = None + cadet_create_lwe_path = None @property def cadet_path(cls) -> Optional[Path]: @@ -131,7 +135,7 @@ def cadet_path(cls) -> Optional[Path]: return None @cadet_path.setter - def cadet_path(cls, cadet_path: os.PathLike) -> None: + def cadet_path(cls, cadet_path: Optional[os.PathLike]) -> None: """ Set the CADET path and initialize the appropriate runner. @@ -145,13 +149,20 @@ def cadet_path(cls, cadet_path: os.PathLike) -> None: If the path is a DLL, a `CadetDLLRunner` runner is used. Otherwise, a `CadetFileRunner` runner is used. """ - cadet_path = Path(cadet_path) - warnings.warn( - "Support for setting cadet.cadet_path will be removed in a future version. " - "TODO: What alternative should be used?", + "Support for setting Cadet.cadet_path will be removed in a future version. " + "Please set the `install_path` on instance level.", DeprecationWarning ) + if cadet_path is None: + cls.use_dll = False + cls._install_path = None + cls.cadet_cli_path = None + cls.cadet_dll_path = None + cls.cadet_create_lwe_path = None + return + + cadet_path = Path(cadet_path) cls.use_dll = cadet_path.suffix in [".dll", ".so"] @@ -314,9 +325,9 @@ def cadet_path(self, cadet_path: os.PathLike) -> None: Otherwise, a `CadetFileRunner` runner is used. """ cadet_path = Path(cadet_path) - warnings.warn( - "Deprecation warning: Support for setting cadet.cadet_path will be removed in a future version.", + "Deprecation warning: Support for setting cadet.cadet_path will be removed " + " in a future version. Use `install_path` instead.", FutureWarning ) self.install_path = cadet_path From ef902d6a2afcdb857c0a1bbf22633069ebe73dd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Schm=C3=B6lder?= Date: Wed, 4 Dec 2024 10:37:26 +0100 Subject: [PATCH 6/9] Reset Cadet.cadet_path after testing If `cadet_path` is not reset, there are undesired consequences which affect other tests. --- tests/test_meta_class_install_path.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_meta_class_install_path.py b/tests/test_meta_class_install_path.py index e46e40e..1ae691e 100644 --- a/tests/test_meta_class_install_path.py +++ b/tests/test_meta_class_install_path.py @@ -29,3 +29,10 @@ def test_meta_class(): assert sim.install_path == install_path_conda assert sim.cadet_dll_path.parent.parent == install_path_conda assert sim.cadet_cli_path.parent.parent == install_path_conda + + # Reset Path + Cadet.cadet_path = None + + +if __name__ == "__main__": + pytest.main([__file__]) From b4cd8a4e3537992c9866c94ef70b1b4edbeb272b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Schm=C3=B6lder?= Date: Wed, 4 Dec 2024 10:45:57 +0100 Subject: [PATCH 7/9] Remove old test This test is obsolte; functionatlity is now tested in test_install_path_settings.py --- tests/test_autodetection.py | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 tests/test_autodetection.py diff --git a/tests/test_autodetection.py b/tests/test_autodetection.py deleted file mode 100644 index 8401049..0000000 --- a/tests/test_autodetection.py +++ /dev/null @@ -1,6 +0,0 @@ -from cadet import Cadet - - -def test_autodetection(): - sim = Cadet() - assert sim.cadet_runner is not None From 1579b0e2b96d7aec900c385458876c201bd6fba3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Schm=C3=B6lder?= Date: Wed, 4 Dec 2024 10:47:07 +0100 Subject: [PATCH 8/9] Compare parent directories when testing CLI/DLL path Because the DLL lives in different subdirectories in Windows / Posix, we should test their parent to ensure the test runs independent of the OS. --- tests/test_meta_class_install_path.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_meta_class_install_path.py b/tests/test_meta_class_install_path.py index 1ae691e..fe8d878 100644 --- a/tests/test_meta_class_install_path.py +++ b/tests/test_meta_class_install_path.py @@ -22,7 +22,7 @@ def test_meta_class(): assert sim.use_dll assert sim.install_path == full_path_dll.parent.parent assert sim.cadet_dll_path == full_path_dll - assert sim.cadet_cli_path.parent == full_path_dll.parent + assert sim.cadet_cli_path.parent.parent == full_path_dll.parent.parent # With an install path given, the sim instance should use the given install path sim = Cadet(install_path=install_path_conda) From b3f61852922dc4433fabe26e767d2a4a8c72ae9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Schm=C3=B6lder?= Date: Wed, 4 Dec 2024 10:48:00 +0100 Subject: [PATCH 9/9] Initialize cli/dll_runner variables in Cadet class Else, there could be errors when deleting the object. --- cadet/cadet.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cadet/cadet.py b/cadet/cadet.py index c6101f8..4de20f1 100644 --- a/cadet/cadet.py +++ b/cadet/cadet.py @@ -213,6 +213,9 @@ def __init__(self, install_path: Optional[Path] = None, use_dll: bool = False, * self.cadet_create_lwe_path: Optional[Path] = None self.return_information: Optional[dict] = None + self._cadet_cli_runner: Optional[CadetCLIRunner] = None + self._cadet_dll_runner: Optional[CadetDLLRunner] = None + # Regardless of settings in the Meta Class, if we get an install_path, we respect the install_path if install_path is not None: self.use_dll = use_dll