Source code for gradelang.state
""" State manager.
"""
import json
from typing import List
from gradelang.question import Question
NIL = ('nil',)
[docs]class State:
symbol_table: dict
setup: tuple
_questions: List[Question]
teardown: tuple
output: dict
question: Question
def __init__(self):
self.clean()
def reset(self):
self.symbol_table = dict()
return
def clean(self):
self.reset()
self.setup = NIL
self._questions = list()
self.teardown = NIL
self.output = {NIL: ''}
return
@property
def questions(self):
yield from self._questions
def add_question(self, name, body):
self._questions.append(
Question(
name=name if name != NIL else len(self._questions),
body=body,
)
)
return
def score(self):
return sum(q.score for q in self.questions)
def max_score(self):
return sum(q.max_score for q in self.questions)
def json(self):
return json.dumps(
{
'tests': [q.json() for q in self.questions]
},
indent=4
)
def markdown(self):
return '\n'.join(filter(lambda x: x, [
'# Results',
f'## Score: {self.score()}/{self.max_score()}',
*[q.markdown() for q in self.questions]
]))
def report(self):
return '\n'.join(filter(lambda x: x, [
'Grade Results',
*[q.report() for q in self.questions]
]))
state = State()