Skip to content

Commit 06ffc13

Browse files
committed
wip
1 parent 83fabf5 commit 06ffc13

File tree

3 files changed

+34
-41
lines changed

3 files changed

+34
-41
lines changed

example_usage.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#!/usr/bin/env python3
2-
"""
3-
Example of using the ripgrep search script programmatically.
4-
"""
2+
"""Example of using the ripgrep search script programmatically."""
53

6-
from ripgrep_search import search_with_ripgrep
74
import json
85

6+
from ripgrep_search import search_with_ripgrep
7+
98
# Search for any pattern you want
109
pattern = "sorter" # Change this to any pattern you need
1110
results = search_with_ripgrep(pattern)
@@ -24,10 +23,6 @@
2423
json.dump(results, f, indent=2)
2524

2625
# Or filter results for specific files
27-
python_files_only = {
28-
path: matches
29-
for path, matches in results.items()
30-
if path.endswith('.py')
31-
}
26+
python_files_only = {path: matches for path, matches in results.items() if path.endswith(".py")}
3227

33-
print(f"\nPython files with matches: {len(python_files_only)}")
28+
print(f"\nPython files with matches: {len(python_files_only)}")

function_call_visitor.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
This module provides a visitor that can track calls to specific functions,
44
including regular functions, methods, classmethods, and staticmethods.
55
"""
6+
67
from __future__ import annotations
78

89
import ast
@@ -103,16 +104,16 @@ def _get_call_text(self, node: ast.Call) -> str:
103104
if node.lineno == node.end_lineno:
104105
line = self._source_lines[node.lineno - 1]
105106
if hasattr(node, "col_offset") and hasattr(node, "end_col_offset"):
106-
return line[node.col_offset:node.end_col_offset]
107+
return line[node.col_offset : node.end_col_offset]
107108
else:
108109
# Multi-line call
109110
lines = []
110111
for i in range(node.lineno - 1, node.end_lineno):
111112
if i < len(self._source_lines):
112113
if i == node.lineno - 1:
113-
lines.append(self._source_lines[i][node.col_offset:])
114+
lines.append(self._source_lines[i][node.col_offset :])
114115
elif i == node.end_lineno - 1:
115-
lines.append(self._source_lines[i][:node.end_col_offset])
116+
lines.append(self._source_lines[i][: node.end_col_offset])
116117
else:
117118
lines.append(self._source_lines[i])
118119
return " ".join(line.strip() for line in lines)
@@ -166,7 +167,7 @@ def visit_Call(self, node: ast.Call):
166167
call_text=self._get_call_text(node),
167168
in_loop=self._in_loop(),
168169
loop_type=self._get_loop_type(),
169-
file_path=self.file_path
170+
file_path=self.file_path,
170171
)
171172
self.calls.append(call_info)
172173

@@ -202,7 +203,7 @@ def get_summary(self) -> dict:
202203
"calls_outside_loops": len(calls_outside_loops),
203204
"all_calls": self.calls,
204205
"loop_calls": calls_in_loops,
205-
"non_loop_calls": calls_outside_loops
206+
"non_loop_calls": calls_outside_loops,
206207
}
207208

208209

test_function_call_visitor.py

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
"""
2-
Test and demonstrate the FunctionCallVisitor capabilities.
3-
"""
1+
"""Test and demonstrate the FunctionCallVisitor capabilities."""
42

5-
import ast
6-
from function_call_visitor import FunctionCallVisitor, analyze_code, analyze_file
3+
from function_call_visitor import analyze_code, analyze_file
74

85

96
def test_basic_calls():
@@ -15,10 +12,10 @@ def example():
1512
max([4, 5, 6])
1613
print("World")
1714
"""
18-
results = analyze_code(code, ['print', 'len'])
15+
results = analyze_code(code, ["print", "len"])
1916
print("Test: Basic Calls")
2017
print(f" Found {results['total_calls']} calls")
21-
for call in results['all_calls']:
18+
for call in results["all_calls"]:
2219
print(f" {call}")
2320
print()
2421

@@ -40,13 +37,13 @@ def process():
4037
4138
print("End") # Outside loop
4239
"""
43-
results = analyze_code(code, ['print', 'len'])
40+
results = analyze_code(code, ["print", "len"])
4441
print("Test: Loop Detection")
4542
print(f" Total calls: {results['total_calls']}")
4643
print(f" In loops: {results['calls_in_loops']}")
4744
print(f" Outside loops: {results['calls_outside_loops']}")
4845
print(" Loop calls:")
49-
for call in results['loop_calls']:
46+
for call in results["loop_calls"]:
5047
print(f" {call}")
5148
print()
5249

@@ -63,9 +60,9 @@ def nested():
6360
print(f"Innermost")
6461
j += 1
6562
"""
66-
results = analyze_code(code, ['print'])
63+
results = analyze_code(code, ["print"])
6764
print("Test: Nested Loops")
68-
for call in results['all_calls']:
65+
for call in results["all_calls"]:
6966
print(f" {call}")
7067
print()
7168

@@ -104,10 +101,10 @@ def helper():
104101
MyClass.create()
105102
MyClass.helper()
106103
"""
107-
results = analyze_code(code, ['append', 'validate', 'len'])
104+
results = analyze_code(code, ["append", "validate", "len"])
108105
print("Test: Method Calls")
109106
print(f" Found {results['total_calls']} calls")
110-
for call in results['all_calls']:
107+
for call in results["all_calls"]:
111108
print(f" {call}")
112109
print()
113110

@@ -132,11 +129,11 @@ def example():
132129
# Nested module calls
133130
result = os.path.dirname(os.path.join("x", "y"))
134131
"""
135-
results = analyze_code(code, ['os.path.join', 'np.array', 'sqrt', 'os.path.exists', 'np.zeros', 'os.path.dirname'])
132+
results = analyze_code(code, ["os.path.join", "np.array", "sqrt", "os.path.exists", "np.zeros", "os.path.dirname"])
136133
print("Test: Module Calls")
137134
print(f" Total calls: {results['total_calls']}")
138135
print(" All calls:")
139-
for call in results['all_calls']:
136+
for call in results["all_calls"]:
140137
print(f" {call}")
141138
print()
142139

@@ -164,10 +161,10 @@ def complex_example():
164161
# Calls in dict comprehensions
165162
d = {x: len(x) for x in ["key1", "key2"]}
166163
"""
167-
results = analyze_code(code, ['len', 'print', 'max'])
164+
results = analyze_code(code, ["len", "print", "max"])
168165
print("Test: Complex Expressions")
169166
print(f" Found {results['total_calls']} calls")
170-
for call in results['all_calls']:
167+
for call in results["all_calls"]:
171168
print(f" {call}")
172169
print()
173170

@@ -191,9 +188,9 @@ async def async_generator():
191188
async def process_item(item):
192189
print(f"Item: {item}")
193190
"""
194-
results = analyze_code(code, ['print', 'process_item'])
191+
results = analyze_code(code, ["print", "process_item"])
195192
print("Test: Async Code")
196-
for call in results['all_calls']:
193+
for call in results["all_calls"]:
197194
print(f" {call}")
198195
print()
199196

@@ -218,10 +215,10 @@ def file_operations():
218215
list2.append(2)
219216
# some_obj.data.append(3) # Would need some_obj to be defined
220217
"""
221-
results = analyze_code(code, ['join', 'append'])
218+
results = analyze_code(code, ["join", "append"])
222219
print("Test: Partial Matching")
223-
print(f" Tracking 'join' and 'append'")
224-
for call in results['all_calls']:
220+
print(" Tracking 'join' and 'append'")
221+
for call in results["all_calls"]:
225222
print(f" {call}")
226223
print()
227224

@@ -254,13 +251,13 @@ def run_all_tests():
254251
print("\nExample: Analyzing the visitor file itself")
255252
print("-" * 60)
256253
try:
257-
results = analyze_file("function_call_visitor.py", ['isinstance', 'append', 'len'])
254+
results = analyze_file("function_call_visitor.py", ["isinstance", "append", "len"])
258255
print(f"Found {results['total_calls']} calls in function_call_visitor.py")
259256
print(f" In loops: {results['calls_in_loops']}")
260257
print(f" Outside loops: {results['calls_outside_loops']}")
261-
if results['loop_calls']:
258+
if results["loop_calls"]:
262259
print("\nCalls in loops:")
263-
for call in results['loop_calls'][:5]: # Show first 5
260+
for call in results["loop_calls"][:5]: # Show first 5
264261
print(f" {call}")
265262
except FileNotFoundError:
266-
print(" (File not found - run from the same directory)")
263+
print(" (File not found - run from the same directory)")

0 commit comments

Comments
 (0)