Skip to content

Commit

Permalink
resolve some SonarQube complaints
Browse files Browse the repository at this point in the history
  • Loading branch information
georgemccabe committed Dec 12, 2024
1 parent 75476cd commit 1105648
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 105 deletions.
85 changes: 12 additions & 73 deletions internal/tests/pytests/util/config_metplus/test_config_metplus.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,64 +68,6 @@ def test_get_default_config_list():
assert actual_both == expected_both


@pytest.mark.parametrize(
'regex,index,id,expected_result', [
# 0: No ID
(r'^FCST_VAR(\d+)_NAME$', 1, None,
{'1': [None],
'2': [None],
'4': [None]}),
# 1: ID and index 2
(r'(\w+)_VAR(\d+)_NAME', 2, 1,
{'1': ['FCST'],
'2': ['FCST'],
'4': ['FCST']}),
# 2: index 1, ID 2, multiple identifiers
(r'^FCST_VAR(\d+)_(\w+)$', 1, 2,
{'1': ['NAME', 'LEVELS'],
'2': ['NAME'],
'4': ['NAME']}),
# 3: command that StatAnalysis wrapper uses
(r'MODEL(\d+)$', 1, None,
{'1': [None],
'2': [None],}),
# 4: TCPairs conensus logic
(r'^TC_PAIRS_CONSENSUS(\d+)_(\w+)$', 1, 2,
{'1': ['NAME', 'MEMBERS', 'REQUIRED', 'MIN_REQ'],
'2': ['NAME', 'MEMBERS', 'REQUIRED', 'MIN_REQ']}),
]
)
@pytest.mark.util
def test_find_indices_in_config_section(metplus_config, regex, index,
id, expected_result):
config = metplus_config
config.set('config', 'FCST_VAR1_NAME', 'name1')
config.set('config', 'FCST_VAR1_LEVELS', 'level1')
config.set('config', 'FCST_VAR2_NAME', 'name2')
config.set('config', 'FCST_VAR4_NAME', 'name4')
config.set('config', 'MODEL1', 'model1')
config.set('config', 'MODEL2', 'model2')

config.set('config', 'TC_PAIRS_CONSENSUS1_NAME', 'name1')
config.set('config', 'TC_PAIRS_CONSENSUS1_MEMBERS', 'member1')
config.set('config', 'TC_PAIRS_CONSENSUS1_REQUIRED', 'True')
config.set('config', 'TC_PAIRS_CONSENSUS1_MIN_REQ', '1')
config.set('config', 'TC_PAIRS_CONSENSUS2_NAME', 'name2')
config.set('config', 'TC_PAIRS_CONSENSUS2_MEMBERS', 'member2')
config.set('config', 'TC_PAIRS_CONSENSUS2_REQUIRED', 'True')
config.set('config', 'TC_PAIRS_CONSENSUS2_MIN_REQ', '2')

indices = config_metplus.find_indices_in_config_section(regex, config,
index_index=index,
id_index=id)

pp = pprint.PrettyPrinter()
print(f'Indices:')
pp.pprint(indices)

assert indices == expected_result


@pytest.mark.parametrize(
'config_var_name, expected_indices, set_met_tool', [
('FCST_GRID_STAT_VAR1_NAME', [1], True),
Expand Down Expand Up @@ -409,14 +351,14 @@ def test_parse_var_list_both(metplus_config, data_type, list_created):
var_list = config_metplus.parse_var_list(conf, time_info=None, data_type=data_type)
print(f'var_list:{var_list}')
for list_to_check in list_created.split(':'):
if (not var_list[0][f'{list_to_check}_name'] == "NAME1" or
not var_list[1][f'{list_to_check}_name'] == "NAME1" or
not var_list[2][f'{list_to_check}_name'] == "NAME2" or
not var_list[3][f'{list_to_check}_name'] == "NAME2" or
not var_list[0][f'{list_to_check}_level'] == "LEVELS11" or
not var_list[1][f'{list_to_check}_level'] == "LEVELS12" or
not var_list[2][f'{list_to_check}_level'] == "LEVELS21" or
not var_list[3][f'{list_to_check}_level'] == "LEVELS22"):
if (var_list[0][f'{list_to_check}_name'] != "NAME1" or
var_list[1][f'{list_to_check}_name'] != "NAME1" or
var_list[2][f'{list_to_check}_name'] != "NAME2" or
var_list[3][f'{list_to_check}_name'] != "NAME2" or
var_list[0][f'{list_to_check}_level'] != "LEVELS11" or
var_list[1][f'{list_to_check}_level'] != "LEVELS12" or
var_list[2][f'{list_to_check}_level'] != "LEVELS21" or
var_list[3][f'{list_to_check}_level'] != "LEVELS22"):
assert False


Expand Down Expand Up @@ -517,8 +459,6 @@ def test_parse_var_list_fcst_and_obs_and_both(metplus_config, data_type, list_le
if expect[f'{dt_lower}_level'] != reality[f'{dt_lower}_level']:
assert False

assert True


# option defined in obs only
@pytest.mark.parametrize(
Expand Down Expand Up @@ -646,9 +586,9 @@ def test_parse_var_list_ensemble(metplus_config):
met_tool='ensemble_stat')

pp = pprint.PrettyPrinter()
print(f'ENSEMBLE_VAR_LIST:')
print('ENSEMBLE_VAR_LIST:')
pp.pprint(ensemble_var_list)
print(f'VAR_LIST:')
print('VAR_LIST:')
pp.pprint(var_list)

assert(len(ensemble_var_list) == len(expected_ens_list))
Expand Down Expand Up @@ -715,9 +655,9 @@ def test_parse_var_list_series_by(metplus_config):
met_tool='series_analysis')

pp = pprint.PrettyPrinter()
print(f'ExtractTiles var list:')
print('ExtractTiles var list:')
pp.pprint(actual_et_list)
print(f'SeriesAnalysis var list:')
print('SeriesAnalysis var list:')
pp.pprint(actual_sa_list)

assert len(actual_et_list) == len(expected_et_list)
Expand Down Expand Up @@ -898,7 +838,6 @@ def test_getraw_instance_with_unset_var(metplus_config):
"""! Replicates bug where CURRENT_FCST_NAME is substituted with
an empty string when copied from an instance section
"""
pytest.skip()
instance = 'my_section'
config = metplus_config
config.set('config', 'MODEL', 'FCST')
Expand Down
19 changes: 10 additions & 9 deletions internal/tests/pytests/util/string_manip/test_util_string_manip.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import pprint
from datetime import datetime

from metplus.util.string_manip import *
from metplus.util.string_manip import get_log_path, get_logfile_info, template_to_regex, subset_list, expand_int_string_to_list, round_0p5, validate_thresholds, get_threshold_via_regex, camel_to_underscore, remove_quotes, getlist, getlistint, list_to_str, comparison_to_letter_format, format_thresh, format_level, find_indices_in_config_section



@pytest.mark.parametrize(
Expand Down Expand Up @@ -373,7 +374,7 @@ def test_getlist_begin_end_incr(list_string, output_list):


@pytest.mark.parametrize(
'input, add_quotes, expected_output', [
'input_list, add_quotes, expected_output', [
(['a', 'b', 'c'], None, '"a", "b", "c"'),
(['0', '1', '2'], None, '"0", "1", "2"'),
(['a', 'b', 'c'], True, '"a", "b", "c"'),
Expand All @@ -385,11 +386,11 @@ def test_getlist_begin_end_incr(list_string, output_list):
]
)
@pytest.mark.util
def test_list_to_str(input, add_quotes, expected_output):
def test_list_to_str(input_list, add_quotes, expected_output):
if add_quotes is None:
assert list_to_str(input) == expected_output
assert list_to_str(input_list) == expected_output
else:
assert list_to_str(input, add_quotes=add_quotes) == expected_output
assert list_to_str(input_list, add_quotes=add_quotes) == expected_output


@pytest.mark.parametrize(
Expand Down Expand Up @@ -440,7 +441,7 @@ def test_format_level(level, expected_result):


@pytest.mark.parametrize(
'regex,index,id,expected_result', [
'regex,index,id_index,expected_result', [
# 0: No ID
(r'^FCST_VAR(\d+)_NAME$', 1, None,
{'1': [None],
Expand Down Expand Up @@ -468,7 +469,7 @@ def test_format_level(level, expected_result):
)
@pytest.mark.util
def test_find_indices_in_config_section(metplus_config, regex, index,
id, expected_result):
id_index, expected_result):
config = metplus_config
config.set('config', 'FCST_VAR1_NAME', 'name1')
config.set('config', 'FCST_VAR1_LEVELS', 'level1')
Expand All @@ -487,10 +488,10 @@ def test_find_indices_in_config_section(metplus_config, regex, index,
config.set('config', 'TC_PAIRS_CONSENSUS2_MIN_REQ', '2')

indices = find_indices_in_config_section(regex, config, index_index=index,
id_index=id)
id_index=id_index)

pp = pprint.PrettyPrinter()
print(f'Indices:')
print('Indices:')
pp.pprint(indices)

assert indices == expected_result
49 changes: 26 additions & 23 deletions metplus/util/string_manip.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import sys
import os
import re
from csv import reader
import random
import string
import logging
Expand Down Expand Up @@ -45,7 +44,7 @@ def remove_quotes(input_string):


def getlist(list_str, expand_begin_end_incr=True):
"""! Returns a list of string elements from a comma
"""!Returns a list of string elements from a comma
separated string of values.
This function MUST also return an empty list [] if s is '' empty.
This function is meant to handle these possible or similar inputs:
Expand All @@ -57,6 +56,8 @@ def getlist(list_str, expand_begin_end_incr=True):
a conf file returns '' an empty string.
@param list_str the string being converted to a list.
@param expand_begin_end_incr If False, do not expand begin_end_incr
notation into a list of items (defaults to True).
@returns list of strings formatted properly and expanded as needed
"""
if not list_str:
Expand All @@ -81,7 +82,7 @@ def getlist(list_str, expand_begin_end_incr=True):

# use regex split to split list string by commas that are not
# found within []s or ()s
item_list = re.split(r',\s*(?![^\[\]]*\]|[^()]*\))', list_str)
item_list = re.split(r',\s*(?![^\[\]]*]|[^()]*\))', list_str)

# regex split will still split by commas that are found between
# quotation marks, so call function to put them back together properly
Expand Down Expand Up @@ -196,11 +197,12 @@ def _fix_list(item_list):
# otherwise add it to the list buffer
else:
list_buffer.append(item)
else:
list_buffer.append(item)
if quote_count == 1:
fixed_list.append(','.join(list_buffer))
list_buffer.clear()
continue

list_buffer.append(item)
if quote_count == 1:
fixed_list.append(','.join(list_buffer))
list_buffer.clear()

# if there are still items in the buffer, add them to end of list
if list_buffer:
Expand All @@ -210,9 +212,8 @@ def _fix_list(item_list):
out_list = []
for item in fixed_list:
if item[0] == '"' and item[-1] == '"':
out_list.append(item.strip('"'))
else:
out_list.append(item)
item = item.strip('"')
out_list.append(item)

return out_list

Expand Down Expand Up @@ -334,18 +335,20 @@ def get_threshold_via_regex(thresh_string):
break

match = re.match(r'^('+comp+r')(.*\d.*)$', thresh)
if match:
comparison = match.group(1)
number = match.group(2)
# try to convert to float if it can, but allow string
try:
number = float(number)
except ValueError:
pass

comparison_number_list.append((comparison, number))
found_match = True
break
if not match:
continue

comparison = match.group(1)
number = match.group(2)
# try to convert to float if it can, but allow string
try:
number = float(number)
except ValueError:
pass

comparison_number_list.append((comparison, number))
found_match = True
break

# if no match was found for the item, return None
if not found_match:
Expand Down

0 comments on commit 1105648

Please sign in to comment.