Skip to content

Self-assessment ​

Capytale users

Self-assessment was mainly developed for Capytale. If you use Basthon in Capytale, see Capytale's documentation on this subject (in French).

In Python, the basthon.autoeval module gives students immediate feedback on their code: does a variable have the right value? Does a function return the right results? In a sequenced notebook, a successful validation also unlocks the rest of the notebook.

Principle ​

The teacher writes, in a Python file, validation objects (instances of the classes of basthon.autoeval). The student calls these objects in the notebook (or the script) to check their work.

python
# file _validation.py
from basthon.autoeval import ValidateVariables, ValidateFunction

test_area = ValidateVariables({"area": 12.5})
test_square = ValidateFunction("square", [-2, -1, 0, 1, 2], target_values=[4, 1, 0, 1, 4])

The file is loaded as a module, for instance with the module URL parameter, then imported at the start of the notebook:

python
from _validation import *

Once they have written their square function, the student assesses it with:

python
test_square()

A message tells them whether the function is not defined, whether at least one test fails or whether everything is right.

Astuce

A file name that starts with _ (like _validation.py) does not draw the student's attention. To fine-tune the validations, write them first in a cell of the notebook, then move them to the file.

Validating variables ​

ValidateVariables checks that variables exist and have a value, given by a {name: expected value} dictionary:

python
from basthon.autoeval import ValidateVariables

test_variables = ValidateVariables({"pi": 3.14, "primes": [2, 3, 5, 7]})

Floating point numbers are compared with a tolerance (see equality).

Validating a function ​

ValidateFunction calls the student's function on test values and compares the results with the expected values, given directly (target_values) or computed by a reference function (valid_function). ValidateFunctionPretty does the same and shows the results of all the tests in a table.

python
from basthon.autoeval import ValidateFunction, ValidateFunctionPretty

test_square = ValidateFunction("square", [-2, -1, 0, 1, 2], target_values=[4, 1, 0, 1, 4])
test_absolute_value = ValidateFunctionPretty("absolute_value", range(-20, 21), valid_function=abs)

For a function with several parameters, each test value is a tuple:

python
from itertools import product

test_xor = ValidateFunctionPretty(
    "xor", product([True, False], repeat=2), valid_function=lambda x, y: x != y
)

For a function without parameters, the only test value is the empty tuple (): the function is called without arguments.

python
test_hello = ValidateFunction("hello", [()], target_values=["Hello!"])

Attention

With from _validation import *, a reference function with the name of the expected function would be imported into the notebook: the student would pass the tests without writing anything. Give it another name, preferably starting with _ (not imported by import *), or use target_values.

The signature of the function (number, names and types of the parameters) can also be checked with check_signature=True: it is compared with that of valid_function (or with a signature given directly to check_signature). ignore_names_in_signature=True does not check the names of the parameters.

python
def _absolute_value(x: float) -> float:
    return x if x > 0 else -x

test_absolute_value = ValidateFunction(
    "absolute_value", [x / 10 for x in range(-20, 21)],
    valid_function=_absolute_value, check_signature=True,
)

Writing your own validation ​

A validation class is decorated with @validationclass and defines the __call__ method, which returns True when the validation succeeds:

python
import sys
import math as _math
import __main__
from basthon.autoeval import validationclass

@validationclass
class ValidateMathModule:
    def __call__(self):
        if getattr(__main__, "math", None) is _math:
            print("Well done, you imported the math module!")
            return True
        print("The math module is not imported.", file=sys.stderr)
        return False

test_math = ValidateMathModule()

Validations count the student's attempts (trial_count()), which allows for instance a hint after several failures, by deriving an existing class:

python
@validationclass
class ValidateHarshad(ValidateVariables):
    def __init__(self):
        super().__init__({"harshad": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]})

    def __call__(self):
        success = super().__call__()
        if not success and self.trial_count() >= 5:
            print("Hint: a one-digit integer is always a harshad number.")
        return success

ValidateAll and ValidateAny gather several validations into a single one, which succeeds when all of them (or at least one of them) succeed:

python
from basthon.autoeval import ValidateAll

test_exercise = ValidateAll([test_square, test_absolute_value])

All the classes and their parameters are described on the API of the basthon Python module page.

Sequenced notebook ​

With the extensions=sequenced URL parameter (see The sequenced extension), the notebook is split by breakpoint cells: the cells that follow a breakpoint stay hidden until it is validated.

  • A code breakpoint cell is validated when its run successfully calls a validation object (for instance test_square()).
  • A Markdown breakpoint cell is validated when it is shown.
  • Breakpoints can be neither deleted, nor moved, nor merged with another cell. Code breakpoint cells are read-only.

A cell becomes a breakpoint with the breakpoint tag, added through the View › Cell Toolbar › Tags menu. Prepare the notebook without the sequenced extension, to see and edit every cell.

Astuce

The first cell of the notebook is often a breakpoint that imports the validations (from _validation import *). For it to be validated, the validation file can call a Validate() object, which always succeeds:

python
first_cell = Validate()
first_cell()

A validation that must not unlock the rest (to gather several tests in a single cell) is created with ignore_breakpoint=True.

Only the validations created in a file (the teacher's validation module) unlock the rest: a validation created in the notebook, for instance Validate()() or test_square = Validate(), unlocks nothing. The validations must therefore be written in a file, not in a cell.

Limits

These protections stop simple workarounds, not a student who reads Basthon's sources: their code runs in the same interpreter as the validations, and the rest of the notebook is already in the browser (and in the downloaded file). A sequenced notebook guides the student; it does not replace a supervised assessment.

An example of a sequenced notebook is in the gallery.