Skip to content

Commit f2f5913

Browse files
format a bunch of fiels
1 parent 6006e9d commit f2f5913

File tree

18 files changed

+79
-53
lines changed

18 files changed

+79
-53
lines changed

flask_to_fastapi_migration/input_repo/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,26 @@
1111
authors = ["Author A", "Author B", "Author C"]
1212
categories = ["Fiction", "Non-Fiction", "Biography"]
1313

14+
1415
# Home Page
1516
@app.route("/")
1617
def home():
1718
return render_template("index.html")
1819

20+
1921
# Books Page
2022
@app.route("/books", methods=["GET"])
2123
def get_books():
2224
return render_template("books.html", books=books)
2325

26+
2427
@app.route("/books", methods=["POST"])
2528
def add_book():
2629
data = request.json
2730
books.append(data)
2831
return jsonify(data), 201
2932

33+
3034
@app.route("/books/<int:book_id>", methods=["PUT"])
3135
def update_book(book_id):
3236
data = request.json
@@ -36,33 +40,39 @@ def update_book(book_id):
3640
return jsonify(book)
3741
return jsonify({"error": "Book not found"}), 404
3842

43+
3944
@app.route("/books/<int:book_id>", methods=["DELETE"])
4045
def delete_book(book_id):
4146
global books
4247
books = [book for book in books if book["id"] != book_id]
4348
return jsonify({"message": "Book deleted"})
4449

50+
4551
# Authors Page
4652
@app.route("/authors", methods=["GET"])
4753
def get_authors():
4854
return render_template("authors.html", authors=authors)
4955

56+
5057
@app.route("/authors", methods=["POST"])
5158
def add_author():
5259
data = request.json
5360
authors.append(data["name"])
5461
return jsonify({"name": data["name"]}), 201
5562

63+
5664
# Categories Page
5765
@app.route("/categories", methods=["GET"])
5866
def get_categories():
5967
return render_template("categories.html", categories=categories)
6068

69+
6170
@app.route("/categories", methods=["POST"])
6271
def add_category():
6372
data = request.json
6473
categories.append(data["name"])
6574
return jsonify({"name": data["name"]}), 201
6675

76+
6777
if __name__ == "__main__":
6878
app.run(debug=True)

freezegun_to_timemachine_migration/run.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from codegen import Codebase
2+
23
codebase = Codebase.from_repo("getmoto/moto", commit="786a8ada7ed0c7f9d8b04d49f24596865e4b7901")
34

45
print("🚀 Starting FreezeGun to TimeMachine conversion...")
@@ -9,32 +10,31 @@
910
print(f"📝 Processing: {file.filepath}")
1011
# Update imports
1112
for imp in file.imports:
12-
if imp.symbol_name and 'freezegun' in imp.source:
13-
if imp.name == 'freeze_time':
13+
if imp.symbol_name and "freezegun" in imp.source:
14+
if imp.name == "freeze_time":
1415
# required due to Codegen limitations
15-
imp.edit('from time_machine import travel')
16+
imp.edit("from time_machine import travel")
1617
else:
17-
imp.set_import_module('time_machine')
18+
imp.set_import_module("time_machine")
1819
# Find all function calls in the file
1920
for fcall in file.function_calls:
2021
# Skip if not a freeze_time call
21-
if 'freeze_time' not in fcall.source:
22+
if "freeze_time" not in fcall.source:
2223
continue
2324
# Get original source and prepare new source
2425
new_source = fcall.source
2526
# Add tick parameter if not present
26-
if not fcall.get_arg_by_parameter_name('tick'):
27-
if new_source.endswith(')'):
27+
if not fcall.get_arg_by_parameter_name("tick"):
28+
if new_source.endswith(")"):
2829
new_source = new_source[:-1]
29-
if not new_source.endswith('('):
30-
new_source += ','
31-
new_source += ' tick=False)'
30+
if not new_source.endswith("("):
31+
new_source += ","
32+
new_source += " tick=False)"
3233
# Replace freeze_time with travel
33-
if '.' in new_source:
34-
new_source = new_source.replace(
35-
'freeze_time', 'travel').replace('freezegun', 'time_machine')
34+
if "." in new_source:
35+
new_source = new_source.replace("freeze_time", "travel").replace("freezegun", "time_machine")
3636
else:
37-
new_source = 'travel' + new_source[len('freeze_time'):]
37+
new_source = "travel" + new_source[len("freeze_time") :]
3838
# Make single edit with complete changes
3939
fcall.edit(new_source)
4040
codebase.commit()

generate_training_data/run.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,8 @@ def run(codebase: Codebase):
8181
# Update metadata
8282
training_data["metadata"]["total_processed"] = len(training_data["functions"])
8383
if training_data["functions"]:
84-
training_data["metadata"]["avg_dependencies"] = sum(
85-
len(f["dependencies"]) for f in training_data["functions"]
86-
) / len(training_data["functions"])
87-
training_data["metadata"]["avg_usages"] = sum(
88-
len(f["usages"]) for f in training_data["functions"]
89-
) / len(training_data["functions"])
84+
training_data["metadata"]["avg_dependencies"] = sum(len(f["dependencies"]) for f in training_data["functions"]) / len(training_data["functions"])
85+
training_data["metadata"]["avg_usages"] = sum(len(f["usages"]) for f in training_data["functions"]) / len(training_data["functions"])
9086

9187
# Print stats
9288
print(f"Processed {training_data['metadata']['total_processed']} functions")

sqlalchemy_1.6_to_2.0/input_repo/main.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
app = FastAPI()
1111
models.Base.metadata.create_all(bind=engine)
1212

13+
1314
# Dependency for the database session
1415
def get_db():
1516
db = SessionLocal()
@@ -18,15 +19,18 @@ def get_db():
1819
finally:
1920
db.close()
2021

22+
2123
# Utility Functions
2224
def get_book_or_404(book_id: int, db: Session):
2325
book = db.query(models.Book).filter(models.Book.id == book_id).first()
2426
if not book:
2527
raise HTTPException(status_code=404, detail="Book not found")
2628
return book
2729

30+
2831
# CRUD Operations
2932

33+
3034
@app.post("/books/", response_model=schemas.Book)
3135
def create_book(book: schemas.BookCreate, db: Session = Depends(get_db)):
3236
db_book = models.Book(**book.dict())
@@ -35,18 +39,21 @@ def create_book(book: schemas.BookCreate, db: Session = Depends(get_db)):
3539
db.refresh(db_book)
3640
return db_book
3741

42+
3843
@app.get("/books/", response_model=List[schemas.Book])
3944
def read_books(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)):
4045
books = db.query(models.Book).offset(skip).limit(limit).all()
4146
return books
4247

48+
4349
@app.get("/books/{book_id}", response_model=schemas.Book)
4450
def read_book(book_id: int, db: Session = Depends(get_db)):
4551
book = db.query(models.Book).filter(models.Book.id == book_id).first()
4652
if book is None:
4753
raise HTTPException(status_code=404, detail="Book not found")
4854
return book
4955

56+
5057
@app.put("/books/{book_id}", response_model=schemas.Book)
5158
def update_book(book_id: int, book: schemas.BookCreate, db: Session = Depends(get_db)):
5259
db_book = db.query(models.Book).filter(models.Book.id == book_id).first()
@@ -58,6 +65,7 @@ def update_book(book_id: int, book: schemas.BookCreate, db: Session = Depends(ge
5865
db.refresh(db_book)
5966
return db_book
6067

68+
6169
@app.delete("/books/{book_id}", response_model=schemas.Book)
6270
def delete_book(book_id: int, db: Session = Depends(get_db)):
6371
db_book = db.query(models.Book).filter(models.Book.id == book_id).first()
@@ -67,6 +75,7 @@ def delete_book(book_id: int, db: Session = Depends(get_db)):
6775
db.commit()
6876
return db_book
6977

78+
7079
@app.post("/publishers/", response_model=schemas.Publisher)
7180
def create_publisher(publisher: schemas.PublisherCreate, db: Session = Depends(get_db)):
7281
db_publisher = models.Publisher(**publisher.dict())
@@ -75,18 +84,21 @@ def create_publisher(publisher: schemas.PublisherCreate, db: Session = Depends(g
7584
db.refresh(db_publisher)
7685
return db_publisher
7786

87+
7888
@app.get("/publishers/", response_model=List[schemas.Publisher])
7989
def read_publishers(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)):
8090
publishers = db.query(models.Publisher).offset(skip).limit(limit).all()
8191
return publishers
8292

93+
8394
@app.get("/publishers/{publisher_id}", response_model=schemas.Publisher)
8495
def read_publisher(publisher_id: int, db: Session = Depends(get_db)):
8596
publisher = db.query(models.Publisher).filter(models.Publisher.id == publisher_id).first()
8697
if not publisher:
8798
raise HTTPException(status_code=404, detail="Publisher not found")
8899
return publisher
89100

101+
90102
@app.put("/publishers/{publisher_id}", response_model=schemas.Publisher)
91103
def update_publisher(publisher_id: int, publisher: schemas.PublisherCreate, db: Session = Depends(get_db)):
92104
db_publisher = db.query(models.Publisher).filter(models.Publisher.id == publisher_id).first()
@@ -98,6 +110,7 @@ def update_publisher(publisher_id: int, publisher: schemas.PublisherCreate, db:
98110
db.refresh(db_publisher)
99111
return db_publisher
100112

113+
101114
@app.delete("/publishers/{publisher_id}", response_model=schemas.Publisher)
102115
def delete_publisher(publisher_id: int, db: Session = Depends(get_db)):
103116
db_publisher = db.query(models.Publisher).filter(models.Publisher.id == publisher_id).first()
@@ -106,4 +119,3 @@ def delete_publisher(publisher_id: int, db: Session = Depends(get_db)):
106119
db.delete(db_publisher)
107120
db.commit()
108121
return db_publisher
109-

sqlalchemy_1.6_to_2.0/input_repo/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from sqlalchemy.orm import relationship
33
from database import Base
44

5+
56
class Publisher(Base):
67
__tablename__ = "publishers"
78

sqlalchemy_1.6_to_2.0/input_repo/schemas.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
from pydantic import BaseModel
22
from typing import List, Optional
33

4+
45
class PublisherBase(BaseModel):
56
name: str
67

8+
79
class PublisherCreate(PublisherBase):
810
pass
911

12+
1013
class Publisher(PublisherBase):
1114
id: int
1215
books: List["Book"] = []
1316

1417
class Config:
1518
orm_mode = True
1619

20+
1721
class BookBase(BaseModel):
1822
title: str
1923
author: str
2024
description: str
2125
publisher_id: Optional[int]
2226

27+
2328
class BookCreate(BookBase):
2429
pass
2530

31+
2632
class Book(BookBase):
2733
id: int
2834
publisher: Optional[Publisher]

sqlalchemy_1.6_to_2.0/output_repo/main.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
app = FastAPI()
1111
models.Base.metadata.create_all(bind=engine)
1212

13+
1314
# Dependency for the database session
1415
def get_db():
1516
db = SessionLocal()
@@ -18,8 +19,10 @@ def get_db():
1819
finally:
1920
db.close()
2021

22+
2123
# CRUD Operations
2224

25+
2326
@app.post("/books/", response_model=schemas.Book)
2427
def create_book(book: schemas.BookCreate, db: Session = Depends(get_db)):
2528
db_book = models.Book(**book.dict())
@@ -28,18 +31,21 @@ def create_book(book: schemas.BookCreate, db: Session = Depends(get_db)):
2831
db.refresh(db_book)
2932
return db_book
3033

34+
3135
@app.get("/books/", response_model=List[schemas.Book])
3236
def read_books(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)):
3337
books = db.query()(models.Book).offset(skip).limit(limit).scalars().all()
3438
return books
3539

40+
3641
@app.get("/books/{book_id}", response_model=schemas.Book)
3742
def read_book(book_id: int, db: Session = Depends(get_db)):
3843
book = db.query()(models.Book).where(models.Book.id == book_id).first()
3944
if book is None:
4045
raise HTTPException(status_code=404, detail="Book not found")
4146
return book
4247

48+
4349
@app.put("/books/{book_id}", response_model=schemas.Book)
4450
def update_book(book_id: int, book: schemas.BookCreate, db: Session = Depends(get_db)):
4551
db_book = db.query()(models.Book).where(models.Book.id == book_id).first()
@@ -51,6 +57,7 @@ def update_book(book_id: int, book: schemas.BookCreate, db: Session = Depends(ge
5157
db.refresh(db_book)
5258
return db_book
5359

60+
5461
@app.delete("/books/{book_id}", response_model=schemas.Book)
5562
def delete_book(book_id: int, db: Session = Depends(get_db)):
5663
db_book = db.query()(models.Book).where(models.Book.id == book_id).first()
@@ -60,6 +67,7 @@ def delete_book(book_id: int, db: Session = Depends(get_db)):
6067
db.commit()
6168
return db_book
6269

70+
6371
@app.post("/publishers/", response_model=schemas.Publisher)
6472
def create_publisher(publisher: schemas.PublisherCreate, db: Session = Depends(get_db)):
6573
db_publisher = models.Publisher(**publisher.dict())
@@ -68,18 +76,21 @@ def create_publisher(publisher: schemas.PublisherCreate, db: Session = Depends(g
6876
db.refresh(db_publisher)
6977
return db_publisher
7078

79+
7180
@app.get("/publishers/", response_model=List[schemas.Publisher])
7281
def read_publishers(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)):
7382
publishers = db.query()(models.Publisher).offset(skip).limit(limit).scalars().all()
7483
return publishers
7584

85+
7686
@app.get("/publishers/{publisher_id}", response_model=schemas.Publisher)
7787
def read_publisher(publisher_id: int, db: Session = Depends(get_db)):
7888
publisher = db.query()(models.Publisher).where(models.Publisher.id == publisher_id).first()
7989
if not publisher:
8090
raise HTTPException(status_code=404, detail="Publisher not found")
8191
return publisher
8292

93+
8394
@app.put("/publishers/{publisher_id}", response_model=schemas.Publisher)
8495
def update_publisher(publisher_id: int, publisher: schemas.PublisherCreate, db: Session = Depends(get_db)):
8596
db_publisher = db.query()(models.Publisher).where(models.Publisher.id == publisher_id).first()
@@ -91,6 +102,7 @@ def update_publisher(publisher_id: int, publisher: schemas.PublisherCreate, db:
91102
db.refresh(db_publisher)
92103
return db_publisher
93104

105+
94106
@app.delete("/publishers/{publisher_id}", response_model=schemas.Publisher)
95107
def delete_publisher(publisher_id: int, db: Session = Depends(get_db)):
96108
db_publisher = db.query()(models.Publisher).where(models.Publisher.id == publisher_id).first()
@@ -99,4 +111,3 @@ def delete_publisher(publisher_id: int, db: Session = Depends(get_db)):
99111
db.delete(db_publisher)
100112
db.commit()
101113
return db_publisher
102-

0 commit comments

Comments
 (0)