Skip to content

Commit 0d52ae0

Browse files
Overwrite existing output file
1 parent 125c6a6 commit 0d52ae0

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

causal_testing/json_front/json_class.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,25 +219,30 @@ def add_modelling_assumptions(self, estimation_model: Estimator): # pylint: dis
219219
return
220220

221221
def _append_to_file(self, line: str, log_level: int = None):
222-
""" Appends given line(s) to the current output file. If log_level is specified it also logs that message to the
222+
"""Appends given line(s) to the current output file. If log_level is specified it also logs that message to the
223223
logging level.
224224
:param line: The line or lines of text to be appended to the file
225225
:param log_level: An integer representing the logging level as specified by pythons inbuilt logging module. It
226226
is possible to use the inbuilt logging level variables such as logging.INFO and logging.WARNING
227227
"""
228-
with open(self.output_path, "a", encoding='utf-8') as f:
229-
f.write(line + "\n", )
228+
with open(self.output_path, "a", encoding="utf-8") as f:
229+
f.write(
230+
line + "\n",
231+
)
230232
if log_level:
231233
logger.log(level=log_level, msg=line)
232234

233235
@staticmethod
234236
def check_file_exists(output_path: Path, overwrite: bool):
235-
""" Method that checks if the given path to an output file already exists. If overwrite is true the check is
237+
"""Method that checks if the given path to an output file already exists. If overwrite is true the check is
236238
passed.
237239
:param output_path: File path for the output file of the JSON Frontend
238240
:param overwrite: bool that if true, the current file can be overwritten
239241
"""
240-
if not overwrite and output_path.is_file():
242+
if overwrite:
243+
file = open(output_path, "w", encoding="utf-8")
244+
file.close()
245+
elif output_path.is_file():
241246
raise FileExistsError(f"Chosen file output ({output_path}) already exists")
242247

243248
@staticmethod
@@ -257,7 +262,7 @@ def get_args(test_args=None) -> argparse.Namespace:
257262
parser.add_argument(
258263
"-w",
259264
help="Specify to overwrite any existing output files. This can lead to the loss of existing outputs if not "
260-
"careful",
265+
"careful",
261266
action="store_true",
262267
)
263268
parser.add_argument(

tests/json_front_tests/test_json_class.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ def setUp(self) -> None:
4141
self.json_class.setup(self.scenario)
4242

4343
def test_setting_paths(self):
44-
self.assertEqual(self.json_class.paths.json_path, Path(self.json_path))
45-
self.assertEqual(self.json_class.paths.dag_path, Path(self.dag_path))
46-
self.assertEqual(self.json_class.paths.data_paths, [Path(self.data_path[0])]) # Needs to be list of Paths
44+
self.assertEqual(self.json_class.input_paths.json_path, Path(self.json_path))
45+
self.assertEqual(self.json_class.input_paths.dag_path, Path(self.dag_path))
46+
self.assertEqual(self.json_class.input_paths.data_paths, [Path(self.data_path[0])]) # Needs to be list of Paths
4747

4848
def test_set_inputs(self):
4949
ctf_input = [Input("test_input", float, self.example_distribution)]

0 commit comments

Comments
 (0)