Skip to content

Commit

Permalink
Fix list parsing error, add pytest
Browse files Browse the repository at this point in the history
Checking for commas had to be moved to first in cast_as_dtype() to
avoid commas being considered part of the datatime string.

Also added pytest for list parsing.
  • Loading branch information
WalterKolczynski-NOAA committed Jul 25, 2024
1 parent bb1eab4 commit ef5ae37
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/wxflow/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ def cast_as_dtype(string: str) -> Union[str, int, float, bool, Any]:
BOOLS = ['n', 'no', 'f', 'false', '.f.', '.false.'] + TRUTHS
BOOLS = [x.upper() for x in BOOLS] + BOOLS + ['Yes', 'No', 'True', 'False']

if ',' in string:
# Convert comma-separated list to python list
return [ cast_as_dtype(elem.strip()) for elem in string.split(',') ]

def _cast_or_not(to_type: Any, string: str):
try:
return to_type(string)
Expand All @@ -187,9 +191,6 @@ def _true_or_not(string: str):
except Exception as exc:
if string in BOOLS: # Likely a boolean, convert to True/False
return _true_or_not(string)
elif ',' in string:
# Convert comma-separated list to python list
return [ cast_as_dtype(elem) for elem in string.split(',') ]
elif '.' in string: # Likely a number and that too a float
return _cast_or_not(float, string)
else: # Still could be a number, may be an integer
Expand Down
23 changes: 22 additions & 1 deletion tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
export SOME_BOOL4=NO
export SOME_BOOL5=.false.
export SOME_BOOL6=.F.
export SOME_LIST1="3, 15, -999"
export SOME_LIST2="0.2,3.5,-9999."
export SOME_LIST3="20221225, 202212251845"
export SOME_LIST4="YES, .false., .T."
export SOME_LIST5="0.2, 15, 20221225, NO"
"""

file1 = """#!/bin/bash
Expand Down Expand Up @@ -60,7 +65,12 @@
'SOME_BOOL3': True,
'SOME_BOOL4': False,
'SOME_BOOL5': False,
'SOME_BOOL6': False
'SOME_BOOL6': False,
'SOME_LIST1': [3, 15, -999],
'SOME_LIST2': [0.2,3.5,-9999.],
'SOME_LIST3': [datetime(2022, 12, 25, 0, 0, 0), datetime(2022, 12, 25, 18, 45, 0)],
'SOME_LIST4': [True, False, True],
'SOME_LIST5': [0.2, 15, datetime(2022, 12, 25, 0, 0, 0), False],
}

file0_dict_set_envvar = file0_dict.copy()
Expand Down Expand Up @@ -107,6 +117,14 @@
('20221215T1830Z', datetime(2022, 12, 15, 18, 30, 0)),
]

list_dtypes = [
('3, 15, -999', [3, 15, -999]),
('0.2,3.5,-9999.', [0.2,3.5,-9999.]),
('20221215,20221215T1830Z', [datetime(2022, 12, 15, 0, 0, 0), datetime(2022, 12, 15, 18, 30, 0)]),
('YES, .false., .T.', [True, False, True]),
('0.2, 15, 20221225, NO', [0.2, 15, datetime(2022, 12, 25, 0, 0, 0), False]),
]


def evaluate(dtypes):
for pair in dtypes:
Expand All @@ -133,6 +151,9 @@ def test_cast_as_dtype_bool():
def test_cast_as_dtype_datetimes():
evaluate(datetime_dtypes)

def test_cast_as_dtype_list():
evaluate(list_dtypes)


@pytest.fixture
def create_configs(tmp_path):
Expand Down

0 comments on commit ef5ae37

Please sign in to comment.