def test_simple(self): def f(): def g(): return 0 return g s = decompile.get_source(f()) exec s self.assertEquals(g(), 0)
def test_function_with_lambda(self): def f(): def g(x=lambda: 0): return x() return g s = decompile.get_source(f()) exec s self.assertEquals(g(), 0)
def test_space_before_colon(self): def f() : def g(): return 0 return g s = decompile.get_source(f()) exec s self.assertEquals(g(), 0)
def test_embedded_lambda_token(self): def f(): def g(x="lambda: 0"): return "lambda: 1" return g s = decompile.get_source(f()) exec s self.assertEquals(g(), "lambda: 1")
def test_args_kwargs(self): def f(): def g(*args, **kwargs): return args[0] + kwargs['foo'] return g s = decompile.get_source(f()) exec s self.assertEquals(g(3, foo=4), 7)
def test_unpacking(self): def fff((x, y)): return x + y s = decompile.get_source(fff) exec s.replace('fff', 'ggg') self.assertEquals(ggg((1, 2)), 3)
def test_variable(self): def f(): def g(): return 0 return g h = f() s = decompile.get_source(h) exec s self.assertEquals(g(), 0)
def test_parameters(self): def f(): def g(x, y, z=1): return x + y + z return g s = decompile.get_source(f()) exec s self.assertEquals(g(3, 4, 5), 12) self.assertEquals(g(3, 4), 8)
def test_multiple_lines(self): def f() : def g(): return 0 return g s = decompile.get_source(f()) exec s self.assertEquals(g(), 0)
def test_line_continuation(self): def f() : def g(): return \ 0 return g s = decompile.get_source(f()) exec s self.assertEquals(g(), 0)
def test_commants(self): def f() : def g(): # foo return \ 0 # bar return g s = decompile.get_source(f()) exec s self.assertEquals(g(), 0)
def test_simple(self): def f(): def g(): return 0 return g s = decompile.get_source(f()) exec s self.assertEquals(g(), 0)
def test_embedded_lambda_token(self): def f(): def g(x="lambda: 0"): return "lambda: 1" return g s = decompile.get_source(f()) exec s self.assertEquals(g(), "lambda: 1")
def test_space_before_colon(self): def f(): def g(): return 0 return g s = decompile.get_source(f()) exec s self.assertEquals(g(), 0)
def test_args_kwargs(self): def f(): def g(*args, **kwargs): return args[0] + kwargs['foo'] return g s = decompile.get_source(f()) exec s self.assertEquals(g(3, foo=4), 7)
def test_function_with_lambda(self): def f(): def g(x=lambda: 0): return x() return g s = decompile.get_source(f()) exec s self.assertEquals(g(), 0)
def test_line_continuation(self): def f(): def g(): return \ 0 return g s = decompile.get_source(f()) exec s self.assertEquals(g(), 0)
def test_commants(self): def f(): def g(): # foo return \ 0 # bar return g s = decompile.get_source(f()) exec s self.assertEquals(g(), 0)
def test_variable(self): def f(): def g(): return 0 return g h = f() s = decompile.get_source(h) exec s self.assertEquals(g(), 0)
def test_parameters(self): def f(): def g(x, y, z=1): return x + y + z return g s = decompile.get_source(f()) exec s self.assertEquals(g(3, 4, 5), 12) self.assertEquals(g(3, 4), 8)
def test_multiple_lines(self): def f(): def g(): return 0 return g s = decompile.get_source(f()) exec s self.assertEquals(g(), 0)
def convert(source_or_ast_or_callable, schema, udfs=None): """ Convert a Python function into its RACO equivalent :param source_or_ast_or_callable: Source string, callable, or AST node :param schema: List of schema for the input parameter(s) :param udfs: List of (name, arity) pairs of UDFs :return: RACO expression representing the source, callable, or AST node """ if isinstance(source_or_ast_or_callable, basestring): try: return convert(ast.parse(source_or_ast_or_callable), schema, udfs) except SyntaxError as e: raise PythonSyntaxException(e.msg, e.lineno, e.offset) elif callable(source_or_ast_or_callable): return convert(get_source(source_or_ast_or_callable), schema, udfs) elif isinstance(source_or_ast_or_callable, ast.AST): return visitor.ExpressionVisitor( schema or [], udfs or []).visit(source_or_ast_or_callable) or None else: raise PythonConvertException( 'Argument was not a source string, callable, or AST node')
def test_tuple(self): t = (lambda: 0), 5 s = decompile.get_source(t[0]) self.assertEquals(eval(s)(), t[0]())
def test_multiple_lambdas(self): t = lambda: 1, lambda: 2, lambda: 3 for f in t: s = decompile.get_source(f) self.assertEquals(eval(s)(), f())
def test_tuple2(self): f = lambda: (0, 5) s = decompile.get_source(f) self.assertEquals(eval(s)(), f())
def test_parameters(self): f = lambda x: x s = decompile.get_source(f) self.assertEquals(eval(s)(5), f(5))
def test_embedded_lambda_token(self): f = lambda: "lambda: 0" s = decompile.get_source(f) self.assertEquals(eval(s)(), f())
def test_parameters(self): f = lambda x: x s = decompile.get_source(f) self.assertEquals(eval(s)(5), f(5))
def test_member(self): s = decompile.get_source(self._member) exec s self.assertEquals(_member(self), 1)
def test_tuple(self): t = (lambda: 0), 5 s = decompile.get_source(t[0]) self.assertEquals(eval(s)(), t[0]())
def test_multiple_lambdas(self): t = lambda: 1, lambda: 2, lambda: 3 for f in t: s = decompile.get_source(f) self.assertEquals(eval(s)(), f())
def test_simple(self): s = decompile.get_source(lambda: 0) self.assertEquals(eval(s)(), 0)
def test_variable(self): f = lambda: 0 s = decompile.get_source(f) self.assertEquals(eval(s)(), f())
def test_space_after_colon(self): s = decompile.get_source(lambda: 0) self.assertEquals(eval(s)(), 0)
def test_newline2(self): f = lambda: \ 0 s = decompile.get_source(f) self.assertEquals(eval(s)(), f())
def test_classmethod(self): s = decompile.get_source(self._classmethod) exec s self.assertEquals(_classmethod(self), 1)
def test_staticmethod(self): s = decompile.get_source(self._staticmethod) exec s self.assertEquals(_staticmethod(3), 4)
def test_args_kwargs(self): f = lambda *args, **kwargs: args[0] + kwargs['foo'] s = decompile.get_source(f) self.assertEqual(eval(s)(5, foo=6), f(5, foo=6))
def test_variable(self): f = lambda: 0 s = decompile.get_source(f) self.assertEquals(eval(s)(), f())
def test_args_kwargs(self): f = lambda *args, **kwargs: args[0] + kwargs['foo'] s = decompile.get_source(f) self.assertEqual(eval(s)(5, foo=6), f(5, foo=6))
def test_simple(self): s = decompile.get_source(lambda: 0) self.assertEquals(eval(s)(), 0)
def test_multiple_parameters(self): f = lambda x, y: x + y s = decompile.get_source(f) self.assertEquals(eval(s)(5, 6), f(5, 6))
def test_space_after_colon(self): s = decompile.get_source(lambda :0) self.assertEquals(eval(s)(), 0)
def test_member(self): s = decompile.get_source(self._member) exec s self.assertEquals(_member(self), 1)
def test_newline2(self): f = lambda: \ 0 s = decompile.get_source(f) self.assertEquals(eval(s)(), f())
def test_classmethod(self): s = decompile.get_source(self._classmethod) exec s self.assertEquals(_classmethod(self), 1)
def test_tuple2(self): f = lambda : (0, 5) s = decompile.get_source(f) self.assertEquals(eval(s)(), f())
def test_staticmethod(self): s = decompile.get_source(self._staticmethod) exec s self.assertEquals(_staticmethod(3), 4)
def test_embedded_lambda_token(self): f = lambda: "lambda: 0" s = decompile.get_source(f) self.assertEquals(eval(s)(), f())
def test_unpacking(self): def fff((x, y)): return x + y s = decompile.get_source(fff) exec s.replace('fff', 'ggg') self.assertEquals(ggg((1, 2)), 3)
def test_multiple_parameters(self): f = lambda x, y: x + y s = decompile.get_source(f) self.assertEquals(eval(s)(5, 6), f(5, 6))
def test_unpacking(self): """ Unpacking is not currently supported """ f = lambda (x, y): x + y self.assertRaises(PythonConvertException, lambda: decompile.get_source(f))
def test_unpacking(self): """ Unpacking is not currently supported """ f = lambda (x, y): x + y self.assertRaises(PythonConvertException, lambda: decompile.get_source(f))
def _get_source(body): try: return get_source(body) except PythonConvertException: return None