meta.asttools - operate on python ast nodes

Module to augment and analize python ast nodes.

This module uses the python ast moduel exclusivly not the depricated compiler.ast.

class meta.asttools.Undedined[source]
meta.asttools.cmp_ast(node1, node2)[source]

Compare if two nodes are equal.

meta.asttools.print_ast(ast, indent=' ', initlevel=0, newline='\n', file=<open file '<stdout>', mode 'w' at 0x23f078>)

Pretty print an ast node.

Parameters:
  • ast – the ast to print.
  • indent – how far to indent a newline.
  • initlevel – starting indent level
  • newline – The newline character.
  • file – file object to print to

To print a short ast you may want to use:

node = ast.parse(source)
print_ast(node, indent='', newline='')
meta.asttools.str_ast(ast, indent=' ', newline='\n')

Returns a string representing the ast.

Parameters:
  • ast – the ast to print.
  • indent – how far to indent a newline.
  • newline – The newline character.
meta.asttools.python_source(ast, file=<open file '<stdout>', mode 'w' at 0x23f078>)

Generate executable python source code from an ast node.

Parameters:
  • ast – ast node
  • file – file to write output to.
meta.asttools.dump_python_source(ast)
Returns:

a string containing executable python source code from an ast node.

Parameters:
  • ast – ast node
  • file – file to write output to.
meta.asttools.lhs(node)

Return a set of symbols in node that are assigned.

Parameters:node – ast node
Returns:set of strings.
meta.asttools.rhs(node)

Return a set of symbols in node that are used.

Parameters:node – ast node
Returns:set of strings.
meta.asttools.conditional_lhs(node)

Group outputs into contitional and stable :param node: ast node

Returns:tuple of (contitional, stable)
meta.asttools.conditional_symbols(node)

Group lhs and rhs into contitional, stable and undefined :param node: ast node

Returns:tuple of (contitional_lhs, stable_lhs),(contitional_rhs, stable_rhs), undefined
meta.asttools.get_symbols(node, ctx_types=(<class '_ast.Load'>, <class '_ast.Store'>))

Returns all symbols defined in an ast node.

if ctx_types is given, then restrict the symbols to ones with that context.

Parameters:
  • node – ast node
  • ctx_types – type or tuple of types that may be found assigned to the ctx attribute of an ast Name node.
meta.asttools.make_graph(node, call_deps=False)

Create a dependency graph from an ast node.

Parameters:
  • node – ast node.
  • call_deps – if true, then the graph will create a cyclic dependance for all function calls. (i.e for a.b(c) a depends on b and b depends on a)
Returns:

a tuple of (graph, undefined)

Previous topic

Meta API

Next topic

meta.decompile - decompile code objects into ast nodes

This Page