codegen

Extension to ast that allow ast -> python code generation.

copyright:Copyright 2008 by Armin Ronacher.
license:BSD.
class core.codegen.SourceGenerator(indent_with, add_line_information=False)[source]

Bases: ast.NodeVisitor

This visitor is able to transform a well formed syntax tree into python sourcecode. For more details have a look at the docstring of the node_to_source function.

body(statements)[source]
body_or_else(node)[source]
decorators(node)[source]
newline(node=None, extra=0)[source]
signature(node)[source]
visit_Assert(node)[source]
visit_Assign(node)[source]
visit_Attribute(node)[source]
visit_AugAssign(node)[source]
visit_BinOp(node)[source]
visit_BoolOp(node)[source]
visit_Break(node)[source]
visit_Bytes(node)[source]
visit_Call(node)[source]
visit_ClassDef(node)[source]
visit_Compare(node)[source]
visit_Continue(node)[source]
visit_Delete(node)[source]
visit_Dict(node)[source]
visit_DictComp(node)[source]
visit_Ellipsis(node)[source]
visit_Expr(node)[source]
visit_ExtSlice(node)[source]
visit_For(node)[source]
visit_FunctionDef(node)[source]
visit_GeneratorExp(node)
visit_Global(node)[source]
visit_If(node)[source]
visit_IfExp(node)[source]
visit_Import(node)[source]
visit_ImportFrom(node)[source]
visit_Lambda(node)[source]
visit_List(node)
visit_ListComp(node)
visit_Name(node)[source]
visit_Nonlocal(node)[source]
visit_Num(node)[source]
visit_Pass(node)[source]
visit_Print(node)[source]
visit_Raise(node)[source]
visit_Repr(node)[source]
visit_Return(node)[source]
visit_Set(node)
visit_SetComp(node)
visit_Slice(node)[source]
visit_Starred(node)[source]
visit_Str(node)[source]
visit_Subscript(node)[source]
visit_TryExcept(node)[source]
visit_TryFinally(node)[source]
visit_Tuple(node)[source]
visit_UnaryOp(node)[source]
visit_While(node)[source]
visit_With(node)[source]
visit_Yield(node)[source]
visit_alias(node)[source]
visit_arguments(node)[source]
visit_comprehension(node)[source]
visit_excepthandler(node)[source]
write(x)[source]
core.codegen.to_source(node, indent_with=' ', add_line_information=False)[source]

This function can convert a node tree back into python sourcecode. This is useful for debugging purposes, especially if you're dealing with custom asts not generated by python itself.

It could be that the sourcecode is evaluable when the AST itself is not compilable / evaluable. The reason for this is that the AST contains some more data than regular sourcecode does, which is dropped during conversion.

Each level of indentation is replaced with indent_with. Per default this parameter is equal to four spaces as suggested by PEP 8, but it might be adjusted to match the application's styleguide.

If add_line_information is set to True comments for the line numbers of the nodes are added to the output. This can be used to spot wrong line number information of statement nodes.

Previous topic

<no title>

Next topic

<no title>

This Page