How to use the transonic.analyses.extast.parse function in transonic

To help you get started, we’ve selected a few transonic examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github fluiddyn / transonic / transonic / analyses / util.py View on Github external
and isinstance(node.value, ast.Call)
            and node.value.func.id == "jit"
        ):
            # if assigned name different from jitted function name
            if node.targets[0].id != node.value.args[0].id:
                # change function names in jitted dict
                # The list "special" contains functions that has to be write from jitted_dicts
                # see transonic.justintime:287
                def_func = extast.unparse(jitted_dicts["functions"][func])
                spl = re.split(r"(\W+)", def_func)
                spl = [
                    node.value.args[0].id if x == node.targets[0].id else x
                    for x in spl
                ]
                st = "".join(str(e) for e in spl)
                jitted_dicts["functions"][func] = extast.parse(st)
                special.append(func)
                jitted_functions.append(node.value.args[0].id)
            else:
                jitted_functions.append(node.targets[0].id)
            module.body.remove(node)
            module.body.insert(
                0,
                [
                    extast.parse(
                        "from "
                        + node.value.args[0].id
                        + " import "
                        + node.value.args[0].id
                    )
                ],
            )
github fluiddyn / transonic / transonic / backends / numba.py View on Github external
def add_numba_comments(code):
    """Add Numba code in Python comments"""
    mod = parse(code)
    new_body = [CommentLine("# __protected__ from numba import njit")]

    for node in mod.body:
        if isinstance(node, ast.FunctionDef):
            new_body.append(CommentLine("# __protected__ @njit"))
        new_body.append(node)

    mod.body = new_body
    return format_str(unparse(mod))
github fluiddyn / transonic / tmp / var_annot / analyze.py View on Github external
from transonic.analyses import extast
from transonic.analyses import analyse_aot
from transonic.analyses.util import print_dumped, print_unparsed

from transonic.backends import backends

backend = backends["cython"]

path_file = "simple.py"

with open(path_file) as file:
    code = file.read()

boosted_dicts, code_dependance, annotations, blocks, code_ext = analyse_aot(code, path_file)

module = extast.parse(code)

for node in module.body:
    if isinstance(node, ast.FunctionDef):
        fdef = node
        break

# annotations_locals = annotations["__locals__"]

# annot = annotations["functions"][fdef.name]

# fdef.body = []
# fdef.decorator_list = []

# print_unparsed(fdef)

print("\n".join(backend.get_signatures(fdef.name, fdef, annotations)))
github fluiddyn / transonic / transonic / analyses / util.py View on Github external
for node in module_ext.body:
            if not isinstance(node, (ast.ImportFrom, ast.Import)):
                continue
            # get the path of the imported module
            file_name, file_path = find_path(node, pathfile)
            # a jitted function or method needs another jitted function
            if not (file_name and file_name not in treated):
                continue
            new_file_name = f"__ext__{func}__{file_name}"
            # get the content of the file
            try:
                with open(str(file_path), "r") as file:
                    content = file.read()
            except:
                raise NotImplementedError(file_name + " can no be found")
            mod = extast.parse(content)
            # filter the code and add it to code_ext dict
            code_ext[classes][new_file_name] = str(
                filter_external_code(mod, node.names)
            )
            # change imported module names
            codes_dependance[func] = change_import_name(
                codes_dependance[func], node, func, relative
            )
            # recursively get the exterior codes
            if code_ext[classes][new_file_name]:
                get_exterior_code(
                    {func: code_ext[classes][new_file_name]},
                    pathfile,
                    new_file_name,
                    classes,
                )
github fluiddyn / transonic / transonic / analyses / util.py View on Github external
def change_import_name(
    code_dep: str, changed_node: object, func_name: str, relative: str = None
):
    """Change the name of changed_node in code_dep by adding "__" + func + "__"
    at the beginning of the imported module, and return the modified code
    """
    mod = extast.parse(code_dep)
    for node in mod.body:
        if extast.unparse(node) == extast.unparse(changed_node):
            if isinstance(node, ast.ImportFrom):
                node.module = f"__ext__{func_name}__{node.module}"
            elif isinstance(node, ast.Import):
                node.names[0].name = f"__ext__{func_name}__{node.names[0].name}"
        if not relative:
            node.level = 0
    return extast.unparse(mod)
github fluiddyn / transonic / transonic / analyses / __init__.py View on Github external
def analyse_aot(code, pathfile, backend_name):
    """Gather the informations for ``@boost`` and blocks"""
    debug = logger.debug

    debug("extast.parse")
    module = extast.parse(code)

    debug("compute ancestors and chains")
    ancestors, duc, udc = compute_ancestors_chains(module)

    debug("filter_code_typevars")
    code_dependance_annotations = filter_code_typevars(module, duc, ancestors)
    debug(code_dependance_annotations)

    debug("find boosted objects")
    boosted_dicts = get_decorated_dicts(
        module, ancestors, duc, None, backend_name
    )
    debug(pformat(boosted_dicts))

    debug("compute the annotations")
github fluiddyn / transonic / transonic / backends / base.py View on Github external
)

        for attr in attributes:
            if attr not in annotations_class:
                raise NotImplementedError(
                    f"self.{attr} used but {attr} not in class annotations"
                )
        types_attrs = {
            "self_" + attr: annotations_class[attr] for attr in attributes
        }
        types_pythran = {**types_attrs, **annotations_meth}

        # TODO: locals_types for methods
        locals_types = None
        signatures_method = self._make_header_from_fdef_annotations(
            extast.parse(python_code).body[0], [types_pythran], locals_types
        )

        str_self_dot_attributes = ", ".join("self." + attr for attr in attributes)
        args_func = [arg.id for arg in fdef.args.args[1:]]
        str_args_func = ", ".join(args_func)

        defaults = fdef.args.defaults
        nb_defaults = len(defaults)
        nb_args = len(fdef.args.args)
        nb_no_defaults = nb_args - nb_defaults - 1

        str_args_value_func = []
        ind_default = 0
        for ind, arg in enumerate(fdef.args.args[1:]):
            name = arg.id
            if ind < nb_no_defaults: