Skip to content

Commit

Permalink
remove ordered dict from compare_namelist.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards4b committed Dec 13, 2024
1 parent 57ccd3f commit dd40400
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions CIME/compare_namelists.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import os, re, logging

from collections import OrderedDict
from CIME.utils import expect, CIMEError

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -73,11 +71,11 @@ def _interpret_value(value_str, filename):
>>> _interpret_value("3*1.0", "foo")
['1.0', '1.0', '1.0']
>>> _interpret_value("'DMS -> value.nc'", "foo")
OrderedDict([('DMS', 'value.nc')])
{'DMS': 'value.nc'}
>>> _interpret_value("'DMS -> 1.0 * value.nc'", "foo")
OrderedDict([('DMS', '1.0*value.nc')])
{'DMS': '1.0*value.nc'}
>>> _interpret_value("'DMS -> 1.0* value.nc'", "foo")
OrderedDict([('DMS', '1.0*value.nc')])
{'DMS': '1.0*value.nc'}
"""
comma_re = re.compile(r"\s*,\s*")
dict_re = re.compile(r"^'(\S+)\s*->\s*(\S+|(?:\S+\s*\*\s*\S+))\s*'")
Expand All @@ -87,7 +85,7 @@ def _interpret_value(value_str, filename):
tokens = [item.strip() for item in comma_re.split(value_str) if item.strip() != ""]
if "->" in value_str:
# dict
rv = OrderedDict()
rv = {}
for token in tokens:
m = dict_re.match(token)
expect(
Expand Down Expand Up @@ -151,15 +149,15 @@ def _parse_namelists(namelist_lines, filename):
... /
... '''
>>> _parse_namelists(teststr.splitlines(), 'foo')
OrderedDict([('nml', OrderedDict([('val', "'foo'"), ('aval', ["'one'", "'two'", "'three'"]), ('maval', ["'one'", "'two'", "'three'", "'four'"]), ('dval', OrderedDict([('one', 'two'), ('three', 'four')])), ('mdval', OrderedDict([('one', 'two'), ('three', 'four'), ('five', 'six')])), ('nval', '1850')])), ('nml2', OrderedDict([('val2', '.false.')]))])
{'nml': {'val': "'foo'", 'aval': ["'one'", "'two'", "'three'"], 'maval': ["'one'", "'two'", "'three'", "'four'"], 'dval': {'one': 'two', 'three': 'four'}, 'mdval': {'one': 'two', 'three': 'four', 'five': 'six'}, 'nval': '1850'}, 'nml2': {'val2': '.false.'}}
>>> teststr = '''&fire_emis_nl
... fire_emis_factors_file = 'fire_emis_factors_c140116.nc'
... fire_emis_specifier = 'bc_a1 = BC', 'pom_a1 = 1.4*OC', 'pom_a2 = A*B*C', 'SO2 = SO2'
... /
... '''
>>> _parse_namelists(teststr.splitlines(), 'foo')
OrderedDict([('fire_emis_nl', OrderedDict([('fire_emis_factors_file', "'fire_emis_factors_c140116.nc'"), ('fire_emis_specifier', ["'bc_a1 = BC'", "'pom_a1 = 1.4*OC'", "'pom_a2 = A*B*C'", "'SO2 = SO2'"])]))])
{'fire_emis_nl': {'fire_emis_factors_file': "'fire_emis_factors_c140116.nc'", 'fire_emis_specifier': ["'bc_a1 = BC'", "'pom_a1 = 1.4*OC'", "'pom_a2 = A*B*C'", "'SO2 = SO2'"]}}
>>> _parse_namelists('blah', 'foo') # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
Expand Down Expand Up @@ -196,27 +194,27 @@ def _parse_namelists(namelist_lines, filename):
... val = 2, 2*13
... /'''
>>> _parse_namelists(teststr.splitlines(), 'foo')
OrderedDict([('nml', OrderedDict([('val', ['2', '13', '13'])]))])
{'nml': {'val': ['2', '13', '13']}}
>>> teststr = '''&nml
... val = 2 2 3
... /'''
>>> _parse_namelists(teststr.splitlines(), 'foo')
OrderedDict([('nml', OrderedDict([('val', ['2', '2', '3'])]))])
{'nml': {'val': ['2', '2', '3']}}
>>> teststr = '''&nml
... val = 'a brown cow' 'a red hen'
... /'''
>>> _parse_namelists(teststr.splitlines(), 'foo')
OrderedDict([('nml', OrderedDict([('val', ["'a brown cow'", "'a red hen'"])]))])
{'nml': {'val': ["'a brown cow'", "'a red hen'"]}}
"""

comment_re = re.compile(r"^[#!]")
namelist_re = re.compile(r"^&(\S+)$")
name_re = re.compile(r"^([^\s=']+)\s*=\s*(.+)$")
rcline_re = re.compile(r"^([^&\s':]+)\s*:\s*(.+)$")

rv = OrderedDict()
rv = {}
current_namelist = None
multiline_variable = None # (name, value)
for line in namelist_lines:
Expand All @@ -238,7 +236,7 @@ def _parse_namelists(namelist_lines, filename):
logger.debug(" Parsing variable '{}' with data '{}'".format(name, value))

if "seq_maps.rc" not in rv:
rv["seq_maps.rc"] = OrderedDict()
rv["seq_maps.rc"] = {}

expect(
name not in rv["seq_maps.rc"],
Expand All @@ -261,7 +259,7 @@ def _parse_namelists(namelist_lines, filename):
# to signify this event
if namelist_re.match(line) is None:
expect(
rv != OrderedDict(),
rv != {},
"File '{}' does not appear to be a namelist file, skipping".format(
filename
),
Expand All @@ -281,7 +279,7 @@ def _parse_namelists(namelist_lines, filename):
),
)

rv[current_namelist] = OrderedDict()
rv[current_namelist] = {}

logger.debug(" Starting namelist '{}'".format(current_namelist))

Expand Down Expand Up @@ -342,17 +340,17 @@ def _parse_namelists(namelist_lines, filename):
real_value = _interpret_value(line, filename)
if type(current_value) is list:
expect(
type(real_value) is not OrderedDict,
type(real_value) is not dict,
"In file '{}', multiline list variable '{}' had dict entries".format(
filename, multiline_variable[0]
),
)
real_value = real_value if type(real_value) is list else [real_value]
current_value.extend(real_value)

elif type(current_value) is OrderedDict:
elif type(current_value) is dict:
expect(
type(real_value) is OrderedDict,
type(real_value) is dict,
"In file '{}', multiline dict variable '{}' had non-dict entries".format(
filename, multiline_variable[0]
),
Expand Down Expand Up @@ -459,7 +457,7 @@ def _compare_values(name, gold_value, comp_value, case):
name, comp_value_list_item
)

elif type(gold_value) is OrderedDict:
elif type(gold_value) is dict:
for key, gold_value_dict_item in gold_value.items():
if key in comp_value:
comments += _compare_values(
Expand Down Expand Up @@ -639,7 +637,7 @@ def _compare_namelists(gold_namelists, comp_namelists, case):
COMP: csw_specifier dict item DMS = 1.0*other.nc
<BLANKLINE>
"""
different_namelists = OrderedDict()
different_namelists = {}
for namelist, gold_names in gold_namelists.items():
if namelist not in comp_namelists:
different_namelists[namelist] = ["Missing namelist: {}\n".format(namelist)]
Expand Down

0 comments on commit dd40400

Please sign in to comment.