예제 #1
0
    def test_pythagoras(self):
        from math import sqrt

        X = Symbol('X')
        Y = Symbol('Y')

        expr = sym_call(sqrt, X**2 + Y**2)
        func = to_callable(expr)

        self.assertEqual(func(X=3, Y=4), 5)
예제 #2
0
    def test_pythagoras(self):
        from math import sqrt

        X = Symbol('X')
        Y = Symbol('Y')

        expr = sym_call(sqrt, X ** 2 + Y ** 2)
        func = to_callable(expr)

        self.assertEqual(func(X=3, Y=4), 5)
예제 #3
0
    def test_to_callable_from_nonsymbolic_callable(self):
        func = mock.Mock(return_value='return value')
        del func._eval  # So it doesn't pretend to be symbolic

        test_callable = to_callable(func)

        # Ensure running to_callable does not call the function
        self.assertFalse(func.called)

        result = test_callable('arg1', 'arg2', kwarg_name='kwarg value')

        func.assert_called_once_with('arg1', 'arg2', kwarg_name='kwarg value')
        self.assertEqual(result, 'return value')
예제 #4
0
    def test_to_callable_from_nonsymbolic_callable(self):
        func = mock.Mock(return_value='return value')
        del func._eval  # So it doesn't pretend to be symbolic

        test_callable = to_callable(func)

        # Ensure running to_callable does not call the function
        self.assertFalse(func.called)

        result = test_callable('arg1', 'arg2', kwarg_name='kwarg value')

        func.assert_called_once_with('arg1', 'arg2', kwarg_name='kwarg value')
        self.assertEqual(result, 'return value')
예제 #5
0
    def test_to_callable_from_symbolic(self):
        mock_expr = mock.Mock()
        mock_expr._eval.return_value = 'eval return value'

        test_callable = to_callable(mock_expr)

        # Ensure running to_callable does not evaluate the expression
        self.assertFalse(mock_expr._eval.called)

        result = test_callable('arg1', 'arg2', kwarg_name='kwarg value')

        mock_expr._eval.assert_called_once_with(
            {0: 'arg1', 1: 'arg2', 'kwarg_name': 'kwarg value'})
        self.assertEqual(result, 'eval return value')
예제 #6
0
    def test_to_callable_from_symbolic(self):
        mock_expr = mock.Mock()
        mock_expr._eval.return_value = 'eval return value'

        test_callable = to_callable(mock_expr)

        # Ensure running to_callable does not evaluate the expression
        self.assertFalse(mock_expr._eval.called)

        result = test_callable('arg1', 'arg2', kwarg_name='kwarg value')

        mock_expr._eval.assert_called_once_with({
            0: 'arg1',
            1: 'arg2',
            'kwarg_name': 'kwarg value'
        })
        self.assertEqual(result, 'eval return value')
예제 #7
0
 def test_to_callable_from_nonsymbolic_noncallable(self):
     test_callable = to_callable('nonsymbolic')
     self.assertEqual(
         test_callable('arg1', 'arg2', kwarg_name='kwarg value'),
         'nonsymbolic')
예제 #8
0
 def test_to_callable_from_nonsymbolic_noncallable(self):
     test_callable = to_callable('nonsymbolic')
     self.assertEqual(
         test_callable('arg1', 'arg2', kwarg_name='kwarg value'),
         'nonsymbolic')