@@ -9,7 +9,7 @@ A quick tour of Codegen in a Jupyter notebook.
9
9
10
10
## Installation
11
11
12
- Install [ ` codegen ` on pypi ] ( https://pypi.org/project/codegen/ ) via [ uv] ( https://github.com/astral-sh/uv ) :
12
+ Install [ codegen] ( https://pypi.org/project/codegen/ ) on Pypi via [ uv] ( https://github.com/astral-sh/uv ) :
13
13
14
14
``` bash
15
15
uv tool install codegen
@@ -34,8 +34,8 @@ The [codgen notebook](/cli/notebook) command creates a virtual environment and o
34
34
# Navigate to your repository
35
35
cd path/to/git/repository
36
36
37
- # Initialize codegen and launch Jupyter
38
- codegen notebook
37
+ # Initialize codegen and launch Jupyter with a demo notebook
38
+ codegen notebook --demo
39
39
```
40
40
41
41
This will:
@@ -47,7 +47,7 @@ This will:
47
47
2 . Launch Jupyter Lab with a pre-configured notebook
48
48
49
49
<Tip >
50
- The notebook comes pre-configured to load your codebase, so you can start
50
+ The ` notebook --demo ` comes pre-configured to load [ FastAPI ] ( https://github.com/fastapi/fastapi ) 's codebase , so you can start
51
51
exploring right away!
52
52
</Tip >
53
53
@@ -58,13 +58,17 @@ Instantiating a [Codebase](/api-reference/core/Codebase) will automatically pars
58
58
``` python
59
59
from codegen import Codebase
60
60
61
- # Parse a codebase
62
- codebase = Codebase(" ./" )
61
+ # Initialize FastAPI codebase
62
+ print (' Cloning and parsing FastAPI to /tmp/codegen/fastapi...' )
63
+ codebase = Codebase.from_repo(' fastapi/fastapi' )
64
+
65
+ # To initialize an existing local codebase, use this constructor
66
+ # codebase = Codebase("path/to/git/repo")
63
67
```
64
68
65
69
<Note >
66
70
This will automatically infer the programming language of the codebase and
67
- parse all files in the codebase.
71
+ parse all files in the codebase. Learn more about [ parsing codebases here ] ( /building-with-codegen/parsing-codebases )
68
72
</Note >
69
73
70
74
<Tip >
@@ -81,8 +85,10 @@ Here are some common patterns for code navigation in Codegen:
81
85
82
86
- Iterate over all [ Functions] ( /api-reference/core/Function ) with [ Codebase.functions] ( /api-reference/core/Codebase#functions )
83
87
- View class inheritance with [ Class.superclasses] ( /api-reference/core/Class#superclasses )
84
- - View function call-sites with [ Function.call_sites] ( /api-reference/core/Function#call-sites )
85
88
- View function usages with [ Function.usages] ( /api-reference/core/Function#usages )
89
+ - View inheritance hierarchies with [ inheritance APIs] ( https://docs.codegen.com/building-with-codegen/class-api#working-with-inheritance )
90
+ - Identify recursive functions by looking at [ FunctionCalls] ( https://docs.codegen.com/building-with-codegen/function-calls-and-callsites )
91
+ - View function call-sites with [ Function.call_sites] ( /api-reference/core/Function#call-sites )
86
92
87
93
``` python
88
94
# Print overall stats
@@ -108,7 +114,7 @@ if recursive:
108
114
print (f " - { func.name} " )
109
115
```
110
116
111
- ### Analyzing Tests
117
+ ## Analyzing Tests
112
118
113
119
Let's specifically drill into large test files, which can be cumbersome to manage.
114
120
@@ -135,38 +141,52 @@ for file, num_tests in file_test_counts.most_common()[:5]:
135
141
print (f " 💡 Functions: { len (file .functions)} " )
136
142
```
137
143
138
- ### Splitting Up Large Test Files
144
+ ## Splitting Up Large Test Files
145
+
146
+ Lets split up the largest test files into separate modules for better organization.
147
+
148
+ This uses Codegen's [ codebase.move_to_file(...)] ( /building-with-codegen/moving-symbols ) , which will:
149
+ - update all imports
150
+ - (optionally) move dependencies
151
+ - do so very fast ⚡️
139
152
140
- Lets split up the largest test files into separate modules for better organization:
153
+ While maintaining correctness.
141
154
142
155
``` python
143
- print (" \n 📦 Splitting Test Files" )
156
+ filename = ' tests/test_path.py'
157
+ print (f " 📦 Splitting Test File: { filename} " )
144
158
print (" =" * 50 )
145
159
146
- # Process top 5 largest test files
147
- for file , num_tests in file_test_counts.most_common()[:5 ]:
148
- # Create a new directory based on the file name
149
- base_name = file .path.replace(' .py' , ' ' )
150
- print (f " \n 🔄 Processing: { file .filepath} " )
151
- print (f " 📊 { num_tests} test classes to split " )
152
-
153
- # Move each test class to its own file
154
- for test_class in file .classes:
155
- if test_class.name.startswith(' Test' ):
156
- # Create descriptive filename from test class name
157
- new_file = f " { base_name} / { test_class.name.lower()} .py "
158
- print (f " 📝 Moving { test_class.name} -> { new_file} " )
159
-
160
- # Codegen handles all the complexity:
161
- # - Creates directories if needed
162
- # - Updates all imports automatically
163
- # - Maintains test dependencies
164
- # - Preserves decorators and docstrings
165
- test_class.move_to_file(new_file)
160
+ # Grab a file
161
+ file = codebase.get_file(filename)
162
+ base_name = filename.replace(' .py' , ' ' )
163
+
164
+ # Group tests by subpath
165
+ test_groups = {}
166
+ for test_function in file .functions:
167
+ if test_function.name.startswith(' test_' ):
168
+ test_subpath = ' _' .join(test_function.name.split(' _' )[:3 ])
169
+ if test_subpath not in test_groups:
170
+ test_groups[test_subpath] = []
171
+ test_groups[test_subpath].append(test_function)
172
+
173
+ # Print and process each group
174
+ for subpath, tests in test_groups.items():
175
+ print (f " \\ n { subpath} / " )
176
+ new_filename = f " { base_name} / { subpath} .py "
177
+
178
+ # Create file if it doesn't exist
179
+ if not codebase.has_file(new_filename):
180
+ new_file = codebase.create_file(new_filename)
181
+ file = codebase.get_file(new_filename)
182
+
183
+ # Move each test in the group
184
+ for test_function in tests:
185
+ print (f " - { test_function.name} " )
186
+ test_function.move_to_file(new_file, strategy = " add_back_edge" )
166
187
167
188
# Commit changes to disk
168
189
codebase.commit()
169
-
170
190
```
171
191
172
192
<Warning >
@@ -299,14 +319,15 @@ if base_class:
299
319
Understand key concepts like working with files, functions, imports, and the
300
320
call graph to effectively manipulate code.
301
321
</Card >
322
+ <Card title = " IDE Setup" icon = " window" href = " /introduction/ide-usage" >
323
+ Iterate locally with your favorite IDE, work with a debugger and build sophisticated codemods
324
+ </Card >
302
325
<Card
303
326
title = " Integrate with AI Tools"
304
- icon = " robot "
327
+ icon = " microchip "
305
328
href = " /introduction/work-with-ai"
306
329
>
307
330
Learn how to use Codegen with Cursor, Devin, Windsurf, and more.
308
331
</Card >
309
- <Card title = " API Reference" icon = " code" href = " /api-reference" >
310
- Explore the complete API documentation for all Codegen classes and methods.
311
- </Card >
332
+
312
333
</CardGroup >
0 commit comments