A lightweight Python tool for analyzing Python code structure using Abstract Syntax Trees (AST). This parser can extract functions and classes from Python files and provide search functionality.
- Parse individual Python files or entire directories
- Extract function and class definitions with line numbers
- Search for functions and classes by name (case-insensitive)
- Save and load parsing results to/from JSON files
- Clear results to start fresh analysis
- Interactive command-line interface
- Python 3.6 or higher
- No external dependencies (uses only Python standard library)
- Clone or download this repository
- Navigate to the project directory
- Run the parser:
python parser.py
Run the parser and follow the menu prompts:
python parser.py
- Parse a directory - Analyze all Python files in a specified directory
- Search for a function - Find functions by name (partial matching)
- Search for a class - Find classes by name (partial matching)
- Show results - Display all parsed functions and classes
- Save results to file - Export results to JSON format
- Load results from file - Import previously saved results
- Clear all parsed results - Reset the parser state
- Exit - Quit the application
- Start the parser:
python parser.py
- Choose option 1 to parse a directory
- Enter directory path (or press Enter for current directory)
- Choose option 4 to view results
- Choose option 2 to search for specific functions
- Choose option 5 to save results for later use
project/
├── parser.py # Main parser application
├── tests/ # Sample Python files for testing
│ ├── sample1.py
│ └── sample2.py
├── results.json # Generated results file (after saving)
└── README.md
The parser extracts the following information:
- Name
- Line number
- File path
- Name
- Line number
- Methods list
- File path
SUMMARY:
Functions found: 5
Classes found: 2
FUNCTIONS:
• add (line 3 in tests/sample1.py)
• subtract (line 7 in tests/sample1.py)
• multiply (line 11 in tests/sample1.py)
CLASSES:
• Calculator (calculate, get_history) (line 15 in tests/sample1.py)
• TodoList (add_task, mark_done, get_tasks) (line 3 in tests/sample2.py)
- File Reading - Read Python source code as text
- AST Generation - Convert code to Abstract Syntax Tree using
ast.parse()
- Tree Traversal - Walk through all nodes using
ast.walk()
- Node Identification - Identify
FunctionDef
andClassDef
nodes - Data Extraction - Extract relevant information (name, line number, etc.)
- Storage - Store results in structured dictionary format
- Function definitions (
def
) - Class definitions (
class
) - Methods within classes
- Nested functions and classes
- Does not analyze function parameters or return types
- Does not extract docstrings or comments
- Does not track variable usage or imports
- Limited to syntactically valid Python files
- Function parameter extraction
- Docstring analysis
- Import statement tracking
- Variable usage analysis
- Code complexity metrics
- Export to different formats (CSV, XML)
The parser includes error handling for:
- File not found errors
- Syntax errors in Python files
- JSON serialization/deserialization errors
- Invalid directory paths
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project is open source and available under the MIT License.
Created as a learning project for understanding Python AST parsing and code analysis techniques.