def test_namespace_hash(self): test = util.NameSpace(name='A', B='C') test2 = test test3 = test.copy() test4 = test.copy() test4.name = 'not_the_same' test5 = util.NameSpace(name='A', B='C') self.assertEqual(test, test2) self.assertEqual(test, test3) self.assertNotEqual(test, test4) self.assertEqual(test, test5) set_test = set([test, test2, test3, test4, test5]) self.assertEqual(len(set_test), 2) self.assertIn(test, set_test) self.assertIn(test4, set_test)
def pod_paths(self, pod, case): d = util.NameSpace() d.POD_CODE_DIR = os.path.join(self.CODE_ROOT, 'diagnostics', pod.name) d.POD_OBS_DATA = os.path.join(self.OBS_DATA_ROOT, pod.name) d.POD_WK_DIR = os.path.join(case.MODEL_WK_DIR, pod.name) d.POD_OUT_DIR = os.path.join(case.MODEL_OUT_DIR, pod.name) return d
def test_namespace_tofrom_dict(self): test = util.NameSpace(name='A', B='C') test2 = test.toDict() self.assertEqual(test2['name'], 'A') self.assertEqual(test2['B'], 'C') test3 = util.NameSpace.fromDict(test2) self.assertEqual(test3.name, 'A') self.assertEqual(test3.B, 'C')
def test_namespace_basic(self): test = util.NameSpace(name='A', B='C') self.assertEqual(test.name, 'A') self.assertEqual(test.B, 'C') with self.assertRaises(AttributeError): _ = test.D test.B = 'D' self.assertEqual(test.B, 'D')
def test_namespace_copy(self): test = util.NameSpace(name='A', B='C') test2 = test.copy() self.assertEqual(test2.name, 'A') self.assertEqual(test2.B, 'C') test2.B = 'D' self.assertEqual(test.B, 'C') self.assertEqual(test2.B, 'D')
def _init_path(self, key, d, env=None): if self._unittest: # use in unit testing only return 'TEST_' + key else: # need to check existence in case we're being called directly if not d.get(key, False): _log.fatal(f"Error: {key} not initialized.") util.exit_handler(code=1) return util.resolve_path(util.from_iter(d[key]), root_path=self.CODE_ROOT, env=env, log=_log) def model_paths(self, case, overwrite=False): d = util.NameSpace() if isinstance(case, dict): name = case['CASENAME'] yr1 = case['FIRSTYR'] yr2 = case['LASTYR'] else: name = case.name yr1 = case.attrs.date_range.start.format(precision=1) yr2 = case.attrs.date_range.end.format(precision=1) case_wk_dir = 'MDTF_{}_{}_{}'.format(name, yr1, yr2) d.MODEL_DATA_DIR = os.path.join(self.MODEL_DATA_ROOT, name) d.MODEL_WK_DIR = os.path.join(self.WORKING_DIR, case_wk_dir) d.MODEL_OUT_DIR = os.path.join(self.OUTPUT_DIR, case_wk_dir) if not overwrite: # bump both WK_DIR and OUT_DIR to same version because name of # former may be preserved when we copy to latter, depending on
def test_namespace_dict_ops(self): test = util.NameSpace(name='A', B='C') self.assertIn('B', test) self.assertNotIn('D', test)