コード例 #1
0
    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')
コード例 #2
0
    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')
コード例 #3
0
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()
コード例 #4
0
 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
コード例 #5
0
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
コード例 #6
0
    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)
コード例 #7
0
    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)
コード例 #8
0
 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
コード例 #9
0
 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
コード例 #10
0
    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')
コード例 #11
0
    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')
コード例 #12
0
    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')
コード例 #13
0
    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')
コード例 #14
0
 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'])
コード例 #15
0
 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'])