-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe_function_collection_excel.py
80 lines (67 loc) · 3.03 KB
/
e_function_collection_excel.py
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
"""Function analyzer for Python files with usage tracking and documentation extraction"""
import os
import ast
from typing import Dict, List
from collections import defaultdict
import pandas as pd
def extract_function_info(file_path: str) -> List[Dict]:
"""Extract function definitions and their docstrings from Python file"""
functions = []
try:
with open(file_path, 'r', encoding='utf-8') as file:
tree = ast.parse(file.read())
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
docstring = ast.get_docstring(node) or "No description available"
functions.append({
'name': node.name,
'file': os.path.basename(file_path),
'path': file_path,
'description': docstring.split('\n')[0],
'arguments': [arg.arg for arg in node.args.args],
'line_number': node.lineno
})
except Exception as e:
print(f"Error processing {file_path}: {e}")
return functions
def analyze_function_usage(directory: str) -> pd.DataFrame:
"""Analyze function definitions and usage across Python files"""
all_functions = []
usage_count = defaultdict(int)
# Walk through directory for Python files
for root, _, files in os.walk(directory):
for file in files:
if file.endswith('.py'):
file_path = os.path.join(root, file)
# Extract function definitions
functions = extract_function_info(file_path)
all_functions.extend(functions)
# Count function usage
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
for func in all_functions:
usage_count[func['name']] += content.count(func['name'])
except Exception as e:
print(f"Error reading {file_path}: {e}")
# Create DataFrame with function information
df = pd.DataFrame(all_functions)
if not df.empty:
df['usage_count'] = df['name'].map(usage_count)
df = df[['name', 'usage_count', 'file', 'path', 'description', 'line_number']]
df = df.sort_values('usage_count', ascending=False)
return df
def main():
upload_dir = r"C:\Users\md8w7\OneDrive University of Missouri\Desktop\ImportantFiles\Milestone4\backend"
print("Analyzing Python functions...")
df = analyze_function_usage(upload_dir)
# Save results to Excel
output_file = os.path.join(upload_dir, "e_function_collection_excel.xlsx")
df.to_excel(output_file, index=False)
print(f"\nAnalysis complete. Results saved to: {output_file}")
# Display summary
print(f"\nTotal functions found: {len(df)}")
print("\nTop 10 most used functions:")
print(df.head(10)[['name', 'usage_count', 'file']].to_string())
if __name__ == "__main__":
main()