Exemplo n.º 1
0
def create_function_nodes(statement_group, function_meta):
    args = ast.arguments(args=[
        ast.Name(id=variable.name) for variable in function_meta["args"]
    ],
                         vararg=None,
                         kwarg=None,
                         defaults=[])
    function_nodes = {}
    function_body = [statement.get_ast() for statement in statement_group]
    if function_meta["has_return_statement"]:
        name = helper.generate_function_name()
        function_nodes[name] = ast.FunctionDef(name=name,
                                               args=args,
                                               body=function_body,
                                               decorator_list=[])
    else:
        for ret_variable in function_meta["returns"]:
            name = helper.generate_function_name()
            return_statement = ast.Return(value=ast.Name(id=ret_variable.name))
            function_nodes[name] = ast.FunctionDef(name=name,
                                                   args=args,
                                                   body=function_body +
                                                   [return_statement],
                                                   decorator_list=[])
    return function_nodes
Exemplo n.º 2
0
 def clone(self):
     new = Function()
     for key in self.has().keys():
         if key in ["id", "name"]:
             continue
         else:
             new[key] = self[key]
     new.base_name = self.name
     new.name = analysis_helper.generate_function_name()
     new.is_cloned = True
     return new
Exemplo n.º 3
0
def create_temp_function(variable, body):
  headers = "\n".join(['library("%s")' % header for header in R_SUPPORTED_LIBRARIES])
  func_name = helper.generate_function_name()
  func_source = """
%s <- function (%s) {
  %s
}
  """ % (func_name, variable, body)
  file_source = "\n\n".join([headers, func_source, ""])
  cache.write_file(TEMP_FUNC_PATH, file_source.encode('utf-8'))
  r_func = functions.get_r_function(TEMP_FUNC_PATH, func_name)
  return r_func
Exemplo n.º 4
0
def create_temp_function_file(variable, body):
    headers = "\n".join(
        ["import numpy", "import pandas", "np, pd = numpy, pandas"])
    file_path = os.path.join(
        props.MISCONCEPTIONS_HOME,
        "temp_func_%s.py" % helper.generate_random_string()[:6])
    func_name = helper.generate_function_name()
    args = ast.arguments(args=[ast.Name(id=str(variable))],
                         vararg=None,
                         kwarg=None,
                         defaults=[])
    func_node = ast.FunctionDef(name=func_name,
                                args=args,
                                body=as_nodes(body),
                                decorator_list=[])
    if func_node is None:
        return None
    func_str = astor.to_source(func_node)
    content = "\n\n".join([headers, func_str])
    cache.write_file(file_path, content)
    if helper.is_valid_file(file_path, props.PYTHON_SRC):
        return func_name, file_path
    cache.delete_file(file_path)
    return None, file_path