From 40d84d521619a74df1e57bdca97b347b88112a7c Mon Sep 17 00:00:00 2001 From: softwareengineerprogrammer <4056124+softwareengineerprogrammer@users.noreply.github.com> Date: Wed, 19 Jun 2024 08:25:48 -0700 Subject: [PATCH] Fixes & tests for comment parsing per https://github.com/NREL/GEOPHIRES-X/pull/239\#pullrequestreview-2128335311 --- src/geophires_x/GeoPHIRESUtils.py | 5 +++-- .../input_comments.txt | 20 +++++++++++++++++ .../test_geophires_input_parameters.py | 7 ++++++ tests/test_geophires_utils.py | 22 +++++++++++++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 tests/geophires_x_client_tests/input_comments.txt diff --git a/src/geophires_x/GeoPHIRESUtils.py b/src/geophires_x/GeoPHIRESUtils.py index 64a7e8b8..4e65ed02 100644 --- a/src/geophires_x/GeoPHIRESUtils.py +++ b/src/geophires_x/GeoPHIRESUtils.py @@ -520,8 +520,9 @@ def read_input_file(return_dict_1, logger=None, input_file_name=None): # successful read of data into list. Now make a dictionary with all the parameter entries. # Index will be the unique name of the parameter. # The value will be a "ParameterEntry" structure, with name, value (optionally with units), optional comment - for line in content: - if line.startswith('#'): + for raw_line in content: + line = raw_line.strip() + if any([line.startswith(x) for x in ['#', '--', '*']]): # skip any line that starts with "#" - # will be the comment parameter continue diff --git a/tests/geophires_x_client_tests/input_comments.txt b/tests/geophires_x_client_tests/input_comments.txt new file mode 100644 index 00000000..ba3559c7 --- /dev/null +++ b/tests/geophires_x_client_tests/input_comments.txt @@ -0,0 +1,20 @@ +# comment +# foo, bar +# foo, bar, baz +# foo, bar, baz, qux corge + +Gradient 1, 69 +Reservoir Depth, 5, -- comment here +End-Use Option, 1, # another comment +Power Plant Type, 4, comments galore + +# another, comment, with, commas, down here + # comment with space + # comment with space, and comma + # comment with space, and commas, plural?! + +-- another comment style +--- three is not the charm, hopefully + +**** A legacy-but-still-supported header comment style, indeed **** +******************************************************************* diff --git a/tests/geophires_x_client_tests/test_geophires_input_parameters.py b/tests/geophires_x_client_tests/test_geophires_input_parameters.py index dc8f926a..f105f11a 100644 --- a/tests/geophires_x_client_tests/test_geophires_input_parameters.py +++ b/tests/geophires_x_client_tests/test_geophires_input_parameters.py @@ -3,6 +3,7 @@ from pathlib import Path from geophires_x_client import GeophiresInputParameters +from geophires_x_client import GeophiresXClient from tests.base_test_case import BaseTestCase @@ -44,3 +45,9 @@ def test_init_with_input_file_and_parameters(self): with open(input_params.as_file_path(), encoding='UTF-8') as f: self.assertEqual('Foo, Bar\nBaz, Qux\nBaz, Quux\nQuuz, 2\n', f.read()) + + def test_input_file_comments(self): + result = GeophiresXClient().get_geophires_result( + GeophiresInputParameters(from_file_path=self._get_test_file_path('input_comments.txt')) + ) + self.assertIsNotNone(result) diff --git a/tests/test_geophires_utils.py b/tests/test_geophires_utils.py index 62e59a62..b9f8c0c1 100644 --- a/tests/test_geophires_utils.py +++ b/tests/test_geophires_utils.py @@ -1,6 +1,8 @@ import sys import unittest +from pathlib import Path +from geophires_x import GeoPHIRESUtils from geophires_x.GeoPHIRESUtils import RecoverableHeat from geophires_x.GeoPHIRESUtils import UtilEff_func from geophires_x.GeoPHIRESUtils import _interp_util_eff_func @@ -12,6 +14,8 @@ from geophires_x.GeoPHIRESUtils import quantity from geophires_x.GeoPHIRESUtils import vapor_pressure_water_kPa from geophires_x.GeoPHIRESUtils import viscosity_water_Pa_sec +from geophires_x.Parameter import ParameterEntry +from tests.base_test_case import BaseTestCase class TestCelsiusToKelvin(unittest.TestCase): @@ -524,5 +528,23 @@ def test_close_temperature(self): self.assertAlmostEqual(result, 419.1703812859442, places=3) +class GeophiresUtilsTestCase(BaseTestCase): + def test_input_comments(self): + d = {} + GeoPHIRESUtils.read_input_file( + d, input_file_name=Path(self._get_test_file_path('geophires_x_client_tests/input_comments.txt')).absolute() + ) + self.assertIsNotNone(d) + self.assertDictEqual( + d, + { + 'Gradient 1': ParameterEntry(Name='Gradient 1', sValue='69', Comment=''), + 'Reservoir Depth': ParameterEntry(Name='Reservoir Depth', sValue='5', Comment='-- comment here'), + 'End-Use Option': ParameterEntry(Name='End-Use Option', sValue='1', Comment='# another comment'), + 'Power Plant Type': ParameterEntry(Name='Power Plant Type', sValue='4', Comment='comments galore'), + }, + ) + + if __name__ == '__main__': unittest.main()