@@ -9,7 +9,7 @@ A quick tour of Codegen in a Jupyter notebook.
99
1010## Installation
1111
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 ) :
1313
1414``` bash
1515uv tool install codegen
@@ -34,8 +34,8 @@ The [codgen notebook](/cli/notebook) command creates a virtual environment and o
3434# Navigate to your repository
3535cd path/to/git/repository
3636
37- # Initialize codegen and launch Jupyter
38- codegen notebook
37+ # Initialize codegen and launch Jupyter with a demo notebook
38+ codegen notebook --demo
3939```
4040
4141This will:
@@ -47,7 +47,7 @@ This will:
47472 . Launch Jupyter Lab with a pre-configured notebook
4848
4949<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
5151 exploring right away!
5252</Tip >
5353
@@ -58,13 +58,17 @@ Instantiating a [Codebase](/api-reference/core/Codebase) will automatically pars
5858``` python
5959from codegen import Codebase
6060
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")
6367```
6468
6569<Note >
6670 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 )
6872</Note >
6973
7074<Tip >
@@ -81,8 +85,10 @@ Here are some common patterns for code navigation in Codegen:
8185
8286- Iterate over all [ Functions] ( /api-reference/core/Function ) with [ Codebase.functions] ( /api-reference/core/Codebase#functions )
8387- 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 )
8588- 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 )
8692
8793``` python
8894# Print overall stats
@@ -108,7 +114,7 @@ if recursive:
108114 print (f " - { func.name} " )
109115```
110116
111- ### Analyzing Tests
117+ ## Analyzing Tests
112118
113119Let's specifically drill into large test files, which can be cumbersome to manage.
114120
@@ -135,38 +141,52 @@ for file, num_tests in file_test_counts.most_common()[:5]:
135141 print (f " 💡 Functions: { len (file .functions)} " )
136142```
137143
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 ⚡️
139152
140- Lets split up the largest test files into separate modules for better organization:
153+ While maintaining correctness.
141154
142155``` python
143- print (" \n 📦 Splitting Test Files" )
156+ filename = ' tests/test_path.py'
157+ print (f " 📦 Splitting Test File: { filename} " )
144158print (" =" * 50 )
145159
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" )
166187
167188# Commit changes to disk
168189codebase.commit()
169-
170190```
171191
172192<Warning >
@@ -299,14 +319,15 @@ if base_class:
299319 Understand key concepts like working with files, functions, imports, and the
300320 call graph to effectively manipulate code.
301321 </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 >
302325 <Card
303326 title = " Integrate with AI Tools"
304- icon = " robot "
327+ icon = " microchip "
305328 href = " /introduction/work-with-ai"
306329 >
307330 Learn how to use Codegen with Cursor, Devin, Windsurf, and more.
308331 </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+
312333</CardGroup >
0 commit comments