-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed_playgrounds.py
More file actions
79 lines (57 loc) · 2.28 KB
/
seed_playgrounds.py
File metadata and controls
79 lines (57 loc) · 2.28 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
#!/usr/bin/env python3
"""
Seed playground data from JSON files to database.
Run after scrape_playgrounds.py
"""
import json
import sys
from pathlib import Path
# Add parent to path for imports
sys.path.insert(0, str(Path(__file__).parent.parent))
from app.database import SessionLocal, engine
from app.models.db import Problem, Base
PLAYGROUND_DIR = Path("d:/PythonProject/deepml/problems_with_playground")
def seed_playgrounds():
"""Seed playground data into existing problem records."""
# Create tables if needed
Base.metadata.create_all(bind=engine)
db = SessionLocal()
try:
# Load summary to get list of problems with playgrounds
summary_file = PLAYGROUND_DIR / "summary.json"
if not summary_file.exists():
print(f"Error: {summary_file} not found. Run scrape_playgrounds.py first.")
return
with open(summary_file, "r", encoding="utf-8") as f:
summary = json.load(f)
print(f"Found {summary['total_with_playground']} problems with playground")
print("=" * 50)
updated = 0
not_found = []
for pg_info in summary["problems"]:
problem_id = pg_info["id"]
# Load playground JSON
pg_file = PLAYGROUND_DIR / f"playground_{problem_id:04d}.json"
if not pg_file.exists():
print(f" Warning: {pg_file} not found")
continue
with open(pg_file, "r", encoding="utf-8") as f:
pg_data = json.load(f)
# Find problem in database
problem = db.query(Problem).filter(Problem.id == problem_id).first()
if problem:
problem.playground_enabled = True
problem.playground_code = pg_data.get("code", "")
updated += 1
print(f" Updated #{problem_id}: {pg_info['title'][:40]}...")
else:
not_found.append(problem_id)
db.commit()
print("=" * 50)
print(f"Updated: {updated} problems")
if not_found:
print(f"Not found in DB: {not_found}")
finally:
db.close()
if __name__ == "__main__":
seed_playgrounds()