-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck_rounds.py
More file actions
251 lines (196 loc) Β· 9.72 KB
/
check_rounds.py
File metadata and controls
251 lines (196 loc) Β· 9.72 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env python3
"""
Comprehensive validation script for Hacker Jeopardy rounds
Checks JSON validity, image references, and data consistency
"""
import json
import os
from pathlib import Path
def validate_json_file(filepath):
"""Validate that a JSON file is well-formed"""
try:
with open(filepath, 'r', encoding='utf-8') as f:
json.load(f)
return True, None
except json.JSONDecodeError as e:
return False, str(e)
except Exception as e:
return False, f"File read error: {e}"
def validate_round_structure(round_data, round_name):
"""Validate round.json structure"""
issues = []
if not isinstance(round_data, dict):
issues.append(f"{round_name}/round.json: Root must be an object")
return issues
# Check required fields
required_fields = ['name', 'categories']
for field in required_fields:
if field not in round_data:
issues.append(f"{round_name}/round.json: Missing required field '{field}'")
# Validate categories
if 'categories' in round_data:
categories = round_data['categories']
if not isinstance(categories, list):
issues.append(f"{round_name}/round.json: 'categories' must be an array")
elif len(categories) == 0:
issues.append(f"{round_name}/round.json: 'categories' array is empty")
else:
for cat in categories:
if not isinstance(cat, str):
issues.append(f"{round_name}/round.json: Category '{cat}' must be a string")
return issues
def validate_category_structure(cat_data, round_name, cat_name):
"""Validate cat.json structure"""
issues = []
if not isinstance(cat_data, dict):
issues.append(f"{round_name}/{cat_name}/cat.json: Root must be an object")
return issues
# Check required fields
required_fields = ['name', 'questions']
for field in required_fields:
if field not in cat_data:
issues.append(f"{round_name}/{cat_name}/cat.json: Missing required field '{field}'")
# Validate questions
if 'questions' in cat_data:
questions = cat_data['questions']
if not isinstance(questions, list):
issues.append(f"{round_name}/{cat_name}/cat.json: 'questions' must be an array")
elif len(questions) == 0:
issues.append(f"{round_name}/{cat_name}/cat.json: 'questions' array is empty")
else:
for i, question in enumerate(questions):
question_issues = validate_question_structure(question, round_name, cat_name, i)
issues.extend(question_issues)
return issues
def validate_question_structure(question, round_name, cat_name, question_index):
"""Validate individual question structure"""
issues = []
if not isinstance(question, dict):
issues.append(f"{round_name}/{cat_name}/cat.json: Question {question_index + 1} must be an object")
return issues
# Check for question field
if 'question' not in question:
issues.append(f"{round_name}/{cat_name}/cat.json: Question {question_index + 1} missing 'question' field")
elif not isinstance(question['question'], str):
issues.append(f"{round_name}/{cat_name}/cat.json: Question {question_index + 1} 'question' must be a string")
# Check for answer or image (one must be present)
has_answer = 'answer' in question
has_image = 'image' in question
if not has_answer and not has_image:
issues.append(f"{round_name}/{cat_name}/cat.json: Question {question_index + 1} must have either 'answer' or 'image'")
elif has_answer and has_image:
issues.append(f"{round_name}/{cat_name}/cat.json: Question {question_index + 1} cannot have both 'answer' and 'image'")
if has_answer and not isinstance(question['answer'], str):
issues.append(f"{round_name}/{cat_name}/cat.json: Question {question_index + 1} 'answer' must be a string")
if has_image and not isinstance(question['image'], str):
issues.append(f"{round_name}/{cat_name}/cat.json: Question {question_index + 1} 'image' must be a string")
return issues
def check_round_consistency():
"""Comprehensive validation of all round files"""
assets_path = Path("src/assets")
issues = []
print("π Performing comprehensive round validation...\n")
# Check round directories in assets
round_dirs = [d for d in assets_path.iterdir() if d.is_dir() and not d.name.startswith('.')]
for round_dir in round_dirs:
round_name = round_dir.name
print(f"π Validating round: {round_name}")
# Check for round.json
round_json = round_dir / "round.json"
if not round_json.exists():
# Skip archive directories that don't have proper structure
if round_name.lower() in ['archiv']:
print(f" βοΈ Skipping archive directory: {round_name}")
continue
issues.append(f"Missing round.json in {round_name}")
continue
# Validate round.json
valid, error = validate_json_file(round_json)
if not valid:
issues.append(f"Invalid JSON in {round_name}/round.json: {error}")
continue
with open(round_json, 'r', encoding='utf-8') as f:
round_data = json.load(f)
# Validate round structure
round_issues = validate_round_structure(round_data, round_name)
issues.extend(round_issues)
if round_issues:
continue # Skip category validation if round.json is invalid
categories = round_data.get('categories', [])
print(f" π {len(categories)} categories: {', '.join(categories[:3])}{'...' if len(categories) > 3 else ''}")
# Check each category
for category in categories:
cat_dir = round_dir / category
cat_json = cat_dir / "cat.json"
if not cat_dir.exists():
issues.append(f"Missing category directory: {round_name}/{category}")
continue
if not cat_json.exists():
issues.append(f"Missing cat.json: {round_name}/{category}/cat.json")
continue
# Validate cat.json
valid, error = validate_json_file(cat_json)
if not valid:
issues.append(f"Invalid JSON in {round_name}/{category}/cat.json: {error}")
continue
with open(cat_json, 'r', encoding='utf-8') as f:
cat_data = json.load(f)
# Validate category structure
cat_issues = validate_category_structure(cat_data, round_name, category)
issues.extend(cat_issues)
questions = cat_data.get('questions', [])
print(f" π {category}: {len(questions)} questions")
# Check each question for image references
for i, question in enumerate(questions):
if 'image' in question:
image_path = cat_dir / question['image']
if not image_path.exists():
issues.append(f"Missing image: {round_name}/{category}/{question['image']} (question {i+1})")
# Check JSON files in cats/hackerjeopardy
print(f"\nπ Validating jeopardy prompt files...")
cats_path = Path("cats/hackerjeopardy")
json_files = list(cats_path.glob("jeopardy_prompts_*.json"))
for json_file in json_files:
valid, error = validate_json_file(json_file)
if not valid:
issues.append(f"Invalid JSON in {json_file.name}: {error}")
continue
try:
with open(json_file, 'r', encoding='utf-8') as f:
data = json.load(f)
questions = data.get('questions', [])
name = data.get('name', json_file.stem.replace('jeopardy_prompts_', ''))
# Check for images in these files (they shouldn't have any)
has_images = any('image' in q for q in questions)
if has_images:
issues.append(f"Unexpected images found in {json_file.name}")
# Check question count (these should have exactly 5)
if len(questions) != 5:
issues.append(f"{json_file.name}: Expected 5 questions, found {len(questions)}")
# Validate question structure
for i, question in enumerate(questions):
if not isinstance(question, dict):
issues.append(f"{json_file.name}: Question {i+1} must be an object")
continue
if 'question' not in question or 'answer' not in question:
issues.append(f"{json_file.name}: Question {i+1} missing required fields")
if 'image' in question:
issues.append(f"{json_file.name}: Question {i+1} should not have image field")
print(f" β
{name}: {len(questions)} questions")
except Exception as e:
issues.append(f"Error processing {json_file.name}: {e}")
return issues
if __name__ == "__main__":
print("π Starting comprehensive Hacker Jeopardy round validation...\n")
issues = check_round_consistency()
print(f"\nπ Validation Results:")
print(f"Total rounds checked: {len([d for d in Path('src/assets').iterdir() if d.is_dir() and not d.name.startswith('.')])}")
print(f"Total prompt files checked: {len(list(Path('cats/hackerjeopardy').glob('jeopardy_prompts_*.json')))}")
if issues:
print(f"\nβ Found {len(issues)} issues:")
for issue in issues:
print(f" β’ {issue}")
print(f"\nβ οΈ Please fix the above issues before deployment.")
else:
print("\nβ
All validations passed! All rounds are consistent, JSON is valid, and all referenced images exist.")
print(f"\nπ Summary: {len(issues)} issues detected")