ASTsearch 0.2.0

ASTsearch is an intelligent search tool for Python code.

To get it:

pip install astsearch

To use it:

# astsearch pattern [path]
astsearch "?/?"  # Division operations in all files in the current directory
pattern

A search pattern, using ? as a wildcard to match anything. The pattern must be a valid Python statement once all ? wilcards have been replaced with a name.

path

A Python file or a directory in which to search. Directories will be searched recursively for .py and .pyw files.

-l, --files-with-matches

Output only the paths of matching files, not the lines that matched.

Contents:

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

See also

astpath
Search through ASTs using XPath syntax

Indices and tables