From 97bc22b83b4d42d0815d4145629b06cf4b1d5055 Mon Sep 17 00:00:00 2001 From: Landung 'Don' Setiawan Date: Tue, 23 Jan 2024 15:26:13 -0800 Subject: [PATCH 1/5] refactor: Use Path.cwd instead of os.path.curdir --- src/caustics/sims/state_dict.py | 3 +-- tests/sims/test_state_dict.py | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/caustics/sims/state_dict.py b/src/caustics/sims/state_dict.py index 66bcade0..49e26df3 100644 --- a/src/caustics/sims/state_dict.py +++ b/src/caustics/sims/state_dict.py @@ -2,7 +2,6 @@ from collections import OrderedDict from typing import Any, Dict, Optional from pathlib import Path -import os from torch import Tensor import torch @@ -185,7 +184,7 @@ def save(self, file_path: Optional[str] = None) -> str: The final path of the saved file """ if not file_path: - file_path = Path(os.path.curdir) / self.__st_file + file_path = Path.cwd() / self.__st_file elif isinstance(file_path, str): file_path = Path(file_path) diff --git a/tests/sims/test_state_dict.py b/tests/sims/test_state_dict.py index ae420770..3b962106 100644 --- a/tests/sims/test_state_dict.py +++ b/tests/sims/test_state_dict.py @@ -1,6 +1,5 @@ from pathlib import Path from tempfile import TemporaryDirectory -import os import sys import pytest @@ -138,7 +137,7 @@ def test_st_file_string(self, simple_state_dict): def test_save(self, simple_state_dict): # Check for default save path - expected_fpath = Path(os.path.curdir) / simple_state_dict._StateDict__st_file + expected_fpath = Path.cwd() / simple_state_dict._StateDict__st_file default_fpath = simple_state_dict.save() assert Path(default_fpath).exists() From 809d4c132e7bca4452cc5245393f13e11e095a54 Mon Sep 17 00:00:00 2001 From: Landung 'Don' Setiawan Date: Tue, 23 Jan 2024 15:36:35 -0800 Subject: [PATCH 2/5] fix: Use open to write bytes and Path to construct path --- src/caustics/io.py | 4 +++- tests/test_io.py | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/caustics/io.py b/src/caustics/io.py index d5a146a8..ddb517c1 100644 --- a/src/caustics/io.py +++ b/src/caustics/io.py @@ -44,7 +44,9 @@ def to_file( # Normalize path to pathlib.Path object path = _normalize_path(path) - path.write_bytes(data) + with open(path, "wb") as f: + f.write(data) + return str(path.absolute()) diff --git a/tests/test_io.py b/tests/test_io.py index 18d56bfc..e5921a82 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -15,13 +15,13 @@ def test_normalize_path(): # Test with a string path - path_str = "/path/to/file.txt" + path_str = str(Path().joinpath("path", "to", "file.txt")) normalized_path = _normalize_path(path_str) assert normalized_path == Path(path_str) assert str(normalized_path), path_str # Test with a Path object - path_obj = Path("/path/to/file.txt") + path_obj = Path(path_str) normalized_path = _normalize_path(path_obj) assert normalized_path == path_obj From 7c8342f0234b44565b8a60e603d15e3180cfeff8 Mon Sep 17 00:00:00 2001 From: Landung 'Don' Setiawan Date: Tue, 23 Jan 2024 15:47:41 -0800 Subject: [PATCH 3/5] test: Update normalize path test --- tests/test_io.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_io.py b/tests/test_io.py index e5921a82..d3116b7c 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -14,16 +14,16 @@ def test_normalize_path(): + path_obj = Path().joinpath("path", "to", "file.txt") # Test with a string path - path_str = str(Path().joinpath("path", "to", "file.txt")) + path_str = str(path_obj) normalized_path = _normalize_path(path_str) - assert normalized_path == Path(path_str) - assert str(normalized_path), path_str + assert normalized_path == path_obj.absolute() + assert str(normalized_path) == str(path_obj.absolute()) # Test with a Path object - path_obj = Path(path_str) normalized_path = _normalize_path(path_obj) - assert normalized_path == path_obj + assert normalized_path == path_obj.absolute() def test_to_and_from_file(): From 92357246a97067d84eb8759cded606777832e29c Mon Sep 17 00:00:00 2001 From: Landung 'Don' Setiawan Date: Tue, 23 Jan 2024 16:40:39 -0800 Subject: [PATCH 4/5] test: Updated code to skip saving on Windows --- tests/sims/test_simulator.py | 9 ++++++--- tests/sims/test_state_dict.py | 18 ++++++++++++------ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/tests/sims/test_simulator.py b/tests/sims/test_simulator.py index 1a5d528b..e6adc2be 100644 --- a/tests/sims/test_simulator.py +++ b/tests/sims/test_simulator.py @@ -43,6 +43,10 @@ def test_set_module_params(self, simple_common_sim): assert simple_common_sim.param1 == params["param1"] assert simple_common_sim.param2 == params["param2"] + @pytest.mark.skipif( + sys.platform.startswith("win"), + reason="Built-in open has different behavior on Windows", + ) def test_load_state_dict(self, simple_common_sim): fpath = simple_common_sim.state_dict().save() loaded_state_dict = StateDict.load(fpath) @@ -65,6 +69,5 @@ def test_load_state_dict(self, simple_common_sim): == simple_common_sim.z_s.value ) - # Cleanup after only for non-windows - if not sys.platform.startswith("win"): - Path(fpath).unlink(missing_ok=True) + # Cleanup after + Path(fpath).unlink() diff --git a/tests/sims/test_state_dict.py b/tests/sims/test_state_dict.py index 3b962106..3a4f04ee 100644 --- a/tests/sims/test_state_dict.py +++ b/tests/sims/test_state_dict.py @@ -135,6 +135,10 @@ def test_st_file_string(self, simple_state_dict): assert simple_state_dict._StateDict__st_file == expected_file + @pytest.mark.skipif( + sys.platform.startswith("win"), + reason="Built-in open has different behavior on Windows", + ) def test_save(self, simple_state_dict): # Check for default save path expected_fpath = Path.cwd() / simple_state_dict._StateDict__st_file @@ -143,9 +147,8 @@ def test_save(self, simple_state_dict): assert Path(default_fpath).exists() assert default_fpath == str(expected_fpath.absolute()) - # Cleanup after only for non-windows - if not sys.platform.startswith("win"): - Path(default_fpath).unlink(missing_ok=True) + # Cleanup after + Path(default_fpath).unlink(missing_ok=True) # Check for specified save path with TemporaryDirectory() as tempdir: @@ -162,11 +165,14 @@ def test_save(self, simple_state_dict): with pytest.raises(ValueError): saved_path = simple_state_dict.save(str(wrong_fpath.absolute())) + @pytest.mark.skipif( + sys.platform.startswith("win"), + reason="Built-in open has different behavior on Windows", + ) def test_load(self, simple_state_dict): fpath = simple_state_dict.save() loaded_state_dict = StateDict.load(fpath) assert loaded_state_dict == simple_state_dict - # Cleanup after only for non-windows - if not sys.platform.startswith("win"): - Path(fpath).unlink(missing_ok=True) + # Cleanup after + Path(fpath).unlink() From 71f1a89fa1fdc8c632aec092f7b60064e624ecf9 Mon Sep 17 00:00:00 2001 From: Landung 'Don' Setiawan Date: Tue, 23 Jan 2024 17:00:23 -0800 Subject: [PATCH 5/5] test: Remove missing_ok --- tests/sims/test_state_dict.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/sims/test_state_dict.py b/tests/sims/test_state_dict.py index 3a4f04ee..4d15591d 100644 --- a/tests/sims/test_state_dict.py +++ b/tests/sims/test_state_dict.py @@ -148,7 +148,7 @@ def test_save(self, simple_state_dict): assert default_fpath == str(expected_fpath.absolute()) # Cleanup after - Path(default_fpath).unlink(missing_ok=True) + Path(default_fpath).unlink() # Check for specified save path with TemporaryDirectory() as tempdir: