padl.dumptools.var2mod

class padl.dumptools.var2mod.CodeGraph

A graph representing python code.

The nodes in the graph are CodeNode objects, representing pieces of code defining python variables. The edges are the dependencies between the nodes.

As an example - the following code:

import foo

def f(x):
    return foo.bar(x) + 1

o = f(100)

defines a codegraph with the nodes:

foo: "import foo"
f: "def f(x) ..."
o: "o = f(100)"

Edges are determined by the variable dependencies between the nodes:

o -> f -> foo

As o depends on f and f depends on foo.

classmethod build(scoped_name: padl.dumptools.symfinder.ScopedName)

Build a codegraph corresponding to a ScopedName.

The name will be searched for in its scope.

dumps()

Create a python source string with the contents of the graph.

print()

Print the graph (for debugging).

class padl.dumptools.var2mod.CodeNode(source: str, globals_: set, name: str, scope: Optional[padl.dumptools.symfinder.Scope] = None, ast_node: Optional[_ast.AST] = None, n: int = 0)

A node in a CodeGraph.

A CodeNode has

  • a source: The source code represented by the CodeNode.

  • a set of globals_: Names the node depends on.

  • (optionally) a scope: The module and function scope the CodeNode lives in.

  • (optionally) an ast_node: An ast.AST object representing the code.

classmethod from_source(source, scope, name)

Build a CodeNode from a source string.

class padl.dumptools.var2mod.Finder(nodetype)

ast.NodeVisitor for finding AST-nodes of a given type in an AST-tree.

Example:

>>> Finder(ast.Name).find(ast.parse('x(y)'))  
[<_ast.Name object at 0x...>, <_ast.Name object at 0x...>]
find(node)

Find sub-nodes of node.

generic_visit(node)

Called if no explicit visitor function exists for a node.

get_source_segments(source)

Get a list of source segments of found nodes in source and their respective positions.

Returns

A list of tuples (<source segment>, <position>) where position is a tuple (<lineno>, <end lineno>, <col offset>, <end col offset>).

Example:

>>> Finder(ast.Name).get_source_segments('x(y)')
[('x', Position(lineno=1, end_lineno=1, col_offset=0, end_col_offset=1)), ('y', Position(lineno=1, end_lineno=1, col_offset=2, end_col_offset=3))]
class padl.dumptools.var2mod.Vars(globals, locals)
globals

Alias for field number 0

locals

Alias for field number 1

padl.dumptools.var2mod.find_codenode(name: padl.dumptools.symfinder.ScopedName, full_dump_module_names=None)

Find the CodeNode corresponding to a ScopedName name.

padl.dumptools.var2mod.find_globals(node: _ast.AST, filter_builtins: bool = True) Set[Tuple[str, int]]

Find all globals used below a node.

Example:

>>> source = '''
... def f(x):
...     y = a + 1
...     z = np.array(x + b)
...     return str(z)
... '''
>>> node = ast.parse(source).body[0]
>>> find_globals(node) == {ScopedName('a', None, 0), ScopedName('b', None, 0),
...                        ScopedName('np.array', None, 0)}
True
Parameters
  • node – AST node to search in.

  • filter_builtins – If True, filter out builtins.

padl.dumptools.var2mod.increment_same_name_var(variables: List[padl.dumptools.symfinder.ScopedName], scoped_name: padl.dumptools.symfinder.ScopedName)

Go through variables and increment the the counter for those with the same name as scoped_name by scoped_name.n.

Example:

>>> import padl as somemodule
>>> out = increment_same_name_var({ScopedName('a', None, 1), ScopedName('b', None, 2)},
...                               ScopedName('b', somemodule, 2))
>>> isinstance(out, set)
True
>>> {(x.name, x.n) for x in out} == {('a', 1), ('b', 4)}
True
padl.dumptools.var2mod.rename(tree: _ast.AST, from_: str, to: str, rename_locals: bool = False)

Rename things in an AST tree.

Parameters
  • tree – The tree in which to rename things.

  • from – Rename everything called this …

  • to – … to this.

  • rename_locals – If True, rename local things, else, only rename globals.