View sourcecode

The following files exists in this folder. Click to view.

answer-py-assert_tpl.php

1 lines ASCII Unix (LF)
1
#!/usr/bin/env python3

"""                                               
Python Dbwebb module for asserting and auto correcting labs.

It reads the answers from a json-file and uses ut
for checking with assertEqual().

"""

import json

class Dbwebb():
    """
    Class for autocorrecting labs.
    """

    def __init__(self, answersFileName="answer.json"):
        """
        Init by reading json-file with answers.
        """
        self.answers = json.load(open(answersFileName))


    def assertEqual(self, question, answer, hint=False):
        """
        Check if the answer is correct or not, present a hint if asked for.
        """

        status = None
        noanswer = "Replace this text with the answer or the variable holding it."

        if answer == noanswer:
            status = question + " NOT YET DONE."
        
        elif answer == self.answers["answers"][question]:
            status = question + " CORRECT. Well done!\n" + str(answer)
        
        else:
            status = question + " FAIL.\nYou said:\n" + answer
            status += "\nHint:\n" + str(self.answers["answers"][question]) if hint else ""

        return status