prism-code-workspace 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- prism_code_workspace-1.0.0.dist-info/METADATA +272 -0
- prism_code_workspace-1.0.0.dist-info/RECORD +30 -0
- prism_code_workspace-1.0.0.dist-info/WHEEL +4 -0
- prism_code_workspace-1.0.0.dist-info/entry_points.txt +3 -0
- prism_code_workspace-1.0.0.dist-info/licenses/LICENSE +21 -0
- python_app/__init__.py +3 -0
- python_app/ai.py +225 -0
- python_app/analysis.py +953 -0
- python_app/compare.py +685 -0
- python_app/compare_engine.py +284 -0
- python_app/compiled_analysis.py +727 -0
- python_app/desktop.py +181 -0
- python_app/function_pov/__init__.py +16 -0
- python_app/function_pov/analyzer.py +464 -0
- python_app/function_pov/graph.py +50 -0
- python_app/function_pov/models.py +53 -0
- python_app/function_pov/pov/__init__.py +17 -0
- python_app/function_pov/pov/engine.py +1630 -0
- python_app/function_pov/pov/exercises.py +1263 -0
- python_app/function_pov_ui.py +1749 -0
- python_app/i18n.py +570 -0
- python_app/installer.nsi +70 -0
- python_app/installer_runtime.nsi +70 -0
- python_app/js_analysis.py +709 -0
- python_app/launch.py +6 -0
- python_app/launch_native.py +14 -0
- python_app/metrics_advisor.py +79 -0
- python_app/native_app.py +1709 -0
- python_app/requirements.txt +7 -0
- python_app/server.py +453 -0
|
@@ -0,0 +1,1749 @@
|
|
|
1
|
+
"""Native PySide6 interface for the restricted Function POV simulator."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import math
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from PySide6.QtCore import Qt, QTimer, Signal
|
|
11
|
+
from PySide6.QtGui import QColor, QFontDatabase, QPainter, QTextCursor
|
|
12
|
+
from PySide6.QtWidgets import (
|
|
13
|
+
QFormLayout,
|
|
14
|
+
QFrame,
|
|
15
|
+
QButtonGroup,
|
|
16
|
+
QHBoxLayout,
|
|
17
|
+
QLabel,
|
|
18
|
+
QLineEdit,
|
|
19
|
+
QListWidget,
|
|
20
|
+
QListWidgetItem,
|
|
21
|
+
QPlainTextEdit,
|
|
22
|
+
QProgressBar,
|
|
23
|
+
QPushButton,
|
|
24
|
+
QScrollArea,
|
|
25
|
+
QSplitter,
|
|
26
|
+
QStackedWidget,
|
|
27
|
+
QTextEdit,
|
|
28
|
+
QVBoxLayout,
|
|
29
|
+
QWidget,
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
from python_app.function_pov.analyzer import analyze_source
|
|
33
|
+
from python_app.function_pov.models import AnalysisResult, FunctionInfo
|
|
34
|
+
from python_app.function_pov.pov.engine import (
|
|
35
|
+
MAX_INTEGER_BITS,
|
|
36
|
+
PovSimulation,
|
|
37
|
+
SimulationError,
|
|
38
|
+
build_program_simulation_from_source,
|
|
39
|
+
build_simulation_from_source,
|
|
40
|
+
)
|
|
41
|
+
from python_app.function_pov.pov.exercises import (
|
|
42
|
+
ExerciseGenerationError,
|
|
43
|
+
GeneratedExercise,
|
|
44
|
+
PracticeExerciseGenerator,
|
|
45
|
+
)
|
|
46
|
+
from python_app.i18n import simulation_error_text, tr
|
|
47
|
+
|
|
48
|
+
MAX_JSON_INPUT_CHARS = 10000
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _parse_json_integer(token: str) -> int:
|
|
52
|
+
digits = token[1:] if token.startswith("-") else token
|
|
53
|
+
if len(digits) > 78:
|
|
54
|
+
raise ValueError("JSON integer is too large.")
|
|
55
|
+
value = int(token)
|
|
56
|
+
if value.bit_length() > MAX_INTEGER_BITS:
|
|
57
|
+
raise ValueError("JSON integer is too large.")
|
|
58
|
+
return value
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _parse_json_float(token: str) -> float:
|
|
62
|
+
value = float(token)
|
|
63
|
+
if not math.isfinite(value):
|
|
64
|
+
raise ValueError("JSON number is not finite.")
|
|
65
|
+
return value
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _reject_json_constant(token: str) -> None:
|
|
69
|
+
raise ValueError("Invalid JSON number: {}.".format(token))
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _decode_json_input(raw: str) -> tuple[Any, str | None]:
|
|
73
|
+
if len(raw) > MAX_JSON_INPUT_CHARS:
|
|
74
|
+
return None, "too_long"
|
|
75
|
+
try:
|
|
76
|
+
return json.loads(
|
|
77
|
+
raw,
|
|
78
|
+
parse_int=_parse_json_integer,
|
|
79
|
+
parse_float=_parse_json_float,
|
|
80
|
+
parse_constant=_reject_json_constant,
|
|
81
|
+
), None
|
|
82
|
+
except json.JSONDecodeError:
|
|
83
|
+
return None, "invalid"
|
|
84
|
+
except (ValueError, RecursionError, OverflowError):
|
|
85
|
+
return None, "unsafe"
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class PixelMascot(QWidget):
|
|
89
|
+
def __init__(self, size: int, celebrating: bool = False, parent=None):
|
|
90
|
+
super().__init__(parent)
|
|
91
|
+
self.setFixedSize(size, size)
|
|
92
|
+
self.celebrating = celebrating
|
|
93
|
+
self._phase = False
|
|
94
|
+
self._timer = QTimer(self)
|
|
95
|
+
self._timer.setInterval(420)
|
|
96
|
+
self._timer.timeout.connect(self._tick)
|
|
97
|
+
self._timer.start()
|
|
98
|
+
|
|
99
|
+
def _tick(self) -> None:
|
|
100
|
+
self._phase = not self._phase
|
|
101
|
+
self.update()
|
|
102
|
+
|
|
103
|
+
def paintEvent(self, event) -> None:
|
|
104
|
+
painter = QPainter(self)
|
|
105
|
+
painter.setRenderHint(QPainter.RenderHint.Antialiasing, False)
|
|
106
|
+
cell = max(2, min(self.width(), self.height()) // 16)
|
|
107
|
+
origin_x = (self.width() - cell * 16) // 2
|
|
108
|
+
origin_y = (self.height() - cell * 16) // 2
|
|
109
|
+
bob = 1 if self._phase else 0
|
|
110
|
+
|
|
111
|
+
def pixel(x: int, y: int, width: int, height: int, color: str) -> None:
|
|
112
|
+
painter.fillRect(
|
|
113
|
+
origin_x + x * cell,
|
|
114
|
+
origin_y + (y + bob) * cell,
|
|
115
|
+
width * cell,
|
|
116
|
+
height * cell,
|
|
117
|
+
QColor(color),
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
pixel(6, 1, 4, 1, "#2f67cb")
|
|
121
|
+
pixel(4, 2, 8, 2, "#2f67cb")
|
|
122
|
+
pixel(2, 4, 12, 7, "#2f67cb")
|
|
123
|
+
pixel(3, 10, 10, 3, "#2f67cb")
|
|
124
|
+
pixel(5, 13, 2, 2, "#2f67cb")
|
|
125
|
+
pixel(9, 13, 2, 2, "#2f67cb")
|
|
126
|
+
pixel(4, 3, 8, 2, "#a9d5ff")
|
|
127
|
+
pixel(3, 5, 10, 5, "#a9d5ff")
|
|
128
|
+
pixel(4, 10, 8, 2, "#a9d5ff")
|
|
129
|
+
pixel(5, 6, 2, 2, "#ffffff")
|
|
130
|
+
pixel(9, 6, 2, 2, "#ffffff")
|
|
131
|
+
pixel(6, 7, 1, 1, "#173b78")
|
|
132
|
+
pixel(10, 7, 1, 1, "#173b78")
|
|
133
|
+
pixel(7, 9, 2, 1, "#2f67cb")
|
|
134
|
+
pixel(12, 3, 1, 1, "#ff7852")
|
|
135
|
+
pixel(13, 2, 1, 1, "#ff7852")
|
|
136
|
+
pixel(14, 3, 1, 1, "#ff7852")
|
|
137
|
+
if self.celebrating and self._phase:
|
|
138
|
+
pixel(1, 3, 1, 1, "#62d6da")
|
|
139
|
+
pixel(14, 10, 1, 1, "#62d6da")
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
class FunctionPOVPage(QWidget):
|
|
143
|
+
backRequested = Signal()
|
|
144
|
+
|
|
145
|
+
def __init__(self, parent=None):
|
|
146
|
+
super().__init__(parent)
|
|
147
|
+
self.setObjectName("FunctionPOVPage")
|
|
148
|
+
self.setStyleSheet(
|
|
149
|
+
"""
|
|
150
|
+
QWidget#FunctionPOVPage { background: #1b1d21; }
|
|
151
|
+
QFrame#PovCard { background: #23262b; border: 1px solid #3b4149; border-radius: 10px; }
|
|
152
|
+
QLabel#PovKicker { color: #62d6da; font-size: 8pt; font-weight: 700; letter-spacing: 1px; }
|
|
153
|
+
QLabel#PovTitle { color: #f4f5f6; font-size: 22pt; font-weight: 700; }
|
|
154
|
+
QLabel#PovSubtitle { color: #aab2bd; }
|
|
155
|
+
QLabel#PovMuted { color: #aab2bd; }
|
|
156
|
+
QLabel#PovExpression { color: #9ce5e7; background: #171a1e; border: 1px solid #343a42; border-radius: 6px; padding: 8px; font-family: "Cascadia Code", "Consolas", monospace; }
|
|
157
|
+
QLabel#PovFeedback { color: #f3c178; background: #30291f; border: 1px solid #6b5434; border-radius: 6px; padding: 8px; }
|
|
158
|
+
QPlainTextEdit#PovSource, QListWidget#PovList { background: #17191d; border: 1px solid #34383f; border-radius: 7px; color: #dfe4ea; selection-background-color: #51402d; font-family: "Cascadia Code", "Consolas", monospace; }
|
|
159
|
+
QPushButton#PovPrimary { background: #ff7852; border: 1px solid #ff7852; border-radius: 6px; color: #201310; font-weight: 700; padding: 8px 14px; }
|
|
160
|
+
QPushButton#PovPrimary:hover { background: #ff8d6c; }
|
|
161
|
+
QPushButton#PovSecondary { background: #2b2f36; border: 1px solid #41464f; border-radius: 6px; color: #e9ecf0; padding: 7px 12px; }
|
|
162
|
+
QPushButton#PovSecondary:hover { border-color: #62d6da; }
|
|
163
|
+
QPushButton#PovMode { background: #2b2f36; border: 1px solid #41464f; border-radius: 6px; color: #e9ecf0; padding: 7px 14px; font-weight: 600; }
|
|
164
|
+
QPushButton#PovMode:checked { background: #51402d; border-color: #ff7852; color: #fff2df; }
|
|
165
|
+
QPushButton#PovMode:disabled { color: #777f89; }
|
|
166
|
+
QPushButton:disabled { color: #777f89; background: #25282d; border-color: #34383f; }
|
|
167
|
+
QProgressBar#PovProgress { background: #17191d; border: 1px solid #3b4149; border-radius: 4px; color: #cbd3dc; text-align: center; height: 14px; }
|
|
168
|
+
QProgressBar#PovProgress::chunk { background: #ff7852; border: 1px solid #62d6da; }
|
|
169
|
+
QFrame#PovCompletionCard { background: #23262b; border: 1px solid #ff7852; border-radius: 12px; }
|
|
170
|
+
"""
|
|
171
|
+
)
|
|
172
|
+
self._source = ""
|
|
173
|
+
self._filename = "main.py"
|
|
174
|
+
self._analysis: AnalysisResult | None = None
|
|
175
|
+
self._functions: dict[str, FunctionInfo] = {}
|
|
176
|
+
self._simulation: PovSimulation | None = None
|
|
177
|
+
self._original_simulation: PovSimulation | None = None
|
|
178
|
+
self._program_simulation: PovSimulation | None = None
|
|
179
|
+
self._program_error: str | None = None
|
|
180
|
+
self._mode = "function"
|
|
181
|
+
self._step_index = 0
|
|
182
|
+
self._passed: set[int] = set()
|
|
183
|
+
self._solved_by_attempt: set[int] = set()
|
|
184
|
+
self._revealed: set[int] = set()
|
|
185
|
+
self._history: dict[int, list[dict[str, Any]]] = {}
|
|
186
|
+
self._drafts: dict[int, str] = {}
|
|
187
|
+
self._parameter_fields: dict[str, QLineEdit] = {}
|
|
188
|
+
self._unsupported_language: str | None = None
|
|
189
|
+
self._completion_metrics: dict[str, Any] | None = None
|
|
190
|
+
self._exercise_generator = PracticeExerciseGenerator()
|
|
191
|
+
self._practice_context: tuple[str, str | None, dict[str, Any]] | None = None
|
|
192
|
+
self._pending_exercise: GeneratedExercise | None = None
|
|
193
|
+
self._active_exercise: GeneratedExercise | None = None
|
|
194
|
+
self._generation_status = ""
|
|
195
|
+
|
|
196
|
+
root = QVBoxLayout(self)
|
|
197
|
+
root.setContentsMargins(22, 16, 22, 18)
|
|
198
|
+
root.setSpacing(12)
|
|
199
|
+
nav = QHBoxLayout()
|
|
200
|
+
self.back_button = QPushButton()
|
|
201
|
+
self.back_button.setObjectName("PovSecondary")
|
|
202
|
+
self.back_button.clicked.connect(self.backRequested)
|
|
203
|
+
nav.addWidget(self.back_button)
|
|
204
|
+
self.nav_label = QLabel()
|
|
205
|
+
self.nav_label.setObjectName("PovKicker")
|
|
206
|
+
nav.addWidget(self.nav_label)
|
|
207
|
+
nav.addStretch(1)
|
|
208
|
+
root.addLayout(nav)
|
|
209
|
+
|
|
210
|
+
self.screens = QStackedWidget()
|
|
211
|
+
self._build_selection_screen()
|
|
212
|
+
self._build_practice_screen()
|
|
213
|
+
self._build_completion_screen()
|
|
214
|
+
self._build_exercise_screen()
|
|
215
|
+
root.addWidget(self.screens, 1)
|
|
216
|
+
self.retranslate()
|
|
217
|
+
|
|
218
|
+
def _build_selection_screen(self) -> None:
|
|
219
|
+
self.selection_screen = QWidget()
|
|
220
|
+
outer = QVBoxLayout(self.selection_screen)
|
|
221
|
+
outer.setContentsMargins(0, 0, 0, 0)
|
|
222
|
+
outer.setSpacing(14)
|
|
223
|
+
|
|
224
|
+
hero = QHBoxLayout()
|
|
225
|
+
self.entry_mascot = PixelMascot(112)
|
|
226
|
+
hero.addWidget(self.entry_mascot)
|
|
227
|
+
hero_copy = QVBoxLayout()
|
|
228
|
+
self.entry_kicker = QLabel()
|
|
229
|
+
self.entry_kicker.setObjectName("PovKicker")
|
|
230
|
+
hero_copy.addWidget(self.entry_kicker)
|
|
231
|
+
self.entry_title = QLabel()
|
|
232
|
+
self.entry_title.setObjectName("PovTitle")
|
|
233
|
+
hero_copy.addWidget(self.entry_title)
|
|
234
|
+
self.entry_subtitle = QLabel()
|
|
235
|
+
self.entry_subtitle.setObjectName("PovSubtitle")
|
|
236
|
+
self.entry_subtitle.setWordWrap(True)
|
|
237
|
+
hero_copy.addWidget(self.entry_subtitle)
|
|
238
|
+
hero_copy.addStretch(1)
|
|
239
|
+
hero.addLayout(hero_copy, 1)
|
|
240
|
+
outer.addLayout(hero)
|
|
241
|
+
|
|
242
|
+
card = QFrame()
|
|
243
|
+
card.setObjectName("PovCard")
|
|
244
|
+
card_layout = QVBoxLayout(card)
|
|
245
|
+
card_layout.setContentsMargins(16, 14, 16, 14)
|
|
246
|
+
card_layout.setSpacing(9)
|
|
247
|
+
mode_row = QHBoxLayout()
|
|
248
|
+
self.mode_group = QButtonGroup(self)
|
|
249
|
+
self.code_mode_button = QPushButton()
|
|
250
|
+
self.code_mode_button.setObjectName("PovMode")
|
|
251
|
+
self.code_mode_button.setCheckable(True)
|
|
252
|
+
self.code_mode_button.clicked.connect(lambda: self._select_mode("code"))
|
|
253
|
+
self.function_mode_button = QPushButton()
|
|
254
|
+
self.function_mode_button.setObjectName("PovMode")
|
|
255
|
+
self.function_mode_button.setCheckable(True)
|
|
256
|
+
self.function_mode_button.clicked.connect(lambda: self._select_mode("function"))
|
|
257
|
+
self.mode_group.addButton(self.code_mode_button)
|
|
258
|
+
self.mode_group.addButton(self.function_mode_button)
|
|
259
|
+
mode_row.addWidget(self.code_mode_button)
|
|
260
|
+
mode_row.addWidget(self.function_mode_button)
|
|
261
|
+
mode_row.addStretch(1)
|
|
262
|
+
card_layout.addLayout(mode_row)
|
|
263
|
+
self.selection_heading = QLabel()
|
|
264
|
+
self.selection_heading.setObjectName("PovKicker")
|
|
265
|
+
card_layout.addWidget(self.selection_heading)
|
|
266
|
+
|
|
267
|
+
columns = QSplitter(Qt.Orientation.Horizontal)
|
|
268
|
+
self.function_list = QListWidget()
|
|
269
|
+
self.function_list.setObjectName("PovList")
|
|
270
|
+
self.function_list.setMinimumWidth(220)
|
|
271
|
+
self.function_list.currentItemChanged.connect(self._function_selected)
|
|
272
|
+
columns.addWidget(self.function_list)
|
|
273
|
+
|
|
274
|
+
details = QWidget()
|
|
275
|
+
detail_layout = QVBoxLayout(details)
|
|
276
|
+
detail_layout.setContentsMargins(8, 0, 0, 0)
|
|
277
|
+
detail_layout.setSpacing(7)
|
|
278
|
+
self.function_heading = QLabel()
|
|
279
|
+
self.function_heading.setObjectName("PovTitle")
|
|
280
|
+
detail_layout.addWidget(self.function_heading)
|
|
281
|
+
self.function_description = QLabel()
|
|
282
|
+
self.function_description.setObjectName("PovMuted")
|
|
283
|
+
self.function_description.setWordWrap(True)
|
|
284
|
+
detail_layout.addWidget(self.function_description)
|
|
285
|
+
self.relationships = QLabel()
|
|
286
|
+
self.relationships.setObjectName("PovMuted")
|
|
287
|
+
self.relationships.setWordWrap(True)
|
|
288
|
+
detail_layout.addWidget(self.relationships)
|
|
289
|
+
self.arguments_heading = QLabel()
|
|
290
|
+
self.arguments_heading.setObjectName("PovKicker")
|
|
291
|
+
detail_layout.addWidget(self.arguments_heading)
|
|
292
|
+
self.parameter_form = QFormLayout()
|
|
293
|
+
self.parameter_form.setContentsMargins(0, 0, 0, 0)
|
|
294
|
+
self.parameter_form.setSpacing(7)
|
|
295
|
+
detail_layout.addLayout(self.parameter_form)
|
|
296
|
+
self.support_note = QLabel()
|
|
297
|
+
self.support_note.setObjectName("PovMuted")
|
|
298
|
+
self.support_note.setWordWrap(True)
|
|
299
|
+
detail_layout.addWidget(self.support_note)
|
|
300
|
+
self.selection_error = QLabel()
|
|
301
|
+
self.selection_error.setObjectName("PovFeedback")
|
|
302
|
+
self.selection_error.setWordWrap(True)
|
|
303
|
+
self.selection_error.hide()
|
|
304
|
+
detail_layout.addWidget(self.selection_error)
|
|
305
|
+
self.start_button = QPushButton()
|
|
306
|
+
self.start_button.setObjectName("PovPrimary")
|
|
307
|
+
self.start_button.clicked.connect(self._start_simulation)
|
|
308
|
+
detail_layout.addWidget(self.start_button, 0, Qt.AlignmentFlag.AlignRight)
|
|
309
|
+
detail_layout.addStretch(1)
|
|
310
|
+
columns.addWidget(details)
|
|
311
|
+
columns.setSizes([300, 620])
|
|
312
|
+
card_layout.addWidget(columns, 1)
|
|
313
|
+
outer.addWidget(card, 1)
|
|
314
|
+
|
|
315
|
+
self.selection_message = QLabel()
|
|
316
|
+
self.selection_message.setObjectName("PovMuted")
|
|
317
|
+
self.selection_message.setWordWrap(True)
|
|
318
|
+
outer.addWidget(self.selection_message)
|
|
319
|
+
self.screens.addWidget(self.selection_screen)
|
|
320
|
+
|
|
321
|
+
def _build_practice_screen(self) -> None:
|
|
322
|
+
self.practice_screen = QWidget()
|
|
323
|
+
outer = QVBoxLayout(self.practice_screen)
|
|
324
|
+
outer.setContentsMargins(0, 0, 0, 0)
|
|
325
|
+
outer.setSpacing(9)
|
|
326
|
+
progress_row = QHBoxLayout()
|
|
327
|
+
self.progress_label = QLabel()
|
|
328
|
+
self.progress_label.setObjectName("PovKicker")
|
|
329
|
+
progress_row.addWidget(self.progress_label)
|
|
330
|
+
progress_row.addStretch(1)
|
|
331
|
+
self.progress_bar = QProgressBar()
|
|
332
|
+
self.progress_bar.setObjectName("PovProgress")
|
|
333
|
+
self.progress_bar.setTextVisible(False)
|
|
334
|
+
self.progress_bar.setFixedWidth(220)
|
|
335
|
+
progress_row.addWidget(self.progress_bar)
|
|
336
|
+
outer.addLayout(progress_row)
|
|
337
|
+
|
|
338
|
+
split = QSplitter(Qt.Orientation.Horizontal)
|
|
339
|
+
source_card = QFrame()
|
|
340
|
+
source_card.setObjectName("PovCard")
|
|
341
|
+
source_layout = QVBoxLayout(source_card)
|
|
342
|
+
source_layout.setContentsMargins(12, 10, 12, 12)
|
|
343
|
+
self.source_heading = QLabel()
|
|
344
|
+
self.source_heading.setObjectName("PovKicker")
|
|
345
|
+
source_layout.addWidget(self.source_heading)
|
|
346
|
+
self.source_view = QPlainTextEdit()
|
|
347
|
+
self.source_view.setObjectName("PovSource")
|
|
348
|
+
self.source_view.setReadOnly(True)
|
|
349
|
+
font = QFontDatabase.systemFont(QFontDatabase.SystemFont.FixedFont)
|
|
350
|
+
font.setPointSize(10)
|
|
351
|
+
self.source_view.setFont(font)
|
|
352
|
+
source_layout.addWidget(self.source_view, 1)
|
|
353
|
+
split.addWidget(source_card)
|
|
354
|
+
|
|
355
|
+
action_scroll = QScrollArea()
|
|
356
|
+
action_scroll.setWidgetResizable(True)
|
|
357
|
+
action_scroll.setFrameShape(QFrame.Shape.NoFrame)
|
|
358
|
+
action_scroll.setStyleSheet(
|
|
359
|
+
"QScrollArea { background: transparent; border: none; } "
|
|
360
|
+
"QScrollArea > QWidget > QWidget { background: transparent; }"
|
|
361
|
+
)
|
|
362
|
+
action_panel = QWidget()
|
|
363
|
+
action_layout = QVBoxLayout(action_panel)
|
|
364
|
+
action_layout.setContentsMargins(2, 2, 2, 2)
|
|
365
|
+
action_layout.setSpacing(8)
|
|
366
|
+
|
|
367
|
+
top = QFrame()
|
|
368
|
+
top.setObjectName("PovCard")
|
|
369
|
+
top_layout = QHBoxLayout(top)
|
|
370
|
+
top_layout.setContentsMargins(10, 8, 10, 8)
|
|
371
|
+
self.game_mascot = PixelMascot(48)
|
|
372
|
+
top_layout.addWidget(self.game_mascot)
|
|
373
|
+
self.step_prompt = QLabel()
|
|
374
|
+
self.step_prompt.setObjectName("PovTitle")
|
|
375
|
+
self.step_prompt.setWordWrap(True)
|
|
376
|
+
top_layout.addWidget(self.step_prompt, 1)
|
|
377
|
+
action_layout.addWidget(top)
|
|
378
|
+
|
|
379
|
+
self.expression_label = QLabel()
|
|
380
|
+
self.expression_label.setObjectName("PovExpression")
|
|
381
|
+
self.expression_label.setWordWrap(True)
|
|
382
|
+
action_layout.addWidget(self.expression_label)
|
|
383
|
+
|
|
384
|
+
memory_card = QFrame()
|
|
385
|
+
memory_card.setObjectName("PovCard")
|
|
386
|
+
memory_layout = QHBoxLayout(memory_card)
|
|
387
|
+
memory_layout.setContentsMargins(10, 8, 10, 8)
|
|
388
|
+
stack_panel = QWidget()
|
|
389
|
+
stack_layout = QVBoxLayout(stack_panel)
|
|
390
|
+
stack_layout.setContentsMargins(0, 0, 0, 0)
|
|
391
|
+
self.stack_heading = QLabel()
|
|
392
|
+
self.stack_heading.setObjectName("PovKicker")
|
|
393
|
+
stack_layout.addWidget(self.stack_heading)
|
|
394
|
+
self.stack_list = QListWidget()
|
|
395
|
+
self.stack_list.setObjectName("PovList")
|
|
396
|
+
self.stack_list.setMaximumHeight(105)
|
|
397
|
+
stack_layout.addWidget(self.stack_list)
|
|
398
|
+
variables_panel = QWidget()
|
|
399
|
+
variables_layout = QVBoxLayout(variables_panel)
|
|
400
|
+
variables_layout.setContentsMargins(0, 0, 0, 0)
|
|
401
|
+
self.variables_heading = QLabel()
|
|
402
|
+
self.variables_heading.setObjectName("PovKicker")
|
|
403
|
+
variables_layout.addWidget(self.variables_heading)
|
|
404
|
+
self.variables_list = QListWidget()
|
|
405
|
+
self.variables_list.setObjectName("PovList")
|
|
406
|
+
self.variables_list.setMaximumHeight(105)
|
|
407
|
+
variables_layout.addWidget(self.variables_list)
|
|
408
|
+
memory_layout.addWidget(stack_panel)
|
|
409
|
+
memory_layout.addWidget(variables_panel)
|
|
410
|
+
action_layout.addWidget(memory_card)
|
|
411
|
+
|
|
412
|
+
challenge_card = QFrame()
|
|
413
|
+
challenge_card.setObjectName("PovCard")
|
|
414
|
+
challenge_layout = QVBoxLayout(challenge_card)
|
|
415
|
+
challenge_layout.setContentsMargins(12, 10, 12, 10)
|
|
416
|
+
challenge_layout.setSpacing(7)
|
|
417
|
+
self.challenge_prompt = QLabel()
|
|
418
|
+
self.challenge_prompt.setWordWrap(True)
|
|
419
|
+
challenge_layout.addWidget(self.challenge_prompt)
|
|
420
|
+
self.answer_row = QWidget()
|
|
421
|
+
answer_layout = QHBoxLayout(self.answer_row)
|
|
422
|
+
answer_layout.setContentsMargins(0, 0, 0, 0)
|
|
423
|
+
self.answer_edit = QLineEdit()
|
|
424
|
+
self.answer_edit.setMaxLength(MAX_JSON_INPUT_CHARS + 1)
|
|
425
|
+
self.answer_edit.returnPressed.connect(self._submit_text_answer)
|
|
426
|
+
answer_layout.addWidget(self.answer_edit, 1)
|
|
427
|
+
self.check_button = QPushButton()
|
|
428
|
+
self.check_button.setObjectName("PovPrimary")
|
|
429
|
+
self.check_button.clicked.connect(self._submit_text_answer)
|
|
430
|
+
answer_layout.addWidget(self.check_button)
|
|
431
|
+
challenge_layout.addWidget(self.answer_row)
|
|
432
|
+
self.choice_row = QWidget()
|
|
433
|
+
self.choice_layout = QHBoxLayout(self.choice_row)
|
|
434
|
+
self.choice_layout.setContentsMargins(0, 0, 0, 0)
|
|
435
|
+
challenge_layout.addWidget(self.choice_row)
|
|
436
|
+
self.feedback_label = QLabel()
|
|
437
|
+
self.feedback_label.setObjectName("PovFeedback")
|
|
438
|
+
self.feedback_label.setWordWrap(True)
|
|
439
|
+
self.feedback_label.hide()
|
|
440
|
+
challenge_layout.addWidget(self.feedback_label)
|
|
441
|
+
self.hint_label = QLabel()
|
|
442
|
+
self.hint_label.setObjectName("PovMuted")
|
|
443
|
+
self.hint_label.setWordWrap(True)
|
|
444
|
+
self.hint_label.hide()
|
|
445
|
+
challenge_layout.addWidget(self.hint_label)
|
|
446
|
+
self.history_heading = QLabel()
|
|
447
|
+
self.history_heading.setObjectName("PovKicker")
|
|
448
|
+
challenge_layout.addWidget(self.history_heading)
|
|
449
|
+
self.history_list = QListWidget()
|
|
450
|
+
self.history_list.setObjectName("PovList")
|
|
451
|
+
self.history_list.setMaximumHeight(92)
|
|
452
|
+
challenge_layout.addWidget(self.history_list)
|
|
453
|
+
action_layout.addWidget(challenge_card)
|
|
454
|
+
action_layout.addStretch(1)
|
|
455
|
+
action_scroll.setWidget(action_panel)
|
|
456
|
+
split.addWidget(action_scroll)
|
|
457
|
+
split.setSizes([570, 430])
|
|
458
|
+
outer.addWidget(split, 1)
|
|
459
|
+
|
|
460
|
+
controls = QHBoxLayout()
|
|
461
|
+
self.previous_button = QPushButton()
|
|
462
|
+
self.previous_button.setObjectName("PovSecondary")
|
|
463
|
+
self.previous_button.clicked.connect(self._previous_step)
|
|
464
|
+
self.restart_button = QPushButton()
|
|
465
|
+
self.restart_button.setObjectName("PovSecondary")
|
|
466
|
+
self.restart_button.clicked.connect(self._restart)
|
|
467
|
+
controls.addWidget(self.previous_button)
|
|
468
|
+
controls.addWidget(self.restart_button)
|
|
469
|
+
controls.addStretch(1)
|
|
470
|
+
self.hint_button = QPushButton()
|
|
471
|
+
self.hint_button.setObjectName("PovSecondary")
|
|
472
|
+
self.hint_button.clicked.connect(self._show_hint)
|
|
473
|
+
self.answer_button = QPushButton()
|
|
474
|
+
self.answer_button.setObjectName("PovSecondary")
|
|
475
|
+
self.answer_button.clicked.connect(self._show_answer)
|
|
476
|
+
self.next_button = QPushButton()
|
|
477
|
+
self.next_button.setObjectName("PovPrimary")
|
|
478
|
+
self.next_button.clicked.connect(self._next_step)
|
|
479
|
+
controls.addWidget(self.hint_button)
|
|
480
|
+
controls.addWidget(self.answer_button)
|
|
481
|
+
controls.addWidget(self.next_button)
|
|
482
|
+
outer.addLayout(controls)
|
|
483
|
+
self.screens.addWidget(self.practice_screen)
|
|
484
|
+
|
|
485
|
+
def _build_completion_screen(self) -> None:
|
|
486
|
+
self.completion_screen = QWidget()
|
|
487
|
+
outer = QVBoxLayout(self.completion_screen)
|
|
488
|
+
outer.setContentsMargins(12, 12, 12, 12)
|
|
489
|
+
outer.addStretch(1)
|
|
490
|
+
content = QWidget()
|
|
491
|
+
content.setMaximumWidth(800)
|
|
492
|
+
content_layout = QVBoxLayout(content)
|
|
493
|
+
content_layout.setContentsMargins(0, 0, 0, 0)
|
|
494
|
+
content_layout.setSpacing(16)
|
|
495
|
+
card = QFrame()
|
|
496
|
+
card.setObjectName("PovCompletionCard")
|
|
497
|
+
card.setMinimumWidth(440)
|
|
498
|
+
card.setMaximumWidth(760)
|
|
499
|
+
card_layout = QVBoxLayout(card)
|
|
500
|
+
card_layout.setContentsMargins(32, 28, 32, 28)
|
|
501
|
+
card_layout.setSpacing(14)
|
|
502
|
+
self.completion_mascot = PixelMascot(144, celebrating=True)
|
|
503
|
+
card_layout.addWidget(self.completion_mascot, 0, Qt.AlignmentFlag.AlignHCenter)
|
|
504
|
+
self.completion_kicker = QLabel()
|
|
505
|
+
self.completion_kicker.setObjectName("PovKicker")
|
|
506
|
+
card_layout.addWidget(self.completion_kicker, 0, Qt.AlignmentFlag.AlignHCenter)
|
|
507
|
+
self.completion_title = QLabel()
|
|
508
|
+
self.completion_title.setObjectName("PovTitle")
|
|
509
|
+
card_layout.addWidget(self.completion_title, 0, Qt.AlignmentFlag.AlignHCenter)
|
|
510
|
+
self.completion_summary = QLabel()
|
|
511
|
+
self.completion_summary.setObjectName("PovSubtitle")
|
|
512
|
+
self.completion_summary.setWordWrap(True)
|
|
513
|
+
self.completion_summary.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
|
514
|
+
self.completion_summary.setMaximumWidth(680)
|
|
515
|
+
card_layout.addWidget(self.completion_summary)
|
|
516
|
+
content_layout.addWidget(card, 0, Qt.AlignmentFlag.AlignHCenter)
|
|
517
|
+
actions = QHBoxLayout()
|
|
518
|
+
self.similar_button = QPushButton()
|
|
519
|
+
self.similar_button.setObjectName("PovPrimary")
|
|
520
|
+
self.similar_button.clicked.connect(self._open_exercise_screen)
|
|
521
|
+
actions.addWidget(self.similar_button)
|
|
522
|
+
self.replay_button = QPushButton()
|
|
523
|
+
self.replay_button.setObjectName("PovSecondary")
|
|
524
|
+
self.replay_button.clicked.connect(self._restart)
|
|
525
|
+
actions.addWidget(self.replay_button)
|
|
526
|
+
content_layout.addLayout(actions)
|
|
527
|
+
outer.addWidget(content, 0, Qt.AlignmentFlag.AlignHCenter)
|
|
528
|
+
outer.addStretch(1)
|
|
529
|
+
self.screens.addWidget(self.completion_screen)
|
|
530
|
+
|
|
531
|
+
def _build_exercise_screen(self) -> None:
|
|
532
|
+
self.exercise_screen = QWidget()
|
|
533
|
+
outer = QVBoxLayout(self.exercise_screen)
|
|
534
|
+
outer.setContentsMargins(0, 0, 0, 0)
|
|
535
|
+
outer.setSpacing(10)
|
|
536
|
+
|
|
537
|
+
hero = QHBoxLayout()
|
|
538
|
+
self.exercise_mascot = PixelMascot(76)
|
|
539
|
+
hero.addWidget(self.exercise_mascot)
|
|
540
|
+
hero_copy = QVBoxLayout()
|
|
541
|
+
self.exercise_title = QLabel()
|
|
542
|
+
self.exercise_title.setObjectName("PovTitle")
|
|
543
|
+
hero_copy.addWidget(self.exercise_title)
|
|
544
|
+
self.exercise_subtitle = QLabel()
|
|
545
|
+
self.exercise_subtitle.setObjectName("PovSubtitle")
|
|
546
|
+
self.exercise_subtitle.setWordWrap(True)
|
|
547
|
+
hero_copy.addWidget(self.exercise_subtitle)
|
|
548
|
+
hero.addLayout(hero_copy, 1)
|
|
549
|
+
outer.addLayout(hero)
|
|
550
|
+
|
|
551
|
+
level_row = QHBoxLayout()
|
|
552
|
+
self.new_values_button = QPushButton()
|
|
553
|
+
self.new_values_button.setObjectName("PovSecondary")
|
|
554
|
+
self.new_values_button.clicked.connect(
|
|
555
|
+
lambda: self._generate_exercise("new_values")
|
|
556
|
+
)
|
|
557
|
+
self.similar_logic_button = QPushButton()
|
|
558
|
+
self.similar_logic_button.setObjectName("PovPrimary")
|
|
559
|
+
self.similar_logic_button.clicked.connect(
|
|
560
|
+
lambda: self._generate_exercise("similar_logic")
|
|
561
|
+
)
|
|
562
|
+
level_row.addWidget(self.new_values_button)
|
|
563
|
+
level_row.addWidget(self.similar_logic_button)
|
|
564
|
+
level_row.addStretch(1)
|
|
565
|
+
outer.addLayout(level_row)
|
|
566
|
+
|
|
567
|
+
self.exercise_status = QLabel()
|
|
568
|
+
self.exercise_status.setObjectName("PovFeedback")
|
|
569
|
+
self.exercise_status.setWordWrap(True)
|
|
570
|
+
outer.addWidget(self.exercise_status)
|
|
571
|
+
|
|
572
|
+
comparison = QSplitter(Qt.Orientation.Horizontal)
|
|
573
|
+
original_card = QFrame()
|
|
574
|
+
original_card.setObjectName("PovCard")
|
|
575
|
+
original_layout = QVBoxLayout(original_card)
|
|
576
|
+
original_layout.setContentsMargins(10, 8, 10, 10)
|
|
577
|
+
self.original_code_heading = QLabel()
|
|
578
|
+
self.original_code_heading.setObjectName("PovKicker")
|
|
579
|
+
original_layout.addWidget(self.original_code_heading)
|
|
580
|
+
self.original_inputs_label = QLabel()
|
|
581
|
+
self.original_inputs_label.setObjectName("PovMuted")
|
|
582
|
+
original_layout.addWidget(self.original_inputs_label)
|
|
583
|
+
self.original_code_view = QPlainTextEdit()
|
|
584
|
+
self.original_code_view.setObjectName("PovSource")
|
|
585
|
+
self.original_code_view.setReadOnly(True)
|
|
586
|
+
original_layout.addWidget(self.original_code_view, 1)
|
|
587
|
+
comparison.addWidget(original_card)
|
|
588
|
+
|
|
589
|
+
exercise_card = QFrame()
|
|
590
|
+
exercise_card.setObjectName("PovCard")
|
|
591
|
+
exercise_layout = QVBoxLayout(exercise_card)
|
|
592
|
+
exercise_layout.setContentsMargins(10, 8, 10, 10)
|
|
593
|
+
self.generated_code_heading = QLabel()
|
|
594
|
+
self.generated_code_heading.setObjectName("PovKicker")
|
|
595
|
+
exercise_layout.addWidget(self.generated_code_heading)
|
|
596
|
+
self.generated_inputs_label = QLabel()
|
|
597
|
+
self.generated_inputs_label.setObjectName("PovMuted")
|
|
598
|
+
exercise_layout.addWidget(self.generated_inputs_label)
|
|
599
|
+
self.generated_code_view = QPlainTextEdit()
|
|
600
|
+
self.generated_code_view.setObjectName("PovSource")
|
|
601
|
+
self.generated_code_view.setReadOnly(True)
|
|
602
|
+
exercise_layout.addWidget(self.generated_code_view, 1)
|
|
603
|
+
comparison.addWidget(exercise_card)
|
|
604
|
+
comparison.setSizes([1, 1])
|
|
605
|
+
outer.addWidget(comparison, 1)
|
|
606
|
+
|
|
607
|
+
actions = QHBoxLayout()
|
|
608
|
+
self.exercise_back_button = QPushButton()
|
|
609
|
+
self.exercise_back_button.setObjectName("PovSecondary")
|
|
610
|
+
self.exercise_back_button.clicked.connect(
|
|
611
|
+
lambda: self.screens.setCurrentWidget(self.completion_screen)
|
|
612
|
+
)
|
|
613
|
+
actions.addWidget(self.exercise_back_button)
|
|
614
|
+
actions.addStretch(1)
|
|
615
|
+
self.start_exercise_button = QPushButton()
|
|
616
|
+
self.start_exercise_button.setObjectName("PovPrimary")
|
|
617
|
+
self.start_exercise_button.clicked.connect(self._start_generated_exercise)
|
|
618
|
+
self.start_exercise_button.setEnabled(False)
|
|
619
|
+
actions.addWidget(self.start_exercise_button)
|
|
620
|
+
outer.addLayout(actions)
|
|
621
|
+
self.screens.addWidget(self.exercise_screen)
|
|
622
|
+
|
|
623
|
+
def retranslate(self) -> None:
|
|
624
|
+
self.back_button.setText(tr("← Back to analysis"))
|
|
625
|
+
self.nav_label.setText(
|
|
626
|
+
tr("BE THE CODE · SAFE PRACTICE")
|
|
627
|
+
if self._mode == "code"
|
|
628
|
+
else tr("FUNCTION POV · SAFE PRACTICE")
|
|
629
|
+
)
|
|
630
|
+
self.code_mode_button.setText(tr("Be the Code"))
|
|
631
|
+
self.function_mode_button.setText(tr("Be the Function"))
|
|
632
|
+
self.entry_kicker.setText(
|
|
633
|
+
tr("A PROGRAM IS A JOURNEY")
|
|
634
|
+
if self._mode == "code"
|
|
635
|
+
else tr("A FUNCTION IS A JOURNEY")
|
|
636
|
+
)
|
|
637
|
+
self.entry_title.setText(tr("Be the Code") if self._mode == "code" else tr("Be the Function"))
|
|
638
|
+
self.entry_subtitle.setText(
|
|
639
|
+
tr("Follow top-level statements, changing values, decisions, loops, and output. "
|
|
640
|
+
"Prism uses a bounded simulation; it never runs your Python code.")
|
|
641
|
+
if self._mode == "code"
|
|
642
|
+
else tr("Choose a function, follow its values, and practise each decision. "
|
|
643
|
+
"Prism uses a bounded simulation; it never runs your Python code.")
|
|
644
|
+
)
|
|
645
|
+
self.selection_heading.setText(tr("CHOOSE A FUNCTION"))
|
|
646
|
+
self.function_heading.setText(tr("Select a function"))
|
|
647
|
+
self.function_description.setText(tr("Function details and arguments will appear here."))
|
|
648
|
+
self.arguments_heading.setText(tr("ARGUMENTS · ENTER JSON VALUES"))
|
|
649
|
+
self.support_note.setText(
|
|
650
|
+
tr("Blank optional arguments use their defaults. Only a safe subset of Python is simulated.")
|
|
651
|
+
)
|
|
652
|
+
self.start_button.setText(tr("Enter function space"))
|
|
653
|
+
self.selection_message.setText(tr("Select a supported function to begin."))
|
|
654
|
+
self.source_heading.setText(tr("YOUR FUNCTION · CURRENT LINE HIGHLIGHTED"))
|
|
655
|
+
self.stack_heading.setText(tr("CALL STACK"))
|
|
656
|
+
self.variables_heading.setText(tr("LOCAL VARIABLES"))
|
|
657
|
+
self.history_heading.setText(tr("ANSWER HISTORY"))
|
|
658
|
+
self.previous_button.setText(tr("← Previous"))
|
|
659
|
+
self.restart_button.setText(tr("↻ Restart"))
|
|
660
|
+
self.hint_button.setText(tr("Hint"))
|
|
661
|
+
self.answer_button.setText(tr("Show answer"))
|
|
662
|
+
self.check_button.setText(tr("Check answer"))
|
|
663
|
+
self.replay_button.setText(
|
|
664
|
+
tr("↻ Replay program") if self._mode == "code" else tr("↻ Replay function")
|
|
665
|
+
)
|
|
666
|
+
self.similar_button.setText(tr("Practice Similar Questions"))
|
|
667
|
+
self.completion_kicker.setText(tr("MISSION COMPLETE"))
|
|
668
|
+
self.completion_title.setText(tr("Stage clear!"))
|
|
669
|
+
self.exercise_title.setText(tr("Practice Similar Questions"))
|
|
670
|
+
self.exercise_subtitle.setText(
|
|
671
|
+
tr("Choose a practice level. The original source stays unchanged, and each new exercise is checked by Prism's safe simulator.")
|
|
672
|
+
)
|
|
673
|
+
self.new_values_button.setText(tr("New Values"))
|
|
674
|
+
self.similar_logic_button.setText(tr("Similar Logic"))
|
|
675
|
+
self.original_code_heading.setText(tr("Original exercise"))
|
|
676
|
+
self.generated_code_heading.setText(tr("New exercise · compare before starting"))
|
|
677
|
+
self.exercise_back_button.setText(tr("← Back to completion"))
|
|
678
|
+
self.start_exercise_button.setText(tr("Start this exercise"))
|
|
679
|
+
self._render_exercise_comparison()
|
|
680
|
+
self._render_exercise_status()
|
|
681
|
+
self._update_progress_labels()
|
|
682
|
+
if self._unsupported_language:
|
|
683
|
+
self.selection_message.setText(
|
|
684
|
+
tr("Function POV currently supports Python only; this analysis uses {language}.",
|
|
685
|
+
language=self._unsupported_language)
|
|
686
|
+
)
|
|
687
|
+
else:
|
|
688
|
+
self._update_selection_message()
|
|
689
|
+
if self._simulation is not None:
|
|
690
|
+
self._render_step()
|
|
691
|
+
function = self._selected_function()
|
|
692
|
+
if function is not None:
|
|
693
|
+
self.function_description.setText(
|
|
694
|
+
function.docstring or tr("No docstring is available for this function.")
|
|
695
|
+
)
|
|
696
|
+
advanced = False
|
|
697
|
+
parameters = {parameter.name: parameter for parameter in function.parameters}
|
|
698
|
+
for name, field in self._parameter_fields.items():
|
|
699
|
+
parameter = parameters[name]
|
|
700
|
+
field.setPlaceholderText(
|
|
701
|
+
tr("Leave blank to use default") if parameter.default is not None
|
|
702
|
+
else tr("Required JSON value")
|
|
703
|
+
)
|
|
704
|
+
advanced = advanced or parameter.kind != "positional_or_keyword"
|
|
705
|
+
if advanced:
|
|
706
|
+
self.support_note.setText(
|
|
707
|
+
tr("This function uses advanced parameter syntax. "
|
|
708
|
+
"Phase 1 supports simple positional parameters only.")
|
|
709
|
+
)
|
|
710
|
+
else:
|
|
711
|
+
self.support_note.setText(
|
|
712
|
+
tr("Blank optional arguments use their defaults. "
|
|
713
|
+
"The simulation never executes your code.")
|
|
714
|
+
)
|
|
715
|
+
self._update_relationships(function)
|
|
716
|
+
if self._completion_metrics is not None:
|
|
717
|
+
self._render_completion_summary()
|
|
718
|
+
self._apply_mode()
|
|
719
|
+
|
|
720
|
+
def _apply_mode(self) -> None:
|
|
721
|
+
code_mode = self._mode == "code"
|
|
722
|
+
self.code_mode_button.setChecked(code_mode)
|
|
723
|
+
self.function_mode_button.setChecked(not code_mode)
|
|
724
|
+
language_supported = self._unsupported_language is None
|
|
725
|
+
self.code_mode_button.setEnabled(language_supported and bool(self._source))
|
|
726
|
+
self.function_mode_button.setEnabled(language_supported and bool(self._functions))
|
|
727
|
+
self.function_list.setVisible(not code_mode)
|
|
728
|
+
self.selection_heading.setText(
|
|
729
|
+
tr("WHOLE PROGRAM") if code_mode else tr("CHOOSE A FUNCTION")
|
|
730
|
+
)
|
|
731
|
+
selected = self._selected_function()
|
|
732
|
+
if code_mode:
|
|
733
|
+
self.function_heading.setText(tr("Program"))
|
|
734
|
+
self.function_description.setText(
|
|
735
|
+
tr("Follow executable top-level statements from start to finish.")
|
|
736
|
+
)
|
|
737
|
+
elif selected is None:
|
|
738
|
+
self.function_heading.setText(tr("Select a function"))
|
|
739
|
+
self.function_description.setText(
|
|
740
|
+
tr("Function details and arguments will appear here.")
|
|
741
|
+
)
|
|
742
|
+
else:
|
|
743
|
+
self.function_heading.setText(selected.name)
|
|
744
|
+
self.function_description.setText(
|
|
745
|
+
selected.docstring or tr("No docstring is available for this function.")
|
|
746
|
+
)
|
|
747
|
+
self.relationships.setVisible(not code_mode)
|
|
748
|
+
self.arguments_heading.setVisible(not code_mode)
|
|
749
|
+
for row in range(self.parameter_form.rowCount()):
|
|
750
|
+
for role in (
|
|
751
|
+
QFormLayout.ItemRole.LabelRole,
|
|
752
|
+
QFormLayout.ItemRole.FieldRole,
|
|
753
|
+
):
|
|
754
|
+
item = self.parameter_form.itemAt(row, role)
|
|
755
|
+
if item is not None and item.widget() is not None:
|
|
756
|
+
item.widget().setVisible(not code_mode)
|
|
757
|
+
if code_mode:
|
|
758
|
+
self.support_note.setText(
|
|
759
|
+
tr("Only a bounded Python subset is simulated. Unsupported statements are explained before play.")
|
|
760
|
+
)
|
|
761
|
+
self.start_button.setText(tr("Trace whole program"))
|
|
762
|
+
self.start_button.setEnabled(self._program_simulation is not None)
|
|
763
|
+
self._update_selection_message()
|
|
764
|
+
else:
|
|
765
|
+
advanced = selected is not None and any(
|
|
766
|
+
parameter.kind != "positional_or_keyword"
|
|
767
|
+
for parameter in selected.parameters
|
|
768
|
+
)
|
|
769
|
+
if advanced:
|
|
770
|
+
self.support_note.setText(
|
|
771
|
+
tr("This function uses advanced parameter syntax. "
|
|
772
|
+
"Phase 1 supports simple positional parameters only.")
|
|
773
|
+
)
|
|
774
|
+
else:
|
|
775
|
+
self.support_note.setText(
|
|
776
|
+
tr("Blank optional arguments use their defaults. "
|
|
777
|
+
"The simulation never executes your code.")
|
|
778
|
+
)
|
|
779
|
+
self.start_button.setText(tr("Enter function space"))
|
|
780
|
+
self.start_button.setEnabled(
|
|
781
|
+
selected is not None and not advanced
|
|
782
|
+
)
|
|
783
|
+
self._update_selection_message()
|
|
784
|
+
|
|
785
|
+
def set_source(self, source: str, filename: str) -> None:
|
|
786
|
+
self._unsupported_language = None
|
|
787
|
+
self._source = source
|
|
788
|
+
self._exercise_generator = PracticeExerciseGenerator()
|
|
789
|
+
self._practice_context = None
|
|
790
|
+
self._pending_exercise = None
|
|
791
|
+
self._active_exercise = None
|
|
792
|
+
self._generation_status = ""
|
|
793
|
+
candidate = Path(filename).name
|
|
794
|
+
self._filename = candidate if Path(candidate).suffix.lower() == ".py" else "main.py"
|
|
795
|
+
self._analysis = analyze_source(source, self._filename)
|
|
796
|
+
self._functions = {function.id: function for function in self._analysis.functions}
|
|
797
|
+
self._simulation = None
|
|
798
|
+
self._original_simulation = None
|
|
799
|
+
self._program_simulation = None
|
|
800
|
+
self._program_error = None
|
|
801
|
+
self._completion_metrics = None
|
|
802
|
+
self._mode = "function" if self._functions else "code"
|
|
803
|
+
self._parameter_fields.clear()
|
|
804
|
+
self.function_list.clear()
|
|
805
|
+
self.start_button.setEnabled(False)
|
|
806
|
+
self.selection_error.hide()
|
|
807
|
+
self.relationships.clear()
|
|
808
|
+
self.function_heading.setText(tr("Select a function"))
|
|
809
|
+
self.function_description.setText(tr("Function details and arguments will appear here."))
|
|
810
|
+
while self.parameter_form.rowCount():
|
|
811
|
+
self.parameter_form.removeRow(0)
|
|
812
|
+
|
|
813
|
+
for function in self._functions.values():
|
|
814
|
+
signature = ", ".join(
|
|
815
|
+
parameter.name + (f"={parameter.default}" if parameter.default is not None else "")
|
|
816
|
+
for parameter in function.parameters
|
|
817
|
+
)
|
|
818
|
+
item = QListWidgetItem(f"{function.name}({signature}) · L{function.line}")
|
|
819
|
+
item.setData(Qt.ItemDataRole.UserRole, function.id)
|
|
820
|
+
self.function_list.addItem(item)
|
|
821
|
+
self.screens.setCurrentWidget(self.selection_screen)
|
|
822
|
+
if self.function_list.count():
|
|
823
|
+
self.function_list.setCurrentRow(0)
|
|
824
|
+
|
|
825
|
+
self._prepare_code_simulation()
|
|
826
|
+
self.retranslate()
|
|
827
|
+
|
|
828
|
+
def _prepare_code_simulation(self) -> None:
|
|
829
|
+
self._program_simulation = None
|
|
830
|
+
self._program_error = None
|
|
831
|
+
if self._unsupported_language is not None or not self._source:
|
|
832
|
+
return
|
|
833
|
+
try:
|
|
834
|
+
self._program_simulation = build_program_simulation_from_source(
|
|
835
|
+
self._source,
|
|
836
|
+
self._filename,
|
|
837
|
+
)
|
|
838
|
+
except (SimulationError, ValueError, OSError) as error:
|
|
839
|
+
self._program_error = str(error)
|
|
840
|
+
|
|
841
|
+
def _select_mode(self, mode: str) -> None:
|
|
842
|
+
if self._unsupported_language is not None:
|
|
843
|
+
return
|
|
844
|
+
self._mode = mode
|
|
845
|
+
if mode == "code" and self._program_simulation is None and self._program_error is None:
|
|
846
|
+
self._prepare_code_simulation()
|
|
847
|
+
self.retranslate()
|
|
848
|
+
|
|
849
|
+
def _update_selection_message(self) -> None:
|
|
850
|
+
if self._analysis is None:
|
|
851
|
+
return
|
|
852
|
+
if self._mode == "code":
|
|
853
|
+
if self._program_error:
|
|
854
|
+
message = tr(
|
|
855
|
+
"This program is outside the safe simulation subset: {error}",
|
|
856
|
+
error=simulation_error_text(self._program_error),
|
|
857
|
+
)
|
|
858
|
+
else:
|
|
859
|
+
message = tr(
|
|
860
|
+
"Ready for Be the Code. The simulator follows bounded steps and never executes your Python code."
|
|
861
|
+
)
|
|
862
|
+
self.selection_message.setText(message)
|
|
863
|
+
return
|
|
864
|
+
errors = [item for item in self._analysis.diagnostics if item.severity == "error"]
|
|
865
|
+
if errors:
|
|
866
|
+
message = tr(
|
|
867
|
+
"Python syntax could not be analysed: {error}",
|
|
868
|
+
error=simulation_error_text(errors[0].message),
|
|
869
|
+
)
|
|
870
|
+
elif not self._functions:
|
|
871
|
+
message = tr("No top-level synchronous functions were found. "
|
|
872
|
+
"Function POV needs a regular def function to begin.")
|
|
873
|
+
else:
|
|
874
|
+
message = tr("This is a simplified, bounded simulation. "
|
|
875
|
+
"Unsupported syntax is explained before play.")
|
|
876
|
+
warnings = [item for item in self._analysis.diagnostics if item.severity != "error"]
|
|
877
|
+
if warnings:
|
|
878
|
+
message += "\n" + tr(
|
|
879
|
+
"Other structures may be outside the supported subset: {details}",
|
|
880
|
+
details="; ".join(item.message for item in warnings[:3]),
|
|
881
|
+
)
|
|
882
|
+
self.selection_message.setText(message)
|
|
883
|
+
|
|
884
|
+
def set_unsupported_language(self, language: str) -> None:
|
|
885
|
+
self._unsupported_language = language
|
|
886
|
+
self._source = ""
|
|
887
|
+
self._exercise_generator = PracticeExerciseGenerator()
|
|
888
|
+
self._practice_context = None
|
|
889
|
+
self._pending_exercise = None
|
|
890
|
+
self._active_exercise = None
|
|
891
|
+
self._generation_status = ""
|
|
892
|
+
self._filename = "main.py"
|
|
893
|
+
self._analysis = None
|
|
894
|
+
self._functions.clear()
|
|
895
|
+
self._simulation = None
|
|
896
|
+
self._original_simulation = None
|
|
897
|
+
self._program_simulation = None
|
|
898
|
+
self._program_error = None
|
|
899
|
+
self._completion_metrics = None
|
|
900
|
+
self._mode = "code"
|
|
901
|
+
self.function_list.clear()
|
|
902
|
+
self._parameter_fields.clear()
|
|
903
|
+
while self.parameter_form.rowCount():
|
|
904
|
+
self.parameter_form.removeRow(0)
|
|
905
|
+
self.function_heading.setText(tr("Select a function"))
|
|
906
|
+
self.function_description.setText(tr("Function details and arguments will appear here."))
|
|
907
|
+
self.relationships.clear()
|
|
908
|
+
self.start_button.setEnabled(False)
|
|
909
|
+
self.selection_error.hide()
|
|
910
|
+
self.selection_message.setText(
|
|
911
|
+
tr("Function POV currently supports Python only; this analysis uses {language}.",
|
|
912
|
+
language=language)
|
|
913
|
+
)
|
|
914
|
+
self.retranslate()
|
|
915
|
+
self.screens.setCurrentWidget(self.selection_screen)
|
|
916
|
+
|
|
917
|
+
def _selected_function(self) -> FunctionInfo | None:
|
|
918
|
+
item = self.function_list.currentItem()
|
|
919
|
+
return self._functions.get(item.data(Qt.ItemDataRole.UserRole)) if item else None
|
|
920
|
+
|
|
921
|
+
def _function_selected(self, current: QListWidgetItem | None, _previous: QListWidgetItem | None) -> None:
|
|
922
|
+
while self.parameter_form.rowCount():
|
|
923
|
+
self.parameter_form.removeRow(0)
|
|
924
|
+
self._parameter_fields.clear()
|
|
925
|
+
self.selection_error.hide()
|
|
926
|
+
if current is None:
|
|
927
|
+
return
|
|
928
|
+
function = self._functions.get(current.data(Qt.ItemDataRole.UserRole))
|
|
929
|
+
if function is None:
|
|
930
|
+
return
|
|
931
|
+
self.function_heading.setText(function.name)
|
|
932
|
+
self.function_description.setText(
|
|
933
|
+
function.docstring or tr("No docstring is available for this function.")
|
|
934
|
+
)
|
|
935
|
+
self._update_relationships(function)
|
|
936
|
+
|
|
937
|
+
advanced = False
|
|
938
|
+
for parameter in function.parameters:
|
|
939
|
+
field = QLineEdit()
|
|
940
|
+
field.setMaxLength(MAX_JSON_INPUT_CHARS + 1)
|
|
941
|
+
field.setPlaceholderText(
|
|
942
|
+
tr("Leave blank to use default") if parameter.default is not None
|
|
943
|
+
else tr("Required JSON value")
|
|
944
|
+
)
|
|
945
|
+
self.parameter_form.addRow(parameter.name, field)
|
|
946
|
+
self._parameter_fields[parameter.name] = field
|
|
947
|
+
if parameter.kind != "positional_or_keyword":
|
|
948
|
+
advanced = True
|
|
949
|
+
if not function.parameters:
|
|
950
|
+
self.parameter_form.addRow(QLabel(tr("This function has no parameters.")))
|
|
951
|
+
|
|
952
|
+
if advanced:
|
|
953
|
+
self.support_note.setText(
|
|
954
|
+
tr("This function uses advanced parameter syntax. "
|
|
955
|
+
"Phase 1 supports simple positional parameters only.")
|
|
956
|
+
)
|
|
957
|
+
self.start_button.setEnabled(False)
|
|
958
|
+
else:
|
|
959
|
+
self.support_note.setText(
|
|
960
|
+
tr("Blank optional arguments use their defaults. "
|
|
961
|
+
"The simulation never executes your code.")
|
|
962
|
+
)
|
|
963
|
+
self.start_button.setEnabled(True)
|
|
964
|
+
|
|
965
|
+
def _update_relationships(self, function: FunctionInfo) -> None:
|
|
966
|
+
if len(self._functions) <= 1 or self._analysis is None:
|
|
967
|
+
self.relationships.clear()
|
|
968
|
+
return
|
|
969
|
+
called = sorted({
|
|
970
|
+
call.callee.split(".")[-1]
|
|
971
|
+
for call in self._analysis.calls
|
|
972
|
+
if call.caller == function.id
|
|
973
|
+
})
|
|
974
|
+
callers = sorted({
|
|
975
|
+
call.caller.split(".")[-1]
|
|
976
|
+
for call in self._analysis.calls
|
|
977
|
+
if call.callee == function.id
|
|
978
|
+
})
|
|
979
|
+
self.relationships.setText(
|
|
980
|
+
tr("Calls: {calls} · Called by: {callers}",
|
|
981
|
+
calls=", ".join(called) or tr("none"),
|
|
982
|
+
callers=", ".join(callers) or tr("none"))
|
|
983
|
+
)
|
|
984
|
+
|
|
985
|
+
def _start_simulation(self) -> None:
|
|
986
|
+
selected_function_id: str | None = None
|
|
987
|
+
arguments: dict[str, Any] = {}
|
|
988
|
+
if self._mode == "code":
|
|
989
|
+
if self._program_simulation is None:
|
|
990
|
+
self._prepare_code_simulation()
|
|
991
|
+
if self._program_error is not None or self._program_simulation is None:
|
|
992
|
+
self.selection_error.setText(
|
|
993
|
+
tr(
|
|
994
|
+
"This program is outside the safe simulation subset: {error}",
|
|
995
|
+
error=(
|
|
996
|
+
simulation_error_text(self._program_error)
|
|
997
|
+
if self._program_error
|
|
998
|
+
else tr("No executable statements were found.")
|
|
999
|
+
),
|
|
1000
|
+
)
|
|
1001
|
+
)
|
|
1002
|
+
self.selection_error.show()
|
|
1003
|
+
self._update_selection_message()
|
|
1004
|
+
return
|
|
1005
|
+
self._simulation = self._program_simulation
|
|
1006
|
+
else:
|
|
1007
|
+
function = self._selected_function()
|
|
1008
|
+
if function is None:
|
|
1009
|
+
return
|
|
1010
|
+
selected_function_id = function.id
|
|
1011
|
+
for name, field in self._parameter_fields.items():
|
|
1012
|
+
entered = field.text()
|
|
1013
|
+
if len(entered) > MAX_JSON_INPUT_CHARS:
|
|
1014
|
+
self.selection_error.setText(
|
|
1015
|
+
tr(
|
|
1016
|
+
"JSON input is too long (maximum {limit} characters).",
|
|
1017
|
+
limit=MAX_JSON_INPUT_CHARS,
|
|
1018
|
+
)
|
|
1019
|
+
)
|
|
1020
|
+
self.selection_error.show()
|
|
1021
|
+
return
|
|
1022
|
+
raw = entered.strip()
|
|
1023
|
+
if not raw:
|
|
1024
|
+
continue
|
|
1025
|
+
value, error = _decode_json_input(raw)
|
|
1026
|
+
if error == "invalid":
|
|
1027
|
+
self.selection_error.setText(
|
|
1028
|
+
tr("Enter {name} as a JSON value, such as 4, true, or \"hello\".",
|
|
1029
|
+
name=name)
|
|
1030
|
+
)
|
|
1031
|
+
self.selection_error.show()
|
|
1032
|
+
return
|
|
1033
|
+
if error == "too_long":
|
|
1034
|
+
self.selection_error.setText(
|
|
1035
|
+
tr(
|
|
1036
|
+
"JSON input is too long (maximum {limit} characters).",
|
|
1037
|
+
limit=MAX_JSON_INPUT_CHARS,
|
|
1038
|
+
)
|
|
1039
|
+
)
|
|
1040
|
+
self.selection_error.show()
|
|
1041
|
+
return
|
|
1042
|
+
if error == "unsafe":
|
|
1043
|
+
self.selection_error.setText(
|
|
1044
|
+
tr("This JSON value is too large, too deeply nested, or not finite.")
|
|
1045
|
+
)
|
|
1046
|
+
self.selection_error.show()
|
|
1047
|
+
return
|
|
1048
|
+
arguments[name] = value
|
|
1049
|
+
try:
|
|
1050
|
+
self._simulation = build_simulation_from_source(
|
|
1051
|
+
self._source,
|
|
1052
|
+
function.id,
|
|
1053
|
+
arguments,
|
|
1054
|
+
self._filename,
|
|
1055
|
+
)
|
|
1056
|
+
except (SimulationError, ValueError, OSError) as error:
|
|
1057
|
+
self.selection_error.setText(
|
|
1058
|
+
tr("This function is outside the safe simulation subset: {error}",
|
|
1059
|
+
error=simulation_error_text(str(error)))
|
|
1060
|
+
)
|
|
1061
|
+
self.selection_error.show()
|
|
1062
|
+
return
|
|
1063
|
+
|
|
1064
|
+
self._practice_context = (
|
|
1065
|
+
self._mode,
|
|
1066
|
+
selected_function_id,
|
|
1067
|
+
dict(arguments),
|
|
1068
|
+
)
|
|
1069
|
+
self._original_simulation = self._simulation
|
|
1070
|
+
self._active_exercise = None
|
|
1071
|
+
self._reset_practice_state()
|
|
1072
|
+
self.screens.setCurrentWidget(self.practice_screen)
|
|
1073
|
+
self._render_step()
|
|
1074
|
+
|
|
1075
|
+
def _reset_practice_state(self) -> None:
|
|
1076
|
+
self._step_index = 0
|
|
1077
|
+
self._completion_metrics = None
|
|
1078
|
+
self._passed.clear()
|
|
1079
|
+
self._solved_by_attempt.clear()
|
|
1080
|
+
self._revealed.clear()
|
|
1081
|
+
self._history.clear()
|
|
1082
|
+
self._drafts.clear()
|
|
1083
|
+
|
|
1084
|
+
def _open_exercise_screen(self) -> None:
|
|
1085
|
+
self._pending_exercise = None
|
|
1086
|
+
self._generation_status = ""
|
|
1087
|
+
if self._practice_context is None:
|
|
1088
|
+
self.similar_logic_button.setEnabled(False)
|
|
1089
|
+
else:
|
|
1090
|
+
mode, function_id, arguments = self._practice_context
|
|
1091
|
+
availability = self._exercise_generator.similar_logic_availability(
|
|
1092
|
+
mode,
|
|
1093
|
+
self._source,
|
|
1094
|
+
self._filename,
|
|
1095
|
+
function_id,
|
|
1096
|
+
arguments,
|
|
1097
|
+
)
|
|
1098
|
+
self.similar_logic_button.setEnabled(availability is None)
|
|
1099
|
+
self._generation_status = availability or ""
|
|
1100
|
+
self.original_code_view.setPlainText(self._source)
|
|
1101
|
+
self.generated_code_view.clear()
|
|
1102
|
+
self._render_exercise_comparison()
|
|
1103
|
+
self.start_exercise_button.setEnabled(False)
|
|
1104
|
+
self._render_exercise_status()
|
|
1105
|
+
self.screens.setCurrentWidget(self.exercise_screen)
|
|
1106
|
+
|
|
1107
|
+
def _generate_exercise(self, level: str) -> None:
|
|
1108
|
+
if self._practice_context is None:
|
|
1109
|
+
self._generation_status = "Complete an original practice first to create a similar exercise."
|
|
1110
|
+
self._pending_exercise = None
|
|
1111
|
+
self.start_exercise_button.setEnabled(False)
|
|
1112
|
+
self._render_exercise_status()
|
|
1113
|
+
return
|
|
1114
|
+
mode, function_id, arguments = self._practice_context
|
|
1115
|
+
try:
|
|
1116
|
+
exercise = self._exercise_generator.generate(
|
|
1117
|
+
level,
|
|
1118
|
+
mode,
|
|
1119
|
+
self._source,
|
|
1120
|
+
self._filename,
|
|
1121
|
+
function_id,
|
|
1122
|
+
arguments,
|
|
1123
|
+
)
|
|
1124
|
+
except ExerciseGenerationError as error:
|
|
1125
|
+
self._generation_status = str(error)
|
|
1126
|
+
self._pending_exercise = None
|
|
1127
|
+
self.start_exercise_button.setEnabled(False)
|
|
1128
|
+
self.generated_code_view.clear()
|
|
1129
|
+
self._render_exercise_comparison()
|
|
1130
|
+
if level == "similar_logic" and self._practice_context is not None:
|
|
1131
|
+
mode, function_id, arguments = self._practice_context
|
|
1132
|
+
availability = self._exercise_generator.similar_logic_availability(
|
|
1133
|
+
mode,
|
|
1134
|
+
self._source,
|
|
1135
|
+
self._filename,
|
|
1136
|
+
function_id,
|
|
1137
|
+
arguments,
|
|
1138
|
+
)
|
|
1139
|
+
self.similar_logic_button.setEnabled(availability is None)
|
|
1140
|
+
self._render_exercise_status()
|
|
1141
|
+
return
|
|
1142
|
+
self._pending_exercise = exercise
|
|
1143
|
+
self._generation_status = ""
|
|
1144
|
+
self.generated_code_view.setPlainText(exercise.source)
|
|
1145
|
+
self._render_exercise_comparison()
|
|
1146
|
+
self.start_exercise_button.setEnabled(True)
|
|
1147
|
+
if self._practice_context is not None:
|
|
1148
|
+
mode, function_id, arguments = self._practice_context
|
|
1149
|
+
availability = self._exercise_generator.similar_logic_availability(
|
|
1150
|
+
mode,
|
|
1151
|
+
self._source,
|
|
1152
|
+
self._filename,
|
|
1153
|
+
function_id,
|
|
1154
|
+
arguments,
|
|
1155
|
+
)
|
|
1156
|
+
self.similar_logic_button.setEnabled(availability is None)
|
|
1157
|
+
self._render_exercise_status()
|
|
1158
|
+
|
|
1159
|
+
def _render_exercise_comparison(self) -> None:
|
|
1160
|
+
function_mode = (
|
|
1161
|
+
self._practice_context is not None
|
|
1162
|
+
and self._practice_context[0] == "function"
|
|
1163
|
+
)
|
|
1164
|
+
self.original_inputs_label.setVisible(function_mode)
|
|
1165
|
+
self.generated_inputs_label.setVisible(
|
|
1166
|
+
function_mode and self._pending_exercise is not None
|
|
1167
|
+
)
|
|
1168
|
+
if function_mode:
|
|
1169
|
+
self.original_inputs_label.setText(
|
|
1170
|
+
tr(
|
|
1171
|
+
"Original arguments: {values}",
|
|
1172
|
+
values=self._format_value(
|
|
1173
|
+
self._simulation_input_values(self._original_simulation)
|
|
1174
|
+
),
|
|
1175
|
+
)
|
|
1176
|
+
)
|
|
1177
|
+
else:
|
|
1178
|
+
self.original_inputs_label.clear()
|
|
1179
|
+
if function_mode and self._pending_exercise is not None:
|
|
1180
|
+
self.generated_inputs_label.setText(
|
|
1181
|
+
tr(
|
|
1182
|
+
"New arguments: {values}",
|
|
1183
|
+
values=self._format_value(
|
|
1184
|
+
self._simulation_input_values(
|
|
1185
|
+
self._pending_exercise.simulation
|
|
1186
|
+
)
|
|
1187
|
+
),
|
|
1188
|
+
)
|
|
1189
|
+
)
|
|
1190
|
+
else:
|
|
1191
|
+
self.generated_inputs_label.clear()
|
|
1192
|
+
|
|
1193
|
+
@staticmethod
|
|
1194
|
+
def _simulation_input_values(simulation: PovSimulation | None) -> dict[str, Any]:
|
|
1195
|
+
if simulation is None:
|
|
1196
|
+
return {}
|
|
1197
|
+
entry = next(
|
|
1198
|
+
(
|
|
1199
|
+
step for step in simulation.steps
|
|
1200
|
+
if step.kind == "enter"
|
|
1201
|
+
and step.function_id == simulation.function_id
|
|
1202
|
+
),
|
|
1203
|
+
None,
|
|
1204
|
+
)
|
|
1205
|
+
return dict(entry.variables) if entry is not None else {}
|
|
1206
|
+
|
|
1207
|
+
def _render_exercise_status(self) -> None:
|
|
1208
|
+
if self._pending_exercise is not None:
|
|
1209
|
+
level = (
|
|
1210
|
+
tr("New Values")
|
|
1211
|
+
if self._pending_exercise.level == "new_values"
|
|
1212
|
+
else tr("Similar Logic")
|
|
1213
|
+
)
|
|
1214
|
+
concepts = ", ".join(
|
|
1215
|
+
tr(concept) for concept in self._pending_exercise.concepts
|
|
1216
|
+
)
|
|
1217
|
+
self.exercise_status.setText(
|
|
1218
|
+
tr(
|
|
1219
|
+
"Generated {level} exercise. Concepts: {concepts}. "
|
|
1220
|
+
"The candidate passed safe simulation.",
|
|
1221
|
+
level=level,
|
|
1222
|
+
concepts=concepts or tr("related Python concepts"),
|
|
1223
|
+
)
|
|
1224
|
+
)
|
|
1225
|
+
if self._pending_exercise.logic_change:
|
|
1226
|
+
self.exercise_status.setText(
|
|
1227
|
+
self.exercise_status.text()
|
|
1228
|
+
+ "\n"
|
|
1229
|
+
+ tr(
|
|
1230
|
+
"Logic change: {explanation}",
|
|
1231
|
+
explanation=tr(self._pending_exercise.logic_change),
|
|
1232
|
+
)
|
|
1233
|
+
)
|
|
1234
|
+
elif self._generation_status:
|
|
1235
|
+
self.exercise_status.setText(tr(self._generation_status))
|
|
1236
|
+
else:
|
|
1237
|
+
self.exercise_status.setText(
|
|
1238
|
+
tr("Choose a level to generate a verified practice exercise.")
|
|
1239
|
+
)
|
|
1240
|
+
|
|
1241
|
+
def _start_generated_exercise(self) -> None:
|
|
1242
|
+
if self._pending_exercise is None:
|
|
1243
|
+
return
|
|
1244
|
+
self._active_exercise = self._pending_exercise
|
|
1245
|
+
self._simulation = self._pending_exercise.simulation
|
|
1246
|
+
self._mode = self._pending_exercise.mode
|
|
1247
|
+
self._reset_practice_state()
|
|
1248
|
+
self.screens.setCurrentWidget(self.practice_screen)
|
|
1249
|
+
self._render_step()
|
|
1250
|
+
|
|
1251
|
+
def _current_step(self):
|
|
1252
|
+
if self._simulation is None:
|
|
1253
|
+
return None
|
|
1254
|
+
return self._simulation.steps[self._step_index]
|
|
1255
|
+
|
|
1256
|
+
def _render_step(self) -> None:
|
|
1257
|
+
step = self._current_step()
|
|
1258
|
+
if step is None:
|
|
1259
|
+
return
|
|
1260
|
+
total = len(self._simulation.steps)
|
|
1261
|
+
if self._simulation.mode == "code":
|
|
1262
|
+
self.progress_label.setText(
|
|
1263
|
+
tr("PROGRAM STEP {current} OF {total}",
|
|
1264
|
+
current=self._step_index + 1, total=total)
|
|
1265
|
+
)
|
|
1266
|
+
else:
|
|
1267
|
+
self.progress_label.setText(
|
|
1268
|
+
tr("STEP {current} OF {total} · {function}",
|
|
1269
|
+
current=self._step_index + 1, total=total, function=step.function_name)
|
|
1270
|
+
)
|
|
1271
|
+
self.progress_bar.setRange(0, max(1, total))
|
|
1272
|
+
self.progress_bar.setValue(self._step_index + 1)
|
|
1273
|
+
source = self._simulation.sources[step.function_id]
|
|
1274
|
+
if self._simulation.mode == "code":
|
|
1275
|
+
self.source_heading.setText(
|
|
1276
|
+
tr("WHOLE PROGRAM · {file}", file=source["file"])
|
|
1277
|
+
)
|
|
1278
|
+
self.stack_heading.setText(tr("PROGRAM CALL STACK"))
|
|
1279
|
+
self.variables_heading.setText(tr("PROGRAM VARIABLES"))
|
|
1280
|
+
else:
|
|
1281
|
+
self.source_heading.setText(
|
|
1282
|
+
tr("{function} · {file}",
|
|
1283
|
+
function=step.function_name, file=source["file"])
|
|
1284
|
+
)
|
|
1285
|
+
self.stack_heading.setText(tr("CALL STACK"))
|
|
1286
|
+
self.variables_heading.setText(tr("LOCAL VARIABLES"))
|
|
1287
|
+
self._set_highlighted_source(source["source"], source["line"], step.line)
|
|
1288
|
+
self.step_prompt.setText(
|
|
1289
|
+
self._prompt_for(
|
|
1290
|
+
step.kind, step.function_name, step.target, step.expression
|
|
1291
|
+
)
|
|
1292
|
+
)
|
|
1293
|
+
self.expression_label.setText(step.expression or tr("Follow the current function state."))
|
|
1294
|
+
self.stack_list.clear()
|
|
1295
|
+
for frame in step.stack:
|
|
1296
|
+
values = ", ".join(
|
|
1297
|
+
f"{name}={self._format_value(value)}"
|
|
1298
|
+
for name, value in frame.get("variables", {}).items()
|
|
1299
|
+
)
|
|
1300
|
+
self.stack_list.addItem(
|
|
1301
|
+
f"{frame.get('function_name', '')}({values})"
|
|
1302
|
+
)
|
|
1303
|
+
self.variables_list.clear()
|
|
1304
|
+
for name, value in step.variables.items():
|
|
1305
|
+
self.variables_list.addItem(f"{name} = {self._format_value(value)}")
|
|
1306
|
+
self._render_challenge(step)
|
|
1307
|
+
self.previous_button.setEnabled(self._step_index > 0)
|
|
1308
|
+
self.restart_button.setEnabled(True)
|
|
1309
|
+
can_continue = not step.challenge or self._step_index in self._passed
|
|
1310
|
+
self.next_button.setEnabled(can_continue)
|
|
1311
|
+
self.next_button.setText(
|
|
1312
|
+
tr("Finish ✓") if self._step_index == total - 1 else tr("Next →")
|
|
1313
|
+
)
|
|
1314
|
+
self.hint_button.setEnabled(step.challenge and self._step_index not in self._passed)
|
|
1315
|
+
self.answer_button.setEnabled(step.challenge and self._step_index not in self._passed)
|
|
1316
|
+
|
|
1317
|
+
def _prompt_for(
|
|
1318
|
+
self,
|
|
1319
|
+
kind: str,
|
|
1320
|
+
function: str,
|
|
1321
|
+
target: str | None,
|
|
1322
|
+
expression: str = "",
|
|
1323
|
+
) -> str:
|
|
1324
|
+
if kind == "program_start":
|
|
1325
|
+
return tr("Predict the first meaningful operation.")
|
|
1326
|
+
if kind == "program_end":
|
|
1327
|
+
return tr("The program finished. Review its final output.")
|
|
1328
|
+
if kind == "enter":
|
|
1329
|
+
return tr("You are now {function}. The caller passed these arguments.", function=function)
|
|
1330
|
+
if kind == "arithmetic":
|
|
1331
|
+
return tr("Work out the value assigned to {target}.", target=target or "")
|
|
1332
|
+
if kind == "assignment":
|
|
1333
|
+
return tr("State update: {expression}", expression=expression)
|
|
1334
|
+
if kind == "loop_condition" and target:
|
|
1335
|
+
return tr("Will the for loop execute another pass?")
|
|
1336
|
+
if kind in {"condition", "loop_condition"}:
|
|
1337
|
+
return tr("Does this condition evaluate to True or False?")
|
|
1338
|
+
if kind == "loop_value":
|
|
1339
|
+
return tr("What value is assigned to {target} in the next loop pass?", target=target or "")
|
|
1340
|
+
if kind == "iteration":
|
|
1341
|
+
return tr("Loop state: {expression}", expression=expression)
|
|
1342
|
+
if kind == "output":
|
|
1343
|
+
return tr("What text does this print call output?")
|
|
1344
|
+
if kind == "next_operation":
|
|
1345
|
+
return tr("Predict the first meaningful operation.")
|
|
1346
|
+
if kind == "function_call":
|
|
1347
|
+
return tr("Which project function is being called here?")
|
|
1348
|
+
if kind == "call_return":
|
|
1349
|
+
return tr("The called function returned. Its value is back with you.")
|
|
1350
|
+
if kind == "return":
|
|
1351
|
+
return tr("What value does {function} return to its caller?", function=function)
|
|
1352
|
+
return tr("Follow the highlighted line, then continue.")
|
|
1353
|
+
|
|
1354
|
+
def _set_highlighted_source(self, source: str, first_line: int, active_line: int) -> None:
|
|
1355
|
+
self.source_view.setPlainText(source)
|
|
1356
|
+
block_number = max(0, active_line - first_line)
|
|
1357
|
+
cursor = QTextCursor(self.source_view.document())
|
|
1358
|
+
cursor.movePosition(QTextCursor.MoveOperation.Start)
|
|
1359
|
+
for _ in range(block_number):
|
|
1360
|
+
if not cursor.movePosition(QTextCursor.MoveOperation.NextBlock):
|
|
1361
|
+
break
|
|
1362
|
+
selection = QTextEdit.ExtraSelection()
|
|
1363
|
+
selection.cursor = cursor
|
|
1364
|
+
selection.cursor.select(QTextCursor.SelectionType.LineUnderCursor)
|
|
1365
|
+
selection.format.setBackground(QColor("#51402d"))
|
|
1366
|
+
selection.format.setForeground(QColor("#fff2df"))
|
|
1367
|
+
self.source_view.setExtraSelections([selection])
|
|
1368
|
+
|
|
1369
|
+
def _render_challenge(self, step) -> None:
|
|
1370
|
+
self.feedback_label.hide()
|
|
1371
|
+
self.hint_label.hide()
|
|
1372
|
+
self.history_list.clear()
|
|
1373
|
+
for record in self._history.get(self._step_index, []):
|
|
1374
|
+
if record["kind"] == "attempt":
|
|
1375
|
+
status = tr("Correct") if record["correct"] else tr("Try again")
|
|
1376
|
+
text = tr("Attempt {number}: {answer} · {status}",
|
|
1377
|
+
number=record["number"], answer=record["answer"], status=status)
|
|
1378
|
+
else:
|
|
1379
|
+
text = tr("Answer revealed: {answer}", answer=record["answer"])
|
|
1380
|
+
self.history_list.addItem(text)
|
|
1381
|
+
if not step.challenge:
|
|
1382
|
+
note = tr("Follow the highlighted line. Continue when you are ready.")
|
|
1383
|
+
if step.kind == "enter":
|
|
1384
|
+
note = tr("Review the caller's values and local state, then continue.")
|
|
1385
|
+
elif step.kind == "call_return":
|
|
1386
|
+
note = tr("The nested call has returned. Continue with the result shown.")
|
|
1387
|
+
elif step.kind == "assignment":
|
|
1388
|
+
note = tr("State updated: {state}", state=step.expression)
|
|
1389
|
+
elif step.kind == "iteration":
|
|
1390
|
+
note = tr("Loop pass state: {state}", state=step.expression)
|
|
1391
|
+
elif step.kind == "program_end":
|
|
1392
|
+
output = self._simulation.output.rstrip("\n") if self._simulation else ""
|
|
1393
|
+
note = tr("Program output: {output}", output=output or tr("No output was produced."))
|
|
1394
|
+
elif step.kind == "program_start":
|
|
1395
|
+
note = tr("Review the source before the first operation.")
|
|
1396
|
+
self.challenge_prompt.setText(note)
|
|
1397
|
+
self.answer_row.hide()
|
|
1398
|
+
self.choice_row.hide()
|
|
1399
|
+
return
|
|
1400
|
+
|
|
1401
|
+
prompt = {
|
|
1402
|
+
"arithmetic": tr("Calculate the result using the values shown."),
|
|
1403
|
+
"condition": tr("Choose the branch that matches this condition."),
|
|
1404
|
+
"loop_condition": tr("Choose whether the loop continues or stops."),
|
|
1405
|
+
"loop_value": tr("Enter the next loop value as a JSON value."),
|
|
1406
|
+
"function_call": tr("Choose the function called at this line."),
|
|
1407
|
+
"program_start": tr("Choose the first meaningful operation in the program."),
|
|
1408
|
+
"next_operation": tr("Choose the first meaningful operation in the program."),
|
|
1409
|
+
"output": tr("Enter the printed text as a JSON string."),
|
|
1410
|
+
"return": tr("Enter the value being returned. Use JSON for text or collections."),
|
|
1411
|
+
}.get(step.kind, tr("Enter your answer as a JSON value."))
|
|
1412
|
+
self.challenge_prompt.setText(prompt)
|
|
1413
|
+
passed = self._step_index in self._passed
|
|
1414
|
+
self.answer_edit.setText(self._drafts.get(self._step_index, ""))
|
|
1415
|
+
self.answer_edit.setEnabled(not passed)
|
|
1416
|
+
self.check_button.setEnabled(not passed)
|
|
1417
|
+
|
|
1418
|
+
while self.choice_layout.count():
|
|
1419
|
+
item = self.choice_layout.takeAt(0)
|
|
1420
|
+
widget = item.widget()
|
|
1421
|
+
if widget is not None:
|
|
1422
|
+
widget.deleteLater()
|
|
1423
|
+
if step.kind in {
|
|
1424
|
+
"condition", "loop_condition", "function_call", "program_start",
|
|
1425
|
+
"next_operation",
|
|
1426
|
+
}:
|
|
1427
|
+
self.answer_row.hide()
|
|
1428
|
+
self.choice_row.show()
|
|
1429
|
+
options = (
|
|
1430
|
+
["True", "False"]
|
|
1431
|
+
if step.kind in {"condition", "loop_condition"}
|
|
1432
|
+
else step.choices
|
|
1433
|
+
)
|
|
1434
|
+
for option in options:
|
|
1435
|
+
button = QPushButton(self._choice_label(step.kind, option))
|
|
1436
|
+
button.setObjectName("PovSecondary")
|
|
1437
|
+
button.setEnabled(not passed)
|
|
1438
|
+
button.clicked.connect(
|
|
1439
|
+
lambda _checked=False, value=option, kind=step.kind:
|
|
1440
|
+
self._submit_answer(
|
|
1441
|
+
value == "True"
|
|
1442
|
+
if kind in {"condition", "loop_condition"}
|
|
1443
|
+
else value,
|
|
1444
|
+
self._choice_label(kind, value),
|
|
1445
|
+
)
|
|
1446
|
+
)
|
|
1447
|
+
self.choice_layout.addWidget(button)
|
|
1448
|
+
else:
|
|
1449
|
+
self.choice_row.hide()
|
|
1450
|
+
self.answer_row.show()
|
|
1451
|
+
if passed:
|
|
1452
|
+
self.feedback_label.setText(
|
|
1453
|
+
tr("This challenge is complete. You can review your answer history.")
|
|
1454
|
+
)
|
|
1455
|
+
self.feedback_label.show()
|
|
1456
|
+
|
|
1457
|
+
@staticmethod
|
|
1458
|
+
def _choice_label(kind: str, option: str) -> str:
|
|
1459
|
+
if kind in {"condition", "loop_condition"}:
|
|
1460
|
+
return tr("True") if option == "True" else tr("False")
|
|
1461
|
+
if kind in {"program_start", "next_operation"}:
|
|
1462
|
+
return {
|
|
1463
|
+
"branch": tr("Branch decision"),
|
|
1464
|
+
"loop": tr("Loop iteration"),
|
|
1465
|
+
"variable update": tr("Variable update"),
|
|
1466
|
+
"function call": tr("Function call"),
|
|
1467
|
+
"output": tr("Output"),
|
|
1468
|
+
}.get(option, option)
|
|
1469
|
+
return option
|
|
1470
|
+
|
|
1471
|
+
def _submit_text_answer(self) -> None:
|
|
1472
|
+
raw = self.answer_edit.text().strip()
|
|
1473
|
+
self._drafts[self._step_index] = raw
|
|
1474
|
+
if not raw:
|
|
1475
|
+
self._show_feedback(tr("Enter an answer before checking it."), False)
|
|
1476
|
+
return
|
|
1477
|
+
value, error = _decode_json_input(raw)
|
|
1478
|
+
if error == "invalid":
|
|
1479
|
+
self._show_feedback(
|
|
1480
|
+
tr("Use a JSON value: for example 4, true, or \"hello\"."),
|
|
1481
|
+
False,
|
|
1482
|
+
)
|
|
1483
|
+
return
|
|
1484
|
+
if error == "too_long":
|
|
1485
|
+
self._show_feedback(
|
|
1486
|
+
tr(
|
|
1487
|
+
"JSON input is too long (maximum {limit} characters).",
|
|
1488
|
+
limit=MAX_JSON_INPUT_CHARS,
|
|
1489
|
+
),
|
|
1490
|
+
False,
|
|
1491
|
+
)
|
|
1492
|
+
return
|
|
1493
|
+
if error == "unsafe":
|
|
1494
|
+
self._show_feedback(
|
|
1495
|
+
tr("This JSON value is too large, too deeply nested, or not finite."),
|
|
1496
|
+
False,
|
|
1497
|
+
)
|
|
1498
|
+
return
|
|
1499
|
+
self._submit_answer(value, raw)
|
|
1500
|
+
|
|
1501
|
+
def _submit_answer(self, value: Any, display: str) -> None:
|
|
1502
|
+
if self._simulation is None:
|
|
1503
|
+
return
|
|
1504
|
+
step = self._current_step()
|
|
1505
|
+
if step is None or not step.challenge or self._step_index in self._passed:
|
|
1506
|
+
return
|
|
1507
|
+
result = self._simulation.check(self._step_index, value)
|
|
1508
|
+
records = self._history.setdefault(self._step_index, [])
|
|
1509
|
+
records.append({
|
|
1510
|
+
"kind": "attempt",
|
|
1511
|
+
"number": sum(record["kind"] == "attempt" for record in records) + 1,
|
|
1512
|
+
"answer": display,
|
|
1513
|
+
"correct": result["correct"],
|
|
1514
|
+
})
|
|
1515
|
+
if result["correct"]:
|
|
1516
|
+
self._passed.add(self._step_index)
|
|
1517
|
+
self._solved_by_attempt.add(self._step_index)
|
|
1518
|
+
self._render_step()
|
|
1519
|
+
if result["correct"]:
|
|
1520
|
+
self._show_feedback(
|
|
1521
|
+
tr("Correct. {answer} is the value for this step.",
|
|
1522
|
+
answer=self._format_value(result.get("answer"))),
|
|
1523
|
+
True,
|
|
1524
|
+
)
|
|
1525
|
+
else:
|
|
1526
|
+
feedback = {
|
|
1527
|
+
"arithmetic": tr(
|
|
1528
|
+
"Not quite. Substitute the current values, then apply the operator. "
|
|
1529
|
+
"For an update, start with the variable's previous value."
|
|
1530
|
+
),
|
|
1531
|
+
"condition": tr(
|
|
1532
|
+
"Not quite. Evaluate the comparison with the values shown, then follow "
|
|
1533
|
+
"the matching True or False branch."
|
|
1534
|
+
),
|
|
1535
|
+
"loop_value": tr(
|
|
1536
|
+
"Not quite. Read the iterable from left to right; each loop pass uses "
|
|
1537
|
+
"the next value, not the previous one."
|
|
1538
|
+
),
|
|
1539
|
+
"loop_condition": tr(
|
|
1540
|
+
"Not quite. Re-evaluate the loop condition with the updated variables "
|
|
1541
|
+
"to decide whether another iteration occurs."
|
|
1542
|
+
),
|
|
1543
|
+
"output": tr(
|
|
1544
|
+
"Not quite. Evaluate each print argument in order, convert the values "
|
|
1545
|
+
"to text, and join them with spaces."
|
|
1546
|
+
),
|
|
1547
|
+
"return": tr(
|
|
1548
|
+
"Not quite. Evaluate the return expression using this function's local "
|
|
1549
|
+
"variables; that value is passed back to the caller."
|
|
1550
|
+
),
|
|
1551
|
+
"function_call": tr(
|
|
1552
|
+
"Not quite. Match the called function name, then trace how its arguments "
|
|
1553
|
+
"become local parameter values."
|
|
1554
|
+
),
|
|
1555
|
+
"program_start": tr(
|
|
1556
|
+
"Not quite. Look past simple assignments for the first decision, loop, update, call, or output."
|
|
1557
|
+
),
|
|
1558
|
+
"next_operation": tr(
|
|
1559
|
+
"Not quite. Look past simple assignments for the first decision, loop, update, call, or output."
|
|
1560
|
+
),
|
|
1561
|
+
}.get(
|
|
1562
|
+
step.kind,
|
|
1563
|
+
tr("Not quite. Compare your answer with the highlighted expression and current values, then try again."),
|
|
1564
|
+
)
|
|
1565
|
+
if step.kind == "loop_condition" and step.target:
|
|
1566
|
+
feedback = tr(
|
|
1567
|
+
"Not quite. Check whether another value remains in the iterable."
|
|
1568
|
+
)
|
|
1569
|
+
self._show_feedback(feedback, False)
|
|
1570
|
+
|
|
1571
|
+
def _show_feedback(self, message: str, correct: bool) -> None:
|
|
1572
|
+
self.feedback_label.setText(message)
|
|
1573
|
+
self.feedback_label.setStyleSheet(
|
|
1574
|
+
"color: #a8e8d0; background: #1c302c; border: 1px solid #3b7568; "
|
|
1575
|
+
"border-radius: 6px; padding: 8px;"
|
|
1576
|
+
if correct else ""
|
|
1577
|
+
)
|
|
1578
|
+
self.feedback_label.show()
|
|
1579
|
+
|
|
1580
|
+
def _show_hint(self) -> None:
|
|
1581
|
+
step = self._current_step()
|
|
1582
|
+
if step is None or not step.challenge or self._simulation is None:
|
|
1583
|
+
return
|
|
1584
|
+
hint = {
|
|
1585
|
+
"arithmetic": tr("Substitute the local values into the expression, then follow operator order."),
|
|
1586
|
+
"condition": tr("Substitute the local values and compare them to choose the matching branch."),
|
|
1587
|
+
"loop_value": tr("Follow the iterable from left to right and find the next value."),
|
|
1588
|
+
"output": tr("Evaluate each print argument, convert it to text, then join them with spaces."),
|
|
1589
|
+
"next_operation": tr("Skip simple assignments and find the first decision, loop, update, call, or output."),
|
|
1590
|
+
"program_start": tr("Skip simple assignments and find the first decision, loop, update, call, or output."),
|
|
1591
|
+
"function_call": tr("Look at the function name immediately before the parentheses."),
|
|
1592
|
+
"return": tr("Evaluate the return expression using the local variables shown."),
|
|
1593
|
+
}.get(step.kind, self._simulation.hint(self._step_index))
|
|
1594
|
+
if step.kind == "loop_condition":
|
|
1595
|
+
hint = (
|
|
1596
|
+
tr("Check whether any values remain in the iterable.")
|
|
1597
|
+
if step.target
|
|
1598
|
+
else tr("Substitute the current values into the while condition.")
|
|
1599
|
+
)
|
|
1600
|
+
self.hint_label.setText(hint)
|
|
1601
|
+
self.hint_label.show()
|
|
1602
|
+
|
|
1603
|
+
def _show_answer(self) -> None:
|
|
1604
|
+
if self._simulation is None:
|
|
1605
|
+
return
|
|
1606
|
+
step = self._current_step()
|
|
1607
|
+
if step is None or not step.challenge or self._step_index in self._passed:
|
|
1608
|
+
return
|
|
1609
|
+
answer = self._simulation.answer(self._step_index)
|
|
1610
|
+
display = self._format_value(answer)
|
|
1611
|
+
self._history.setdefault(self._step_index, []).append({
|
|
1612
|
+
"kind": "reveal",
|
|
1613
|
+
"answer": display,
|
|
1614
|
+
})
|
|
1615
|
+
self._revealed.add(self._step_index)
|
|
1616
|
+
self._passed.add(self._step_index)
|
|
1617
|
+
self._render_step()
|
|
1618
|
+
self._show_feedback(
|
|
1619
|
+
tr("Answer: {answer}. Review the highlighted line before continuing.",
|
|
1620
|
+
answer=display),
|
|
1621
|
+
True,
|
|
1622
|
+
)
|
|
1623
|
+
|
|
1624
|
+
def _previous_step(self) -> None:
|
|
1625
|
+
if self._step_index > 0:
|
|
1626
|
+
if self.answer_edit.isVisible():
|
|
1627
|
+
self._drafts[self._step_index] = self.answer_edit.text()
|
|
1628
|
+
self._step_index -= 1
|
|
1629
|
+
self._render_step()
|
|
1630
|
+
|
|
1631
|
+
def _next_step(self) -> None:
|
|
1632
|
+
step = self._current_step()
|
|
1633
|
+
if step is None or (step.challenge and self._step_index not in self._passed):
|
|
1634
|
+
return
|
|
1635
|
+
if self._step_index == len(self._simulation.steps) - 1:
|
|
1636
|
+
self._complete()
|
|
1637
|
+
return
|
|
1638
|
+
if self.answer_edit.isVisible():
|
|
1639
|
+
self._drafts[self._step_index] = self.answer_edit.text()
|
|
1640
|
+
self._step_index += 1
|
|
1641
|
+
self._render_step()
|
|
1642
|
+
|
|
1643
|
+
def _restart(self) -> None:
|
|
1644
|
+
if self._simulation is None:
|
|
1645
|
+
return
|
|
1646
|
+
self._step_index = 0
|
|
1647
|
+
self._completion_metrics = None
|
|
1648
|
+
self._passed.clear()
|
|
1649
|
+
self._solved_by_attempt.clear()
|
|
1650
|
+
self._revealed.clear()
|
|
1651
|
+
self._history.clear()
|
|
1652
|
+
self._drafts.clear()
|
|
1653
|
+
self.screens.setCurrentWidget(self.practice_screen)
|
|
1654
|
+
self._render_step()
|
|
1655
|
+
|
|
1656
|
+
def _complete(self) -> None:
|
|
1657
|
+
if self._simulation is None:
|
|
1658
|
+
return
|
|
1659
|
+
challenges = [index for index, step in enumerate(self._simulation.steps) if step.challenge]
|
|
1660
|
+
attempts = sum(
|
|
1661
|
+
record["kind"] == "attempt"
|
|
1662
|
+
for history in self._history.values()
|
|
1663
|
+
for record in history
|
|
1664
|
+
)
|
|
1665
|
+
self._completion_metrics = {
|
|
1666
|
+
"entry_name": self._simulation.entry_name,
|
|
1667
|
+
"total": len(challenges),
|
|
1668
|
+
"solved": len(self._solved_by_attempt),
|
|
1669
|
+
"revealed": len(self._revealed),
|
|
1670
|
+
"attempts": attempts,
|
|
1671
|
+
"value": self._final_return_value(),
|
|
1672
|
+
"output": self._simulation.output.rstrip("\n"),
|
|
1673
|
+
"variables": self._format_value(self._simulation.final_variables),
|
|
1674
|
+
"practice_level": (
|
|
1675
|
+
self._active_exercise.level if self._active_exercise else None
|
|
1676
|
+
),
|
|
1677
|
+
"concepts": (
|
|
1678
|
+
self._active_exercise.concepts if self._active_exercise else ()
|
|
1679
|
+
),
|
|
1680
|
+
}
|
|
1681
|
+
self._render_completion_summary()
|
|
1682
|
+
self.screens.setCurrentWidget(self.completion_screen)
|
|
1683
|
+
|
|
1684
|
+
def _render_completion_summary(self) -> None:
|
|
1685
|
+
if self._completion_metrics is None:
|
|
1686
|
+
return
|
|
1687
|
+
metrics = self._completion_metrics
|
|
1688
|
+
practice_summary = ""
|
|
1689
|
+
if metrics["practice_level"]:
|
|
1690
|
+
level = (
|
|
1691
|
+
tr("New Values")
|
|
1692
|
+
if metrics["practice_level"] == "new_values"
|
|
1693
|
+
else tr("Similar Logic")
|
|
1694
|
+
)
|
|
1695
|
+
concepts = ", ".join(tr(concept) for concept in metrics["concepts"])
|
|
1696
|
+
practice_summary = "\n" + tr(
|
|
1697
|
+
"Practice level: {level} · Concepts: {concepts}",
|
|
1698
|
+
level=level,
|
|
1699
|
+
concepts=concepts,
|
|
1700
|
+
)
|
|
1701
|
+
if self._simulation is not None and self._simulation.mode == "code":
|
|
1702
|
+
output = metrics["output"] or tr("No output was produced.")
|
|
1703
|
+
if len(output) > 1600:
|
|
1704
|
+
output = output[:1600] + "\n" + tr("Output shortened for display.")
|
|
1705
|
+
self.completion_summary.setText(
|
|
1706
|
+
tr("Mode: Be the Code\nChallenges: {total} · Solved by you: {solved} · "
|
|
1707
|
+
"Answers revealed: {revealed} · Attempts: {attempts}{practice}\n"
|
|
1708
|
+
"Final output:\n{output}\nFinal variables: {variables}",
|
|
1709
|
+
total=metrics["total"],
|
|
1710
|
+
solved=metrics["solved"],
|
|
1711
|
+
revealed=metrics["revealed"],
|
|
1712
|
+
attempts=metrics["attempts"],
|
|
1713
|
+
practice=practice_summary,
|
|
1714
|
+
output=output,
|
|
1715
|
+
variables=metrics["variables"])
|
|
1716
|
+
)
|
|
1717
|
+
else:
|
|
1718
|
+
function_name = metrics["entry_name"]
|
|
1719
|
+
self.completion_summary.setText(
|
|
1720
|
+
tr("Function: {function}\nChallenges: {total} · Solved by you: {solved} · "
|
|
1721
|
+
"Answers revealed: {revealed} · Attempts: {attempts}{practice}\nFinal return value: {value}",
|
|
1722
|
+
function=function_name,
|
|
1723
|
+
total=metrics["total"],
|
|
1724
|
+
solved=metrics["solved"],
|
|
1725
|
+
revealed=metrics["revealed"],
|
|
1726
|
+
attempts=metrics["attempts"],
|
|
1727
|
+
practice=practice_summary,
|
|
1728
|
+
value=metrics["value"])
|
|
1729
|
+
)
|
|
1730
|
+
|
|
1731
|
+
def _final_return_value(self) -> str:
|
|
1732
|
+
if self._simulation is None:
|
|
1733
|
+
return tr("No explicit return value.")
|
|
1734
|
+
return_steps = [
|
|
1735
|
+
index for index, step in enumerate(self._simulation.steps)
|
|
1736
|
+
if step.kind == "return" and step.function_id == self._simulation.function_id
|
|
1737
|
+
]
|
|
1738
|
+
if not return_steps:
|
|
1739
|
+
return tr("No explicit return value.")
|
|
1740
|
+
return self._format_value(self._simulation.answer(return_steps[-1]))
|
|
1741
|
+
|
|
1742
|
+
def _update_progress_labels(self) -> None:
|
|
1743
|
+
step = self._current_step()
|
|
1744
|
+
if step is None:
|
|
1745
|
+
self.progress_label.setText(tr("FUNCTION POV"))
|
|
1746
|
+
|
|
1747
|
+
@staticmethod
|
|
1748
|
+
def _format_value(value: Any) -> str:
|
|
1749
|
+
return json.dumps(value, ensure_ascii=False, sort_keys=True)
|