-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathnotebooks.py
More file actions
216 lines (179 loc) · 7.13 KB
/
notebooks.py
File metadata and controls
216 lines (179 loc) · 7.13 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import json
from pathlib import Path
from typing import Any
DEFAULT_CELLS = [
{
"cell_type": "code",
"source": """from codegen.sdk.core.codebase import Codebase
# Initialize codebase
codebase = Codebase('../../')
# Print out stats
print("🔍 Codebase Analysis")
print("=" * 50)
print(f"📚 Total Files: {len(codebase.files)}")
print(f"⚡ Total Functions: {len(codebase.functions)}")
print(f"🔄 Total Imports: {len(codebase.imports)}")""".strip(),
}
]
DEMO_CELLS = [
##### [ CODEGEN DEMO ] #####
{
"cell_type": "markdown",
"source": """# Codegen Demo: FastAPI
Welcome to [Codegen](https://docs.codegen.com)!
This demo notebook will walk you through some features of Codegen applied to [FastAPI](https://github.com/fastapi/fastapi).
See the [getting started](https://docs.codegen.com/introduction/getting-started) guide to learn more.""".strip(),
},
{
"cell_type": "code",
"source": """from codegen.sdk.core.codebase import Codebase
# Initialize FastAPI codebase
print('Cloning and parsing FastAPI to /tmp/codegen/fastapi...')
codebase = Codebase.from_repo('fastapi/fastapi', commit="eab0653a346196bff6928710410890a300aee4ae")
# To initialize a local codebase, use this constructor
# codebase = Codebase("path/to/git/repo")""".strip(),
},
##### [ CODEBASE ANALYSIS ] #####
{
"cell_type": "markdown",
"source": """# Codebase Analysis
Let's do a quick codebase analysis!
- Grab codebase content with [codebase.functions](https://docs.codegen.com/building-with-codegen/symbol-api) et al.
- View inheritance hierarchies with [inhertance APIs](https://docs.codegen.com/building-with-codegen/class-api#working-with-inheritance)
- Identify recursive functions by looking at [FunctionCalls](https://docs.codegen.com/building-with-codegen/function-calls-and-callsites)""".strip(),
},
{
"cell_type": "code",
"source": """# Print overall stats
print("🔍 FastAPI Analysis")
print("=" * 50)
print(f"📚 Total Classes: {len(codebase.classes)}")
print(f"⚡ Total Functions: {len(codebase.functions)}")
print(f"🔄 Total Imports: {len(codebase.imports)}")
# Find class with most inheritance
if codebase.classes:
deepest_class = max(codebase.classes, key=lambda x: len(x.superclasses))
print(f"\\n🌳 Class with most inheritance: {deepest_class.name}")
print(f" 📊 Chain Depth: {len(deepest_class.superclasses)}")
print(f" ⛓️ Chain: {' -> '.join(s.name for s in deepest_class.superclasses)}")
# Find first 5 recursive functions
recursive = [f for f in codebase.functions
if any(call.name == f.name for call in f.function_calls)][:5]
if recursive:
print(f"\\n🔄 Recursive functions:")
for func in recursive:
print(f" - {func.name} ({func.file.filepath})")""".strip(),
},
##### [ TEST DRILL DOWN ] #####
{
"cell_type": "markdown",
"source": """# Drilling Down on Tests
Let's specifically drill into large test files, which can be cumbersome to manage:""".strip(),
},
{
"cell_type": "code",
"source": """from collections import Counter
# Filter to all test functions and classes
test_functions = [x for x in codebase.functions if x.name.startswith('test_')]
print("🧪 Test Analysis")
print("=" * 50)
print(f"📝 Total Test Functions: {len(test_functions)}")
print(f"📊 Tests per File: {len(test_functions) / len(codebase.files):.1f}")
# Find files with the most tests
print("\\n📚 Top Test Files by Count")
print("-" * 50)
file_test_counts = Counter([x.file for x in test_functions])
for file, num_tests in file_test_counts.most_common()[:5]:
print(f"🔍 {num_tests} test functions: {file.filepath}")
print(f" 📏 File Length: {len(file.source.split('\\n'))} lines")
print(f" 💡 Functions: {len(file.functions)}")""".strip(),
},
##### [ TEST SPLITTING ] #####
{
"cell_type": "markdown",
"source": """# Splitting Up Large Test Files
Lets split up the largest test files into separate modules for better organization.
This uses Codegen's [codebase.move_to_file(...)](https://docs.codegen.com/building-with-codegen/moving-symbols), which will:
- update all imports
- (optionally) move depenencies
- do so very fast ⚡️
While maintaining correctness.""",
},
##### [ TEST SPLITTING ] #####
{
"cell_type": "code",
"source": """filename = 'tests/test_path.py'
print(f"📦 Splitting Test File: {filename}")
print("=" * 50)
# Grab a file
file = codebase.get_file(filename)
base_name = filename.replace('.py', '')
# Group tests by subpath
test_groups = {}
for test_function in file.functions:
if test_function.name.startswith('test_'):
test_subpath = '_'.join(test_function.name.split('_')[:3])
if test_subpath not in test_groups:
test_groups[test_subpath] = []
test_groups[test_subpath].append(test_function)
# Print and process each group
for subpath, tests in test_groups.items():
print(f"\\n{subpath}/")
new_filename = f"{base_name}/{subpath}.py"
# Create file if it doesn't exist
if not codebase.has_file(new_filename):
new_file = codebase.create_file(new_filename)
file = codebase.get_file(new_filename)
# Move each test in the group
for test_function in tests:
print(f" - {test_function.name}")
test_function.move_to_file(new_file, strategy="add_back_edge")
# Commit changes to disk
codebase.commit()""".strip(),
},
##### [ RESET ] #####
{
"cell_type": "markdown",
"source": """## View Changes
You can now view changes by `cd /tmp/codegen/fastapi && git diff`
Enjoy!
# Reset
Reset your codebase to it's initial state, discarding all changes
Learn more in [commit and reset](https://docs.codegen.com/building-with-codegen/commit-and-reset).""".strip(),
},
{
"cell_type": "code",
"source": """codebase.reset()""".strip(),
},
]
def create_cells(cells_data: list[dict[str, str]]) -> list[dict[str, Any]]:
"""Convert cell data into Jupyter notebook cell format."""
return [
{
"cell_type": cell["cell_type"],
"source": cell["source"],
"metadata": {},
"execution_count": None,
"outputs": [] if cell["cell_type"] == "code" else None,
}
for cell in cells_data
]
def create_notebook(jupyter_dir: Path, demo: bool = False) -> Path:
"""Create a new Jupyter notebook if it doesn't exist.
Args:
jupyter_dir: Directory where the notebook should be created
demo: Whether to create a demo notebook with FastAPI example code
Returns:
Path to the created or existing notebook
"""
notebook_path = jupyter_dir / ("demo.ipynb" if demo else "tmp.ipynb")
if not notebook_path.exists():
cells = create_cells(DEMO_CELLS if demo else DEFAULT_CELLS)
notebook_content = {
"cells": cells,
"metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}},
"nbformat": 4,
"nbformat_minor": 4,
}
notebook_path.write_text(json.dumps(notebook_content, indent=2))
return notebook_path