Skip to content

Commit

Permalink
Fixes & tests for comment parsing per https://github.com/NREL/GEOPHIR…
Browse files Browse the repository at this point in the history
  • Loading branch information
softwareengineerprogrammer committed Jun 19, 2024
1 parent 6f99d65 commit 40d84d5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/geophires_x/GeoPHIRESUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 20 additions & 0 deletions tests/geophires_x_client_tests/input_comments.txt
Original file line number Diff line number Diff line change
@@ -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 ****
*******************************************************************
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
22 changes: 22 additions & 0 deletions tests/test_geophires_utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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()

0 comments on commit 40d84d5

Please sign in to comment.