How to use the transonic.util.format_str 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 / backends / base.py View on Github external
def _make_code_from_fdef_node(self, fdef):
        transformed = TypeHintRemover().visit(fdef)
        # convert the AST back to source code
        code = extast.unparse(transformed)
        return format_str(code)
github fluiddyn / transonic / tmp / methods / try_analyze.py View on Github external
index_self = tokens.index((NAME, "self"))

    tokens_attr = []
    for ind, attr in enumerate(attributes_self):
        tokens_attr.append((NAME, attr))
        tokens_attr.append((OP, ","))

    tokens = tokens[:index_self] + tokens_attr + tokens[index_self + 2 :]

    index_func_name = tokens.index((NAME, func_name))
    name_backend_func = f"__for_method__{cls_name}__{func_name}"
    tokens[index_func_name] = (NAME, name_backend_func)

    new_code = untokenize(tokens).decode("utf-8")
    new_code = format_str(new_code)

    # args_pythran = attributes + args_func
    types_pythran = types_attrs + types_func

    pythran_signatures = "\n"

    for types_string_signature in compute_signatures_from_typeobjects(
        types_pythran
    ):
        pythran_signatures += (
            "# pythran export "
            + name_backend_func
            + "("
            + ", ".join(types_string_signature)
            + ")\n"
        )
github fluiddyn / transonic / transonic / backends / numba.py View on Github external
xsimd=False,
        openmp=False,
        str_accelerator_flags: Optional[str] = None,
        parallel=True,
        force=True,
    ):
        if name_ext_file is None:
            name_ext_file = self.name_ext_from_path_backend(path_backend)

        with open(path_backend) as file:
            source = file.read()

        source = source.replace("# __protected__ ", "")

        with open(path_backend.with_name(name_ext_file), "w") as file:
            file.write(format_str(source))

        compiling = False
        process = None
        return compiling, process
github fluiddyn / transonic / transonic / backends / base.py View on Github external
else:
            str_args_backend_func = str_args_func

        name_var_code_new_method = f"__code_new_method__{class_name}__{meth_name}"

        self._append_line_header_variable(
            signatures_method, name_var_code_new_method
        )
        python_code += (
            f'\n{name_var_code_new_method} = """\n\n'
            f"def new_method(self, {str_args_value_func}):\n"
            f"    return backend_func({str_args_backend_func})"
            '\n\n"""\n'
        )

        return signatures_method, format_str(python_code)
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))