def convolute(ast_g, ast_f): 'Return an AST that represents g(f(args))' # TODO: fix up the ast.Calls to use lambda_call if possible # Sanity checks. For example, g can have only one input argument (e.g. f's result) if (not lambda_test(ast_g)) or (not lambda_test(ast_f)): raise BaseException("Only lambdas in Selects!") # Combine the lambdas into a single call by calling g with f as an argument l_g = copy.deepcopy(lambda_unwrap(ast_g)) l_f = copy.deepcopy(lambda_unwrap(ast_f)) x = arg_name() f_arg = ast.Name(x, ast.Load()) call_g = ast.Call(l_g, [ast.Call(l_f, [f_arg], [])], []) # TODO: Rewrite with lambda_build args = ast.arguments(args=[ast.arg(arg=x)]) call_g_lambda = ast.Lambda(args=args, body=call_g) # Build a new call to nest the functions return call_g_lambda
def test_lambda_test_expression(): assert lambda_test(ast.parse("x")) is False
def test_lambda_test_raw_lambda(): rl = cast(ast.Expr, ast.parse("lambda x: x").body[0]).value assert lambda_test(rl) is True
def test_lambda_test_lambda_module(): assert lambda_test(ast.parse("lambda x: x")) is True
def test_lambda_simple_ast_expr(): assert lambda_test(ast.Not()) is False
def test_lambda_assure_lambda(): try: lambda_test(ast.parse("lambda x : x+1")) assert False except Exception: pass
def test_lambda_assure_expression(): try: lambda_test(ast.parse("x")) assert False except Exception: pass
def test_lambda_test_raw_lambda(): rl = cast(ast.Expr, ast.parse('lambda x: x').body[0]).value assert lambda_test(rl) == True
def test_lambda_test_lambda_module(): assert lambda_test(ast.parse('lambda x: x')) == True