def test_set_get_first_level(self): """Test setting, getting sub-state at the lowest level """ with substate_context('test'): state['a'] = 123 self.assertEqual(state['a'], 123) self.assertEqual(state['test.a'], 123) self.assertRaises(KeyError, state.__getitem__, 'a')
def _replicate_single_thread(function, no_replicates, subkey_pattern=SUBSTATE_KEY_PATTERN): """Replicate the experiment defined by the function """ for i in range(no_replicates): with substate_context(subkey_pattern % i): log.debug("Running " + subkey_pattern % i) function()
def __call__(self): with substate_context(self.context): self.ready_queue.put(True) log.debug("Running " + self.context) try: result = self.target() except Exception as err: log.fatal("Error in sub-process: %s", traceback.format_exc()) raise err return result
def collect_results(key, subkey_pattern=SUBSTATE_KEY_PATTERN, no_replicates=None): """Collect the results of a replicated experiment in a list """ result = [] no_replicates = no_replicates or int(conf['pyexperiment.n_replicates']) for i in range(no_replicates): with substate_context(subkey_pattern % i): result.append(state[key]) return result
def test_get_section(self): """Test getting a section of the state """ with substate_context('test'): state['a.a'] = 12 state['a.b'] = 13 self.assertIn('a', state) self.assertIn('a.a', state) self.assertIn('a.b', state) self.assertEqual(state['a.a'], 12) self.assertEqual(state['a.b'], 13)
def test_iterate(self): """Test iterating over sub state """ with substate_context('test'): state['a'] = 1 state['b'] = 2 for elem in state: if elem == 'a': self.assertEqual(state[elem], 1) elif elem == 'b': self.assertEqual(state[elem], 2) else: assert False
def test_set_get_higher_levels(self): """Test setting, getting sub-state at the higher levels """ with substate_context('test'): state['a.b'] = 123 state['c.d.e'] = 345 self.assertEqual(state['a.b'], 123) self.assertEqual(state['c.d.e'], 345) self.assertEqual(state['test.a.b'], 123) self.assertEqual(state['test.c.d.e'], 345) self.assertRaises(KeyError, state.__getitem__, 'a.b') self.assertRaises(KeyError, state.__getitem__, 'c.d.e')
def test_global_state(self): """Test setting, getting global state in sub-state context """ with substate_context('test'): state['a.b'] = 123 state['c.d.e'] = 345 state['__foo'] = 42 state['__bar.foo'] = 43 self.assertEqual(state['a.b'], 123) self.assertEqual(state['c.d.e'], 345) self.assertEqual(state['__foo'], 42) self.assertEqual(state['__bar.foo'], 43) self.assertEqual(state['test.a.b'], 123) self.assertEqual(state['test.c.d.e'], 345) self.assertEqual(state['__foo'], 42) self.assertEqual(state['__bar.foo'], 43) self.assertRaises(KeyError, state.__getitem__, 'a.b') self.assertRaises(KeyError, state.__getitem__, 'c.d.e')
def test_get_nonexisting(self): """Test getting an item of the state that does not exist """ with substate_context('test'): self.assertRaises(KeyError, lambda: state['bla'])