Changeset 4126
- Timestamp:
- 2013-10-24T18:49:17+02:00 (11 years ago)
- 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 153 153 """ 154 154 __version__ = "0.1.0" 155 156 155 import re 157 156 … … 270 269 data = sanitize(data) 271 270 for k, v in data.iteritems(): 272 pat = r"(%s\s +=\s+).+?(\s*[!\n])" % (k,)271 pat = r"(%s\s*=\s*).+?(\s*[!\n])" % (k,) 273 272 repl = r"\g<1>%s\g<2>" % (v,) 274 273 text = re.sub(pat, repl, text) … … 342 341 """ 343 342 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 = " " 355 345 else: 356 text = ".FALSE." 346 delim = ", " 347 text = delim.join([convert(item) for item in data]) 357 348 else: 358 text = str(data)349 text = convert(data) 359 350 return text 351 352 def 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 362 def 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 370 def 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 378 def 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 395 def 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 405 def 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 360 413 361 414 def sanitize(data): -
branches/2013/dev_r3987_UKMO4_OBS/NEMOGCM/TOOLS/OBSTOOLS/OOO/ooo/test_nml.py
r4122 r4126 5 5 def version(vn): 6 6 # 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("."))) 8 8 9 9 HEADER = """ … … 16 16 LOGIC = " ln_test = .TRUE. ! Comment\n" 17 17 NUMERIC = " nn_off_idx = 1 2 3\n" 18 FORD= " nn_off_idx=1 2 3\n"18 NOSPACE = " nn_off_idx=1 2 3\n" 19 19 NEGATIVE = " nn_off_idx = -1 -2 -3\n" 20 20 LIST = " off_files = 'a.nc' 'b.nc' 'c.nc' ! Comment\n" … … 28 28 self.logic_text = "\n".join([HEADER, LOGIC, FOOTER]) 29 29 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]) 31 31 self.neg_numeric_text = "\n".join([HEADER, NEGATIVE, FOOTER]) 32 32 self.list_text = "\n".join([HEADER, LIST, FOOTER]) … … 63 63 self.assertDictEqual(data, truth) 64 64 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) 67 67 truth = {"nn_off_idx": "1 2 3"} 68 68 self.assertDictEqual(data, truth) … … 84 84 85 85 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" 88 88 text = nml.replace(FIXTURE, {"x": "z"}) 89 89 self.assertEqual(text, RESULT) 90 90 91 91 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" 94 106 text = nml.replace(FIXTURE, {"x": "z"}) 95 107 self.assertEqual(text, RESULT) … … 160 172 self.char = "foo@bar.com" 161 173 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) 162 180 163 181 def test_should_format_numeric_list(self): … … 173 191 def test_should_format_strings(self): 174 192 data = nml.tostring(self.char) 175 result = self.char193 result = "'%s'" % (self.char,) 176 194 self.assertEqual(data, result) 177 195 … … 180 198 result = str(self.num) 181 199 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) 182 204 183 205 def test_should_format_logicals(self): … … 186 208 self.assertEqual(data.upper(), result) 187 209 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 188 215 class TestUpdateNamelist(unittest.TestCase): 189 216 def setUp(self): … … 194 221 self.single = """ 195 222 &namone 196 x = y223 x = 'y' 197 224 / 198 225 """ 199 226 self.single_update = """ 200 227 &namone 201 x = z228 x = 'z' 202 229 / 203 230 """ 204 231 205 232 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'"}) 207 234 self.assertEqual(trial, self.single) 208 235 209 236 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'"}) 211 238 self.assertEqual(trial, self.single_update) 212 239
Note: See TracChangeset
for help on using the changeset viewer.