def get_score_type(submission=None, task=None): """Given a task, instantiate the corresponding ScoreType class. submission (Submission): the submission that needs the task type. task (Task): the task we want to score. return (object): an instance of the correct ScoreType class. """ # Validate arguments. if [x is not None for x in [submission, task]].count(True) != 1: raise ValueError("Need at most one way to get the score type.") if submission is not None: task = submission.task score_type_name = task.score_type try: score_type_parameters = json.loads(task.score_type_parameters) except json.decoder.JSONDecodeError as error: logger.error("Cannot decode score type parameters.\n%r." % error) raise public_testcases = dict((testcase.num, testcase.public) for testcase in task.testcases) cls = plugin_lookup(score_type_name, "cms.grading.scoretypes", "scoretypes") return cls(score_type_parameters, public_testcases)
def get_score_type(submission=None, task=None): """Given a task, istantiate the corresponding ScoreType class. submission (Submission): the submission that needs the task type. task (Task): the task we want to score. return (object): an instance of the correct ScoreType class. """ # Validate arguments. if [x is not None for x in [submission, task]].count(True) != 1: raise ValueError("Need at most one way to get the score type.") if submission is not None: task = submission.task score_type_name = task.score_type try: score_type_parameters = json.loads(task.score_parameters) except json.decoder.JSONDecodeError as error: logger.error("Cannot decode score type parameters.\n%r." % error) raise public_testcases = dict( (testcase.num, testcase.public) for testcase in task.testcases) cls = plugin_lookup(score_type_name, "cms.grading.scoretypes", "scoretypes") return cls(score_type_parameters, public_testcases)
def get_score_type(submission=None, task=None): """Given a task, istantiate the corresponding ScoreType class. submission (Submission): the submission that needs the task type. task (Task): the task we want to score. return (object): an instance of the correct ScoreType class. """ # Validate arguments. if [x is not None for x in [submission, task]].count(True) != 1: raise ValueError("Need at most one way to get the score type.") if submission is not None: task = submission.task score_type_name = task.score_type # TODO: manage exceptions when parameters cannot be decoded. score_type_parameters = json.loads(task.score_parameters) public_testcases = [testcase.public for testcase in task.testcases] cls = plugin_lookup(score_type_name, "cms.grading.scoretypes", "scoretypes") return cls(score_type_parameters, public_testcases)
def get_auth_type(auth_type_name=None): """Given an auth type name, find the corresponding AuthType class. auth_type_name (string): if we only need the class, we can give only the auth type name. return (object): an instance of the correct AuthType class. """ # Recover information from the arguments. return plugin_lookup(auth_type_name, "cms.server.authtypes", "authtypes")
def get_score_type(submission=None, task=None, dataset_id=None): """Given a task, instantiate the corresponding ScoreType class. submission (Submission): the submission that needs the task type. task (Task): the task we want to score. dataset_id (int): the dataset id to use, or None for active. return (object): an instance of the correct ScoreType class. """ # Validate arguments. if [x is not None for x in [submission, task]].count(True) != 1: raise ValueError("Need at most one way to get the score type.") if submission is not None: task = submission.task if dataset_id is None: dataset_id = task.active_dataset_id dataset = Dataset.get_from_id(dataset_id, task.sa_session) score_type_name = dataset.score_type try: score_type_parameters = json.loads(dataset.score_type_parameters) except json.decoder.JSONDecodeError as error: logger.error("Cannot decode score type parameters for task " "%d \"%s\", dataset %d \"%s\"\n%r." % ( task.id, task.name, dataset.id, dataset.description, error)) return None public_testcases = dict( (testcase.num, testcase.public) for testcase in dataset.testcases) cls = plugin_lookup(score_type_name, "cms.grading.scoretypes", "scoretypes") try: return cls(score_type_parameters, public_testcases) except Exception as error: logger.error("Cannot instantiate score type for task " "%d \"%s\", dataset %d \"%s\"\n%r." % ( task.id, task.name, dataset.id, dataset.description, error)) return None
def get_score_type(submission=None, task=None, dataset_id=None): """Given a task, instantiate the corresponding ScoreType class. submission (Submission): the submission that needs the task type. task (Task): the task we want to score. dataset_id (int): the dataset id to use, or None for active. return (object): an instance of the correct ScoreType class. """ # Validate arguments. if [x is not None for x in [submission, task]].count(True) != 1: raise ValueError("Need at most one way to get the score type.") if submission is not None: task = submission.task if dataset_id is None: dataset_id = task.active_dataset_id dataset = Dataset.get_from_id(dataset_id, task.sa_session) score_type_name = dataset.score_type try: score_type_parameters = json.loads(dataset.score_type_parameters) except json.decoder.JSONDecodeError as error: logger.error( "Cannot decode score type parameters for task " "%d \"%s\", dataset %d \"%s\"\n%r." % (task.id, task.name, dataset.id, dataset.description, error)) return None public_testcases = dict( (testcase.num, testcase.public) for testcase in dataset.testcases) cls = plugin_lookup(score_type_name, "cms.grading.scoretypes", "scoretypes") try: return cls(score_type_parameters, public_testcases) except Exception as error: logger.error( "Cannot instantiate score type for task " "%d \"%s\", dataset %d \"%s\"\n%r." % (task.id, task.name, dataset.id, dataset.description, error)) return None
def get_task_type(job=None, file_cacher=None, dataset=None, task_type_name=None): """Given a job, instantiate the corresponding TaskType class. job (Job): the job to perform. file_cacher (FileCacher): a file cacher object. dataset (Dataset): if we don't want to grade, but just to get information, we can provide only the dataset and not the submission. task_type_name (string): again, if we only need the class, we can give only the task type name. return (object): an instance of the correct TaskType class. """ # Validate arguments. if [x is not None for x in [job, dataset, task_type_name]].count(True) != 1: raise ValueError("Need exactly one way to get the task type.") elif [x is not None for x in [job, file_cacher]].count(True) not in [0, 2]: raise ValueError("Need file cacher to perform a job.") # Recover information from the arguments. task_type_parameters = None if job is not None: task_type_name = job.task_type if dataset is not None: task_type_name = dataset.task_type try: task_type_parameters = json.loads(dataset.task_type_parameters) except json.decoder.JSONDecodeError as error: logger.error("Cannot decode task type parameters.\n%r." % error) raise job = Job() job.task_type = task_type_name job.task_type_parameters = task_type_parameters cls = plugin_lookup(task_type_name, "cms.grading.tasktypes", "tasktypes") return cls(job, file_cacher)
def get_task_type(submission=None, file_cacher=None, task=None, task_type_name=None): """Given a submission, istantiate the corresponding TaskType class. submission (Submission): the submission that needs the task type. file_cacher (FileCacher): a file cacher object. task (Task): if we don't want to grade, but just to get information, we can provide only the task and not the submission. task_type_name (string): again, if we only need the class, we can give only the task type name. return (object): an instance of the correct TaskType class. """ # Validate arguments. if [x is not None for x in [submission, task, task_type_name]].count(True) != 1: raise ValueError("Need at most one way to get the task type.") elif [x is not None for x in [submission, file_cacher]].count(True) not in [0, 2]: raise ValueError("Need file cacher to grade a submission.") # Recover information from the arguments. task_type_parameters = None if submission is not None: task = submission.task if task is not None: task_type_name = task.task_type try: task_type_parameters = json.loads(task.task_type_parameters) except json.decoder.JSONDecodeError as error: logger.error("Cannot decode score type parameters.\n%r." % error) raise cls = plugin_lookup(task_type_name, "cms.grading.tasktypes", "tasktypes") return cls(submission, task_type_parameters, file_cacher)
def get_score_type(dataset): """Given a dataset, instantiate the corresponding ScoreType class. datset (Dataset): the Dataset whose ScoreType we want return (object): an instance of the correct ScoreType class. """ score_type_name = dataset.score_type try: score_type_parameters = json.loads(dataset.score_type_parameters) except json.decoder.JSONDecodeError as error: logger.error("Cannot decode score type parameters.\n%r." % error) raise public_testcases = dict((testcase.num, testcase.public) for testcase in dataset.testcases) cls = plugin_lookup(score_type_name, "cms.grading.scoretypes", "scoretypes") return cls(score_type_parameters, public_testcases)
def get_task_type_class(name): """Load the TaskType class given as parameter.""" return plugin_lookup(name, "cms.grading.tasktypes", "tasktypes")
def get_score_type_class(name): """Load the ScoreType class given as parameter.""" return plugin_lookup(name, "cms.grading.scoretypes", "scoretypes")
def get_task_type_class(name): return plugin_lookup(name, "cms.grading.tasktypes", "tasktypes")
def get_language_class(name): """Load the Language subclass given as parameter.""" return plugin_lookup(name, "cms.grading.languages", "languages")