New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
Changeset 4126 – NEMO

Changeset 4126


Ignore:
Timestamp:
2013-10-24T18:49:17+02:00 (11 years ago)
Author:
andrewryan
Message:

added latest version of OOO namelist editor and test suite

Location:
branches/2013/dev_r3987_UKMO4_OBS/NEMOGCM/TOOLS/OBSTOOLS/OOO/ooo
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/2013/dev_r3987_UKMO4_OBS/NEMOGCM/TOOLS/OBSTOOLS/OOO/ooo/nml.py

    r4122 r4126  
    153153""" 
    154154__version__ = "0.1.0" 
    155  
    156155import re 
    157156 
     
    270269        data = sanitize(data) 
    271270    for k, v in data.iteritems(): 
    272         pat = r"(%s\s+=\s+).+?(\s*[!\n])" % (k,) 
     271        pat = r"(%s\s*=\s*).+?(\s*[!\n])" % (k,) 
    273272        repl = r"\g<1>%s\g<2>" % (v,) 
    274273        text = re.sub(pat, repl, text) 
     
    342341    """ 
    343342    if isinstance(data, list): 
    344         # Conversion function 
    345         def convert(word): 
    346             if isinstance(word, str): 
    347                 result = "'%s'" % (word,) 
    348             else: 
    349                 result = str(word) 
    350             return result 
    351         text = " ".join([convert(item) for item in data]) 
    352     elif isinstance(data, bool): 
    353         if data: 
    354             text = ".TRUE." 
     343        if same_type(data): 
     344            delim = " " 
    355345        else: 
    356             text = ".FALSE." 
     346            delim = ", " 
     347        text = delim.join([convert(item) for item in data]) 
    357348    else: 
    358         text = str(data) 
     349        text = convert(data) 
    359350    return text 
     351 
     352def numeric(word): 
     353    # Tests input string is numeric data 
     354    parts = word.split(" ") 
     355    try: 
     356        map(float, parts) 
     357        flag = True 
     358    except ValueError: 
     359        flag = False 
     360    return flag 
     361 
     362def logical(word): 
     363    # Tests input string is numeric data 
     364    if word.upper() in [".FALSE.", ".TRUE."]: 
     365        flag = True 
     366    else: 
     367        flag = False 
     368    return flag 
     369 
     370def listed(word): 
     371    # Tests input string is not a list 
     372    if ("," in word) or (" " in word): 
     373        flag = True 
     374    else: 
     375        flag = False 
     376    return flag 
     377 
     378def convert(word): 
     379    # Conversion function 
     380    if isinstance(word, str): 
     381        if (quoted(word) or numeric(word)  
     382            or logical(word) or listed(word)): 
     383            result = "%s" % (word,) 
     384        else: 
     385            result = "'%s'" % (word,) 
     386    elif isinstance(word, bool): 
     387        if word: 
     388            result = ".TRUE." 
     389        else: 
     390            result = ".FALSE." 
     391    else: 
     392        result = str(word) 
     393    return result 
     394 
     395def quoted(word): 
     396    # Checks if string begins/ends with quotation marks 
     397    if (word.startswith("'") and word.endswith("'")): 
     398        flag = True 
     399    elif (word.startswith('"') and word.endswith('"')): 
     400        flag = True 
     401    else:  
     402        flag = False 
     403    return flag 
     404 
     405def same_type(data): 
     406    # True if all entries are the same type 
     407    types = map(type, data) 
     408    if len(set(types)) == 1: 
     409        flag = True 
     410    else: 
     411        flag = False 
     412    return flag 
    360413 
    361414def sanitize(data): 
  • branches/2013/dev_r3987_UKMO4_OBS/NEMOGCM/TOOLS/OBSTOOLS/OOO/ooo/test_nml.py

    r4122 r4126  
    55def version(vn): 
    66    # Converts module version string to int tuple as PEP-440 
    7     return tuple(map(int, nml.__version__.split("."))) 
     7    return tuple(map(int, vn.split("."))) 
    88 
    99HEADER = """ 
     
    1616LOGIC = " ln_test = .TRUE. ! Comment\n" 
    1717NUMERIC = " nn_off_idx = 1 2 3\n" 
    18 FORD = " nn_off_idx=1 2 3\n" 
     18NOSPACE = " nn_off_idx=1 2 3\n" 
    1919NEGATIVE = " nn_off_idx = -1 -2 -3\n" 
    2020LIST = " off_files = 'a.nc' 'b.nc' 'c.nc' ! Comment\n" 
     
    2828        self.logic_text = "\n".join([HEADER, LOGIC, FOOTER]) 
    2929        self.numeric_text = "\n".join([HEADER, NUMERIC, FOOTER]) 
    30         self.ford_text = "\n".join([HEADER, FORD, FOOTER]) 
     30        self.nospace_text = "\n".join([HEADER, NOSPACE, FOOTER]) 
    3131        self.neg_numeric_text = "\n".join([HEADER, NEGATIVE, FOOTER]) 
    3232        self.list_text = "\n".join([HEADER, LIST, FOOTER]) 
     
    6363        self.assertDictEqual(data, truth) 
    6464 
    65     def test_ford_data(self): 
    66         data = nml.variables(self.ford_text) 
     65    def test_nospace_data(self): 
     66        data = nml.variables(self.nospace_text) 
    6767        truth = {"nn_off_idx": "1 2 3"} 
    6868        self.assertDictEqual(data, truth) 
     
    8484 
    8585    def test_replace_variable_comment(self): 
    86         FIXTURE = " x = y ! comment \n" 
    87         RESULT = " x = z ! comment \n" 
     86        FIXTURE = " x = 'y' ! comment \n" 
     87        RESULT = " x = 'z' ! comment \n" 
    8888        text = nml.replace(FIXTURE, {"x": "z"}) 
    8989        self.assertEqual(text, RESULT) 
    9090 
    9191    def test_replace_variable_no_comment(self): 
    92         FIXTURE = " x = y \n" 
    93         RESULT = " x = z \n" 
     92        FIXTURE = " x = 'y' \n" 
     93        RESULT = " x = 'z' \n" 
     94        text = nml.replace(FIXTURE, {"x": "z"}) 
     95        self.assertEqual(text, RESULT) 
     96 
     97    def test_replace_variable_no_space(self): 
     98        FIXTURE = " x='y' \n" 
     99        RESULT = " x='z' \n" 
     100        text = nml.replace(FIXTURE, {"x": "z"}) 
     101        self.assertEqual(text, RESULT) 
     102 
     103    def test_replace_variable_no_space_comment(self): 
     104        FIXTURE = " x='y' ! comment \n" 
     105        RESULT = " x='z' ! comment \n" 
    94106        text = nml.replace(FIXTURE, {"x": "z"}) 
    95107        self.assertEqual(text, RESULT) 
     
    160172        self.char = "foo@bar.com" 
    161173        self.num = 10 
     174        self.mixed_list = ["foo.nc", -1, True] 
     175 
     176    def test_should_format_mixed_list(self): 
     177        data = nml.tostring(self.mixed_list) 
     178        result = "'foo.nc', -1, .TRUE." 
     179        self.assertEqual(data, result) 
    162180 
    163181    def test_should_format_numeric_list(self): 
     
    173191    def test_should_format_strings(self): 
    174192        data = nml.tostring(self.char) 
    175         result = self.char 
     193        result = "'%s'" % (self.char,) 
    176194        self.assertEqual(data, result) 
    177195 
     
    180198        result = str(self.num) 
    181199        self.assertEqual(data, result) 
     200 
     201    def test_should_not_format_numeric_string(self): 
     202        input = "3.14159" 
     203        self.assertEqual(nml.tostring(input), input) 
    182204 
    183205    def test_should_format_logicals(self): 
     
    186208        self.assertEqual(data.upper(), result) 
    187209 
     210    def test_should_not_format_string_of_list_data(self): 
     211        for input in ["1 2 3", "1, 2, 3", ".TRUE. .FALSE."]: 
     212            case = nml.tostring(input) 
     213            self.assertEqual(case, input) 
     214 
    188215class TestUpdateNamelist(unittest.TestCase): 
    189216    def setUp(self): 
     
    194221        self.single = """ 
    195222&namone 
    196    x = y 
     223   x = 'y' 
    197224/ 
    198225""" 
    199226        self.single_update = """ 
    200227&namone 
    201    x = z 
     228   x = 'z' 
    202229/ 
    203230""" 
    204231 
    205232    def test_should_append_new_variable_to_namelist(self): 
    206         trial = nml.update("namone", self.empty, {"x": "y"}) 
     233        trial = nml.update("namone", self.empty, {"x": "'y'"}) 
    207234        self.assertEqual(trial, self.single) 
    208235 
    209236    def test_should_update_existing_variables(self): 
    210         trial = nml.update("namone", self.single, {"x": "z"}) 
     237        trial = nml.update("namone", self.single, {"x": "'z'"}) 
    211238        self.assertEqual(trial, self.single_update) 
    212239 
Note: See TracChangeset for help on using the changeset viewer.