From 2fe178a2fb1fa991355c2ddc64caa31f0d7697f6 Mon Sep 17 00:00:00 2001 From: Mudassir Chapra <37051110+muddi900@users.noreply.github.com> Date: Wed, 4 Jan 2023 20:08:11 +0000 Subject: [PATCH] all open statements and file paths use `os.path.join` updated post --- pyelmer/elmer.py | 12 ++++++------ pyelmer/execute.py | 4 ++-- pyelmer/post.py | 4 ++-- pyelmer/test/test_elmer.py | 12 ++++++------ 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/pyelmer/elmer.py b/pyelmer/elmer.py index ca29de0..30776f5 100644 --- a/pyelmer/elmer.py +++ b/pyelmer/elmer.py @@ -121,7 +121,7 @@ def write_boundary_ids(self, simulation_dir): simulation_dir (str): Path of simulation directory """ data = {boundary.id: name for name, boundary in self.boundaries.items()} - with open(simulation_dir + "/boundaries.yml", "w") as f: + with open(os.path.join(simulation_dir + "/boundaries.yml"), "w") as f: yaml.dump(data, f, sort_keys=False) def _dict_to_str(self, dictionary, *, key_value_separator=" = "): @@ -425,7 +425,7 @@ def load_simulation(name, setup_file=""): Simulation object. """ if setup_file == "": - setup_file = f"{data_dir}/simulations.yml" + setup_file = os.path.join(data_dir, "simulations.yml") with open(setup_file) as f: settings = yaml.safe_load(f)[name] sim = Simulation() @@ -445,7 +445,7 @@ def load_material(name, simulation, setup_file=""): Material object. """ if setup_file == "": - setup_file = f"{data_dir}/materials.yml" + setup_file = os.path.join(data_dir, "materials.yml") with open(setup_file) as f: data = yaml.safe_load(f)[name] return Material(simulation, name, data) @@ -463,7 +463,7 @@ def load_solver(name, simulation, setup_file=""): Solver object. """ if setup_file == "": - setup_file = f"{data_dir}/solvers.yml" + setup_file = os.path.join(data_dir, "solvers.yml") with open(setup_file) as f: data = yaml.safe_load(f)[name] return Solver(simulation, name, data) @@ -481,7 +481,7 @@ def load_boundary(name, simulation, setup_file=""): Boundary object. """ if setup_file == "": - setup_file = f"{data_dir}/boundaries.yml" + setup_file = os.path.join(data_dir, "boundaries.yml") with open(setup_file) as f: data = yaml.safe_load(f)[name] return Boundary(simulation, name, data=data) @@ -499,7 +499,7 @@ def load_initial_condition(name, simulation, setup_file=""): InitialCondition object. """ if setup_file == "": - setup_file = f"{data_dir}/initial_conditions.yml" + setup_file = os.path.join(data_dir, "initial_conditions.yml") with open(setup_file) as f: data = yaml.safe_load(f)[name] return InitialCondition(simulation, name, data) diff --git a/pyelmer/execute.py b/pyelmer/execute.py index f2af72e..1a0301a 100644 --- a/pyelmer/execute.py +++ b/pyelmer/execute.py @@ -24,7 +24,7 @@ def run_elmer_grid(sim_dir, meshfile, elmergrid=None): elmergrid = "ElmerGrid" args = [elmergrid, "14", "2", meshfile] - with open(sim_dir + "/elmergrid.log", "w") as f: + with open(os.path.join(sim_dir, "elmergrid.log"), "w") as f: subprocess.run(args, cwd=sim_dir, stdout=f, stderr=f) mesh_dir = sim_dir + "/" + ".".join(meshfile.split(".")[:-1]) @@ -72,7 +72,7 @@ def run_elmer_solver(sim_dir, elmersolver=None): elmersolver = "ElmerSolver" args = [elmersolver, "case.sif"] - with open(sim_dir + "/elmersolver.log", "w") as f: + with open(os.path.join(sim_dir, "elmersolver.log"), "w") as f: subprocess.run(args, cwd=sim_dir, stdout=f, stderr=f) diff --git a/pyelmer/post.py b/pyelmer/post.py index 4de734d..c6a41a9 100644 --- a/pyelmer/post.py +++ b/pyelmer/post.py @@ -64,7 +64,7 @@ def scan_logfile(sim_dir): Returns: list[str], list[str], dict: error messages, warnings, statistics """ - with open(sim_dir + "/elmersolver.log", "r") as f: + with open(os.path.join(sim_dir, "elmersolver.log"), "r") as f: log = f.readlines() for i in range(len(log)): log[i] = log[i][:-1] @@ -92,7 +92,7 @@ def plot_residuals(sim_dir, solvers, save=False): solvers (list): solvers to analyze - currently works only for 'heat equation' and 'statmagsolver' """ - with open(sim_dir + "/elmersolver.log", "r") as f: + with open(os.path.join(sim_dir, "elmersolver.log"), "r") as f: log = f.readlines() for i in range(len(log)): log[i] = log[i][:-1] diff --git a/pyelmer/test/test_elmer.py b/pyelmer/test/test_elmer.py index 5e5346a..566adaf 100644 --- a/pyelmer/test/test_elmer.py +++ b/pyelmer/test/test_elmer.py @@ -159,7 +159,7 @@ def test_equation(): def test_load_simulation(): - with open(file_dir + "/test_data/simulations.yml") as f: + with open(os.path.join(file_dir, "test_data", "simulations.yml") as f: settings = yaml.safe_load(f)["test_simulation"] assert ( elmer.load_simulation( @@ -170,7 +170,7 @@ def test_load_simulation(): def test_load_material(): - with open(file_dir + "/test_data/materials.yml") as f: + with open(os.path.join(file_dir, "test_data", "materials.yml") as f: data = yaml.safe_load(f)["test_material"] sim = elmer.Simulation() assert ( @@ -182,7 +182,7 @@ def test_load_material(): def test_load_solver(): - with open(file_dir + "/test_data/solvers.yml") as f: + with open(os.path.join(file_dir, "test_data", "solvers.yml") as f: data = yaml.safe_load(f)["test_solver"] sim = elmer.Simulation() assert ( @@ -194,7 +194,7 @@ def test_load_solver(): def test_load_body_force(): - with open(file_dir + "/test_data/body_forces.yml") as f: + with open(os.path.join(file_dir, "test_data", "body_forces.yml") as f: data = yaml.safe_load(f)["test_body_force"] sim = elmer.Simulation() assert ( @@ -206,7 +206,7 @@ def test_load_body_force(): def test_load_boundary(): - with open(file_dir + "/test_data/boundaries.yml") as f: + with open(os.path.join(file_dir, "test_data", "boundaries.yml") as f: data = yaml.safe_load(f)["test_boundary"] sim = elmer.Simulation() assert elmer.load_boundary( @@ -218,7 +218,7 @@ def test_load_boundary(): def test_load_initial_condition(): - with open(file_dir + "/test_data/initial_conditions.yml") as f: + with open(os.path.join(file_dir, "test_data", "initial_conditions.yml") as f: data = yaml.safe_load(f)["test_initial_condition"] sim = elmer.Simulation() assert (