def test_check_tree(self): tc = scicfg.SciConfig() tc._branch('a') tc.a._isinstance('c', (int, float)) def validate_b(value): return 0 <= value <= 256 tc.a._validate('b', validate_b) t2 = scicfg.SciConfig() t2._branch('a') t2.a.c = 2.0 t2.a.b = 50 t2._check(tc) t2.a.b = 350 with self.assertRaises(TypeError): t2._check(tc) t2.a.b = 250 t2.a.c = '23' with self.assertRaises(TypeError): t2._check(tc) t2.a.c = 2.0 t2.a.b = 50 t2._update(tc) t2._check() t2.a.d = 50 with self.assertRaises(TypeError): t2._strict(True)
def test_update_frozen(self): t = scicfg.SciConfig() t.b = 2 t._branch('a') t2 = scicfg.SciConfig() t2.b = 3 t2._freeze(True) with self.assertRaises(ValueError): t2._update(t)
def test_update(self): t = scicfg.SciConfig() t._isinstance('f', int) t2 = scicfg.SciConfig(strict=True) with self.assertRaises(TypeError): t2.f = 2 t2._update(t, descriptions=False) with self.assertRaises(TypeError): t2.f = 2 t2._update(t) t2.f = 2
def test_check_struct(self): tc = scicfg.SciConfig() tc._branch('a') t2 = scicfg.SciConfig() t2._branch('a') t2._check(tc, struct=True) t2._branch('b') with self.assertRaises(TypeError): t2._check(tc, struct=True) with self.assertRaises(TypeError): tc._check(t2, struct=True)
def test_update_structfrozen(self): t = scicfg.SciConfig() t.b = 2 t2 = scicfg.SciConfig() t2.b = 3 t2._freeze_struct(True) t2._update(t) self.assertEqual(t.b, 2) t._branch('a') with self.assertRaises(ValueError): t2._update(t)
def test_get(self): tc = scicfg.SciConfig() tc._branch('a.b') tc.a.b.c = 1 self.assertEqual(tc._get('a.b.c', 2), 1) self.assertEqual(tc._get('a.b.d', 2), 2)
def test_lines(self): tc = scicfg.SciConfig() tc._branch('a') tc.a.b = 1 tc.a.d = [1, 2, 3] self.assertTrue( tc.__str__() in ['a.b=1\na.d=[1, 2, 3]', 'a.d=[1, 2, 3]\na.b=1'])
def __init__(self, cfg, **kwargs): """ You should define m_channels and s_channels, motor and sensory channels for the environment, here, as a list of Channel instances. """ if isinstance(cfg, dict): cfg = scicfg.SciConfig(cfg) self.cfg = cfg self.cfg._update(self.defcfg, overwrite=False)
def test_fromkeys1(self): tc = scicfg.SciConfig() t = tc._fromkeys(('a', 'c.d'), 3) self.assertEqual(set(t._items()),set((('a', 3), ('c.d', 3)))) with self.assertRaises(ValueError): t = scicfg.SciConfig._fromkeys(('a', 'a.d'))
def test_unfreeze(self): cfg = scicfg.SciConfig() cfg['a.b'] = 3 cfg['a.a.c'] = 3 cfg._freeze(True) cfg2 = cfg._deepcopy() cfg2['b'] = 1
def test_nestitem(self): tc = scicfg.SciConfig() tc['a.b'] = 1 self.assertEqual(tc.a.b, 1) self.assertTrue('a.b' in tc) self.assertTrue('a.c' not in tc)
def test_popitem0(self): tc = scicfg.SciConfig() tc._branch('a') tc.a.b = 1 self.assertEqual(tc._popitem(), ('a.b', 1)) with self.assertRaises(KeyError): tc._popitem()
def test_coverage(self): tc = scicfg.SciConfig() tc._branch('a') with self.assertRaises(KeyError): tc._coverage('a.b') tc.a.b = 1 self.assertEqual(tc._coverage('a.b'), 0) tc.a.b self.assertEqual(tc._coverage('a.b'), 1)
def test_docstring(self): tc = scicfg.SciConfig() tc._branch('a') tc._docstring('a.b', 'a nice descriptive docstring') tc._docstring('a.c', """Another one""") self.assertEqual(tc._docstring('a.b'), 'a nice descriptive docstring') self.assertEqual(tc._docstring('a.c'), 'Another one') self.assertEqual(tc._docstring('a.f'), None)
def test_freeze_struct(self): tc = scicfg.SciConfig() tc._branch('a') tc.a.b = 1 tc._freeze_struct(True) with self.assertRaises(ValueError): tc.a.c = 1 tc.a.b = 2
def test_history(self): tc = scicfg.SciConfig() tc._branch('a') with self.assertRaises(KeyError): tc._history('a.b') tc.a.b = 1 self.assertEqual(tc._history('a.b'), [1]) tc.a.b = '2' self.assertEqual(tc._history('a.b'), [1, '2'])
def test_walk(self): cfg = scicfg.SciConfig() cfg['a.b.c'] = 1 cfg['a.b.d'] = 2 cfg['a.e'] = 3 self.assertEqual( self.listit(cfg._walk()), [[[], ['a'], ''], [[], ['c', 'd'], 'a.b'], [['b'], ['e'], 'a']])
def test_setdefault(self): tc = scicfg.SciConfig() tc._branch('a.b') tc.a.b.c = 1 tc.a._setdefault('b.c', 2) tc.a._setdefault('e.c', 4) self.assertEqual(tc.a.b.c, 1) self.assertEqual(tc.a.e.c, 4)
def test_deepcopy(self): cfg = scicfg.SciConfig() cfg['a.b'] = 3 cfg['a.a.c'] = 3 cfg2 = cfg._copy() cfg2['b'] = 1 with self.assertRaises(KeyError): cfg['b']
def test_kvi(self): tc = scicfg.SciConfig() tc._branch('a') tc.a['b'] = 1 tc.color = 'blue' self.assertEqual({'a.b', 'color'}, set(tc._keys())) self.assertEqual({1, 'blue'}, set(tc._values())) self.assertEqual({('a.b', 1), ('color', 'blue')}, set(tc._items()))
def test_update_dict(self): d = {'b' : 2, 'abc.cde.d': 3, 'abc.f' : 4} t = scicfg.SciConfig() t._update(d, overwrite=True) for key, value in d.items(): self.assertEqual(t[key], value) self.assertEqual(t.abc.cde.d, 3)
def test_nestedbranch(self): tc = scicfg.SciConfig() tc._branch('a.b') tc.a.b.c = 1 self.assertEqual(tc.a.b.c, 1) tc._branch('a.b.defg.hc.i') tc.a.b.defg.hc.i.c = 3 self.assertEqual(tc.a.b.defg.hc.i.c, 3) self.assertEqual(tc.a.b.c, 1)
def test_item(self): tc = scicfg.SciConfig() tc._branch('a') tc.a['b'] = 1 self.assertEqual(tc.a.b, 1) self.assertEqual(tc.a['b'], 1) tc.a.b = 2 self.assertEqual(tc.a.b, 2) self.assertEqual(tc.a['b'], 2)
def test_copy(self): tc = scicfg.SciConfig() tc._branch('a.b') tc.a.b.c = 1 tc['a.b.defg.hc.i'] = 3 t2 = tc._copy() self.assertEqual(tc, t2) t3 = tc._deepcopy() self.assertEqual(tc, t3)
def __init__(self): m_bounds = ((0.0, 1.0), (0.0, 1.0)) self.m_channels = [ envs.Channel(i, mb_i) for i, mb_i in enumerate(m_bounds) ] self.s_channels = [envs.Channel(i) for i in enumerate((2, 3))] self._cfg = scicfg.SciConfig() self._cfg.m_channels = self.m_channels self._cfg.s_channels = self.s_channels self._cfg._freeze(True)
def test_in(self): tc = scicfg.SciConfig() tc._branch('a.b') tc.a.b.c = 1 self.assertTrue('a.b.c' in tc) self.assertTrue(not 'a.b.d' in tc) self.assertTrue('a' in tc) self.assertTrue('a.b' in tc) with self.assertRaises(ValueError): 'a.' not in tc
def test_both(self): tc = scicfg.SciConfig() tc._branch('a') tc.a.b = 1 tc.a.d = [1, 2, 3] filename = tempname() tc._to_file(filename) t2 = scicfg.SciConfig._from_file(filename) self.assertEqual(tc, t2)
def test_pickle(self): t = scicfg.SciConfig() t.a = scicfg.SciConfig() t.a.b = 1 t.a.c = '2' t.a._branch('efg') t.a.efg.h = [2, 3, 4] t['b.farm'] = None s = pickle.dumps(t) t2 = pickle.loads(s) self.assertEqual(t, t2) filename = tempname() with open(filename, 'wb') as f: pickle.dump(t, f) with open(filename, 'rb') as f: t2 = pickle.load(f) self.assertEqual(t, t2)
def test_describe2(self): defcfg = scicfg.SciConfig() defcfg._describe('m_channels', instanceof=collections.Iterable, docstring='Motor channels to generate random order of') defcfg._describe('s_channels', instanceof=collections.Iterable, docstring='Sensory channels to generate random goal from') defcfg._describe('models.fwd', instanceof=str, docstring='The name of the forward model to use') defcfg._describe('models.inv', instanceof=str, docstring='The name of the invserse model to use') defcfg._describe('models.kwargs', instanceof=dict, docstring='optional keyword arguments')
def test_update_dict2(self): d = {'b' : 2, 'abc.cde.d': 3, 'abc.f' : 4} t = scicfg.SciConfig() t.b = [1, 2] t['abc.f'] = None t._update(d, overwrite=False) self.assertEqual(t.b, [1, 2]) self.assertEqual(t.abc.cde.d, 3) self.assertEqual(t.abc.f, None)