-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed_problems.py
More file actions
170 lines (128 loc) · 5.99 KB
/
seed_problems.py
File metadata and controls
170 lines (128 loc) · 5.99 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
"""
Seed script to import problems from JSON files into database.
Run once: python seed_problems.py
"""
import json
import sys
from pathlib import Path
# Add parent to path for imports
sys.path.insert(0, str(Path(__file__).parent))
from app.database import engine, SessionLocal, Base
from app.models.db import Problem
# Path to problem JSON files
PROBLEMS_DIR = Path("d:/PythonProject/deepml/problems")
def seed_problems():
"""Import all problems from JSON files into database."""
# Create tables if not exist
Base.metadata.create_all(bind=engine)
db = SessionLocal()
try:
# Get existing problem IDs
existing_ids = {p.id for p in db.query(Problem.id).all()}
print(f"[*] Found {len(existing_ids)} existing problems in database")
# Read all problem files
problem_files = sorted(PROBLEMS_DIR.glob("problem_*.json"))
print(f"[*] Found {len(problem_files)} problem files")
imported = 0
skipped = 0
for problem_file in problem_files:
try:
# Extract ID from filename (problem_0001.json -> 1)
problem_id = int(problem_file.stem.split("_")[1])
# Skip if already exists
if problem_id in existing_ids:
skipped += 1
continue
with open(problem_file, "r", encoding="utf-8") as f:
data = json.load(f)
# Create Problem record
problem = Problem(
id=problem_id,
title=data.get("title", f"Problem {problem_id}"),
category=data.get("category", "Unknown"),
difficulty=data.get("difficulty", "medium"),
description=data.get("description", ""),
starter_code=data.get("starter_code", ""),
example=json.dumps(data.get("example")) if data.get("example") else None,
test_cases=json.dumps(data.get("test_cases", [])),
learn_section=data.get("learn_section", data.get("learn", "")),
video=json.dumps(data.get("video")) if isinstance(data.get("video"), list) else data.get("video"),
pytorch_starter_code=data.get("pytorch_starter_code"),
pytorch_test_cases=json.dumps(data.get("pytorch_test_cases")) if data.get("pytorch_test_cases") else None,
tinygrad_starter_code=data.get("tinygrad_starter_code"),
tinygrad_test_cases=json.dumps(data.get("tinygrad_test_cases")) if data.get("tinygrad_test_cases") else None,
cuda_starter_code=data.get("cuda_starter_code"),
cuda_test_cases=json.dumps(data.get("cuda_test_cases")) if data.get("cuda_test_cases") else None,
)
db.add(problem)
db.commit() # Commit each record individually for SQLite
imported += 1
if imported % 50 == 0:
print(f" [+] Imported {imported} problems...")
except Exception as e:
db.rollback()
print(f" [!] Error importing {problem_file.name}: {e}")
continue
# Final commit
db.commit()
print(f"\n[+] Import complete!")
print(f" Imported: {imported}")
print(f" Skipped (existing): {skipped}")
print(f" Total in database: {len(existing_ids) + imported}")
finally:
db.close()
def seed_quests():
"""Import existing quest JSON files into database."""
from app.models.db import Quest
QUESTS_DIR = Path("d:/PythonProject/deepml/quests")
db = SessionLocal()
try:
# Get existing quest problem IDs
existing_ids = {q.problem_id for q in db.query(Quest.problem_id).all()}
print(f"[*] Found {len(existing_ids)} existing quests in database")
quest_files = sorted(QUESTS_DIR.glob("quest_*.json"))
print(f"[*] Found {len(quest_files)} quest files")
imported = 0
skipped = 0
for quest_file in quest_files:
try:
problem_id = int(quest_file.stem.split("_")[1])
if problem_id in existing_ids:
skipped += 1
continue
with open(quest_file, "r", encoding="utf-8") as f:
data = json.load(f)
# Extract quest metadata for logging
title = data.get("title", "Unknown")
category = data.get("category", "Unknown")
sub_quests = data.get("sub_quests", [])
num_sub_quests = len(sub_quests)
quest = Quest(
problem_id=problem_id,
data=json.dumps(data)
)
db.add(quest)
db.commit() # Commit each record individually
imported += 1
# Log each imported quest with details
print(f" [+] #{problem_id:04d} | {category[:20]:<20} | {title[:40]:<40} | {num_sub_quests} sub-quests")
except Exception as e:
db.rollback()
print(f" [!] Error importing {quest_file.name}: {e}")
continue
db.commit()
print(f"\n[+] Quest import complete!")
print(f" Imported: {imported}")
print(f" Skipped: {skipped}")
finally:
db.close()
if __name__ == "__main__":
print("=" * 60)
print("Problem & Quest Database Seeder")
print("=" * 60)
print("\n[1] Seeding Problems...")
seed_problems()
print("\n[2] Seeding Quests...")
seed_quests()
print("\n" + "=" * 60)
print("Done!")