From 813f377b0d34dc211820e8e869c106a4adf8cd0b Mon Sep 17 00:00:00 2001 From: peverwhee Date: Fri, 13 Sep 2024 17:28:49 -0600 Subject: [PATCH] more python review comments; fix unit tests --- cime_config/hist_config.py | 252 ++++++++---------- .../hist_config_files/atm_in_flat | 6 +- .../hist_config_files/atm_in_multi | 2 +- test/unit/test_hist_config.py | 13 +- 4 files changed, 126 insertions(+), 147 deletions(-) diff --git a/cime_config/hist_config.py b/cime_config/hist_config.py index 5c1b4286..1f23b530 100644 --- a/cime_config/hist_config.py +++ b/cime_config/hist_config.py @@ -33,10 +33,6 @@ # %s = seconds since midnight GMT in current day, # %u = unit number (e.g., h0, i) # -# rhfilename_spec is the template for history restart files -_DEFAULT_RESTART_HIST_SPEC = '%c.cam.r%u.%y-%m-%d-%s.nc' -# hfilename_spec is the template for each history file -_DEFAULT_HISTORY_SPEC = '%c.cam.%u%f.%y-%m-%d-%s.nc' # Note, these lists should match the corresponding lists in # cam_hist_config_file.F90 @@ -46,7 +42,9 @@ 'steps', 'seconds', 'minutes', 'hours', 'days', 'months', 'years'] _OUT_PRECS = ['REAL32', 'REAL64'] -_NETCDF_ID_RE = re.compile(r"^[a-z][a-z0-9_]{0,62}$", re.IGNORECASE) +_TRUE_VALUES = {"true", "t", ".true."} +_FALSE_VALUES = {"false", "f", ".false."} +_NETCDF_ID_RE = re.compile(r"^[a-z]\w{0,62}$", re.IGNORECASE) ############################################################################## ### @@ -81,7 +79,8 @@ def _blank_config_line(line): >>> _blank_config_line("this is a ! weird line but is not blank") False """ - return (not line.strip()) or (line.strip()[0] == '!') + sline = line.strip() + return not sline or sline.startswith('!') ############################################################################## def _is_integer(entry): @@ -102,7 +101,7 @@ def _is_integer(entry): ival = int(str(entry).strip()) except ValueError: ival = None - errmsg = f"{entry.strip()} is not an integer" + errmsg = f"{entry} is not an integer" # end try # end if return ival, errmsg @@ -119,45 +118,33 @@ def _list_of_idents(entry, sep=','): >>> _list_of_idents("foo") (['foo'], None) >>> _list_of_idents("foo bar") - (None, "'foo bar' is not a valid identifier") + (None, 'Found invalid identifiers:\\n foo bar') >>> _list_of_idents("foo, bAr") (['foo', 'bAr'], None) >>> _list_of_idents("foo, BA2r3") (['foo', 'BA2r3'], None) >>> _list_of_idents("foo, 3bar") - (None, "'3bar' is not a valid identifier") + (None, 'Found invalid identifiers:\\n 3bar') >>> _list_of_idents("foo.3bar") - (None, "'foo.3bar' is not a valid identifier") + (None, 'Found invalid identifiers:\\n foo.3bar') >>> _list_of_idents("foo3bariendnaadfasdfbasdlkfap983rasdfvalsda938qjnasdasd98adfasxd") - (None, "'foo3bariendnaadfasdfbasdlkfap983rasdfvalsda938qjnasdasd98adfasxd' is not a valid identifier") + (None, 'Found invalid identifiers:\\n foo3bariendnaadfasdfbasdlkfap983rasdfvalsda938qjnasdasd98adfasxd') >>> _list_of_idents("foo.3bar, foo3bariendnaadfasdfbasdlkfap983rasdfvalsda938qjnasdasd98adfasxd") - (None, "'foo.3bar' is not a valid identifier\\n 'foo3bariendnaadfasdfbasdlkfap983rasdfvalsda938qjnasdasd98adfasxd' is not a valid identifier") + (None, 'Found invalid identifiers:\\n foo.3bar\\n foo3bariendnaadfasdfbasdlkfap983rasdfvalsda938qjnasdasd98adfasxd') >>> _list_of_idents("foo.3bar; foo", sep=';') - (None, "'foo.3bar' is not a valid identifier") + (None, 'Found invalid identifiers:\\n foo.3bar') >>> _list_of_idents(" ") (None, 'No identifiers found') """ - errmsg = None if entry.strip(): - good_list = [x.strip() for x in entry.split(sep)] - for sample in good_list: - if _NETCDF_ID_RE.match(sample) is None: - if errmsg: - errmsg += "\n " - else: - errmsg = "" - # end if - errmsg += f"'{sample}' is not a valid identifier" - # end if - # end for - if errmsg: - good_list = None + potential_list = [x.strip() for x in entry.split(sep)] + bad_list = [sample for sample in potential_list if _NETCDF_ID_RE.match(sample) is None] + if len(bad_list) > 0: + return (None, f"Found invalid identifiers:\n " + '\n '.join(bad_list)) # end if - else: - good_list = None - errmsg = "No identifiers found" + return (potential_list, None) # end if - return good_list, errmsg + return (None, "No identifiers found") ############################################################################## def _is_mult_period(entry): @@ -182,44 +169,44 @@ def _is_mult_period(entry): >>> _is_mult_period("1*nyear") ((1, 'nyear'), None) >>> _is_mult_period("-6*nhours") - (None, 'multiplier must be a positive integer') + (None, "multiplier '-6' in '-6*nhours' must be a positive integer") >>> _is_mult_period("7h*nhours") - (None, 'multiplier must be a positive integer') + (None, "multiplier '7h' in '7h*nhours' must be a positive integer") >>> _is_mult_period("5*nhours*ndays") - (None, "Bad formatting of frequency, must be in the form of '*