-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathanalyze_generator_candidates.py
More file actions
125 lines (98 loc) · 3.73 KB
/
Copy pathanalyze_generator_candidates.py
File metadata and controls
125 lines (98 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""Analyze Generator Migration Candidates
Identifies list comprehensions that can be converted to generators
for memory optimization.
Criteria for good candidates:
1. List is only iterated once (not reused)
2. List is used in sum(), max(), min(), any(), all()
3. List is passed directly to another function that accepts iterables
4. Large intermediate lists (>100 items)
Copyright 2025 Smart-AI-Memory
Licensed under Fair Source License 0.9
"""
import ast
import sys
from pathlib import Path
from typing import Any
# Add src to path
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
class ListCompFinder(ast.NodeVisitor):
"""Find list comprehensions and analyze their usage."""
def __init__(self):
self.candidates = []
self.current_function = None
def visit_FunctionDef(self, node):
"""Track current function for context."""
old_function = self.current_function
self.current_function = node.name
self.generic_visit(node)
self.current_function = old_function
def visit_ListComp(self, node):
"""Analyze list comprehension usage."""
# Get source code (approximate)
try:
code = ast.unparse(node)
except Exception:
code = "<unparsable>"
# This is a simplified analysis - real usage would need more context
self.candidates.append(
{
"function": self.current_function,
"code": code[:100], # Truncate long expressions
"line": getattr(node, "lineno", 0),
}
)
self.generic_visit(node)
def analyze_file(file_path: Path) -> list[dict[str, Any]]:
"""Analyze a Python file for list comprehension candidates."""
try:
content = file_path.read_text(encoding="utf-8")
tree = ast.parse(content)
finder = ListCompFinder()
finder.visit(tree)
return finder.candidates
except (SyntaxError, UnicodeDecodeError):
return []
def main():
"""Analyze key files for generator migration opportunities."""
print("=" * 70)
print("GENERATOR MIGRATION CANDIDATE ANALYSIS")
print("=" * 70)
print()
# Key files to analyze
target_files = [
"src/empathy_os/project_index/scanner.py",
"src/empathy_os/cost_tracker.py",
"src/empathy_os/pattern_library.py",
"src/empathy_os/workflows/test_gen.py",
"src/empathy_os/telemetry/cli.py",
]
total_candidates = 0
for file_path_str in target_files:
file_path = Path(file_path_str)
if not file_path.exists():
continue
print(f"File: {file_path}")
print("-" * 70)
candidates = analyze_file(file_path)
if candidates:
print(f"Found {len(candidates)} list comprehensions:")
for i, candidate in enumerate(candidates[:10], 1): # Show first 10
print(f" {i}. Line {candidate['line']}: {candidate['function'] or '<module>'}")
print(f" {candidate['code']}")
if len(candidates) > 10:
print(f" ... and {len(candidates) - 10} more")
else:
print(" No list comprehensions found")
print()
total_candidates += len(candidates)
print("=" * 70)
print(f"TOTAL: {total_candidates} list comprehensions across {len(target_files)} files")
print("=" * 70)
print()
print("MANUAL REVIEW NEEDED:")
print("- Check if list is only used once (good candidate)")
print("- Check if list is used in sum(), max(), min(), any(), all()")
print("- Avoid converting if list is reused multiple times")
print("- Avoid converting if len() is needed (unless we can count differently)")
if __name__ == "__main__":
main()