Exemplo n.º 1
0
    def run(self, *parameters):
        """Execute our program.

        Returns None on failure and sets exception property to the error type.
        """
        if not self.compile():
            return None
        try:
            result = geneticlua.evaluate(*parameters)
            # Consider NaN results an error
            # Example of Lua NaN: x = x % x where x == 0
            if math.isnan(result):
                result = None
            # and consider infinite results an error
            # Example of Lua Inf: x = x / 0 where x == 0
            elif math.isinf(result):
                result = None
        except geneticlua.ExecutionError as Ex:
            # Some sort of Lua runtime error
            return None
        except geneticlua.TimeoutError as Ex:
            # Lua code was killed due to using too many instructions
            self.timeout = True
            return None
        return result
Exemplo n.º 2
0
 def test_execute_success(self):
     result = geneticlua.compile('function evaluate(x)\nreturn x*x\nend')
     self.assertEqual(result, True)
     result = geneticlua.evaluate(5)
     self.assertEqual(result, 25)