Skip to content

Commit

Permalink
refactor: avoid validating twice when exporting in QuestionWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
MHajoha committed Sep 18, 2024
1 parent 4472cb2 commit 2951478
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions questionpy/_wrappers/_question.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pydantic import JsonValue

from questionpy import Attempt, Question
from questionpy_common.api.attempt import AttemptModel, AttemptScoredModel, AttemptStartedModel, AttemptUi, ScoreModel
from questionpy_common.api.attempt import AttemptModel, AttemptScoredModel, AttemptStartedModel, AttemptUi
from questionpy_common.api.question import QuestionInterface, QuestionModel
from questionpy_common.environment import get_qpy_environment

Expand Down Expand Up @@ -43,11 +43,11 @@ def _export_question(question: Question) -> QuestionModel:
)


def _export_attempt(attempt: Attempt) -> AttemptModel:
return AttemptModel(
lang=_get_output_lang(),
variant=attempt.attempt_state.variant,
ui=AttemptUi(
def _export_attempt(attempt: Attempt) -> dict:
return {
"lang": _get_output_lang(),
"variant": attempt.attempt_state.variant,
"ui": AttemptUi(
formulation=attempt.formulation,
general_feedback=attempt.general_feedback,
specific_feedback=attempt.specific_feedback,
Expand All @@ -57,19 +57,19 @@ def _export_attempt(attempt: Attempt) -> AttemptModel:
files=attempt.files,
cache_control=attempt.cache_control,
),
)
}


def _export_score(attempt: Attempt) -> ScoreModel:
def _export_score(attempt: Attempt) -> dict:
plain_scoring_state = attempt.to_plain_scoring_state()
return ScoreModel(
scoring_state=None if plain_scoring_state is None else json.dumps(plain_scoring_state),
scoring_code=attempt.scoring_code,
score=attempt.score,
score_final=attempt.score_final,
scored_inputs=attempt.scored_inputs,
scored_subquestions={},
)
return {
"scoring_state": None if plain_scoring_state is None else json.dumps(plain_scoring_state),
"scoring_code": attempt.scoring_code,
"score": attempt.score,
"score_final": attempt.score_final,
"scored_inputs": attempt.scored_inputs,
"scored_subquestions": {},
}


class QuestionWrapper(QuestionInterface):
Expand All @@ -79,9 +79,7 @@ def __init__(self, question: Question) -> None:
def start_attempt(self, variant: int) -> AttemptStartedModel:
attempt = self._question.start_attempt(variant)
plain_attempt_state = attempt.to_plain_attempt_state()
return AttemptStartedModel(
**_export_attempt(attempt).model_dump(), attempt_state=json.dumps(plain_attempt_state)
)
return AttemptStartedModel(**_export_attempt(attempt), attempt_state=json.dumps(plain_attempt_state))

def _get_attempt_internal(
self,
Expand All @@ -99,7 +97,7 @@ def _get_attempt_internal(
def get_attempt(
self, attempt_state: str, scoring_state: str | None = None, response: dict[str, JsonValue] | None = None
) -> AttemptModel:
return _export_attempt(self._get_attempt_internal(attempt_state, scoring_state, response))
return AttemptModel(**_export_attempt(self._get_attempt_internal(attempt_state, scoring_state, response)))

def score_attempt(
self,
Expand All @@ -112,7 +110,7 @@ def score_attempt(
) -> AttemptScoredModel:
attempt = self._get_attempt_internal(attempt_state, scoring_state, response)
attempt.score_response(try_scoring_with_countback=try_scoring_with_countback, try_giving_hint=try_giving_hint)
return AttemptScoredModel(**_export_attempt(attempt).model_dump(), **_export_score(attempt).model_dump())
return AttemptScoredModel(**_export_attempt(attempt), **_export_score(attempt))

def export_question_state(self) -> str:
plain_state = self._question.to_plain_state()
Expand Down

0 comments on commit 2951478

Please sign in to comment.