Python API

class astsearch.ASTPatternFinder(pattern)

Scans Python code for AST nodes matching pattern.

Parameters:pattern (ast.AST) – The node pattern to search for
scan_ast(tree)

Walk an AST and yield nodes matching pattern.

Parameters:tree (ast.AST) – The AST in which to search
scan_file(file)

Parse a file and yield AST nodes matching pattern.

Parameters:file – Path to a Python file, or a readable file object
scan_directory(directory)

Walk files in a directory, yielding (filename, node) pairs matching pattern.

Parameters:directory (str) – Path to a directory to search

Only files with a .py or .pyw extension will be scanned.

astsearch.prepare_pattern(s)

Turn a string pattern into an AST pattern

This parses the string to an AST, and generalises it a bit for sensible matching. ? is treated as a wildcard that matches anything. Names in the pattern will match names or attribute access (i.e. foo will match bar.foo in files).

See also

astcheck
The AST pattern matching used by ASTsearch
Green Tree Snakes
An overview of the available AST nodes and how to work with them