Python API

class astsearch.ASTPatternFinder(pattern)

Scans Python code for AST nodes matching pattern.

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

Walk an AST and yield nodes matching pattern.

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

Parse a file and yield AST nodes matching pattern.

Parameters:file – Path to a Python file, or a readable file object

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.


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 in files).

See also

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