From b36a9349cec7878a867c9aeb85f31143c790a1d5 Mon Sep 17 00:00:00 2001 From: Simona Wittner Date: Wed, 25 Dec 2024 16:51:08 +0100 Subject: [PATCH] Create student class --- krummstab/__init__.py | 11 ++++++----- krummstab/config.py | 12 +++++++----- krummstab/students.py | 16 ++++++++++++++++ krummstab/submissions.py | 5 ++++- krummstab/teams.py | 21 ++++++++++++++------- 5 files changed, 47 insertions(+), 18 deletions(-) create mode 100644 krummstab/students.py diff --git a/krummstab/__init__.py b/krummstab/__init__.py index 7862bd8..02f1110 100755 --- a/krummstab/__init__.py +++ b/krummstab/__init__.py @@ -636,7 +636,7 @@ def print_marks(_the_config: config.Config, sheet: sheets.Sheet) -> None: if submission.team == team_to_print: key = submission.team.get_team_key() for student in submission.team.members: - full_name = f"{student[0]} {student[1]}" + full_name = f"{student.first_name} {student.last_name}" output_str = f"{full_name:>35};" if _the_config.points_per == "exercise": # The value `marks` assigned to the team_dir key is a @@ -661,8 +661,8 @@ def create_individual_marks_file(_the_config: config.Config, sheet: sheets.Sheet student_marks = {} for submission in sheet.get_relevant_submissions(): team_key = submission.team.get_team_key() - for first_name, last_name, email in submission.team.members: - student_key = email.lower() + for student in submission.team.members: + student_key = student.email.lower() student_marks.update({student_key: team_marks.get(team_key)}) file_content = { "tutor_name": _the_config.tutor_name, @@ -1207,7 +1207,8 @@ def print_missing_submissions(_the_config: config.Config, sheet: sheets.Sheet) - print(f"* {missing_team.last_names_to_string()}") -def lookup_teams(_the_config: config.Config, team_dir: pathlib.Path): +def lookup_teams(_the_config: config.Config, + team_dir: pathlib.Path) -> tuple[str, list[Team]]: """ Extracts the team ID from the directory name and searches for teams based on the extracted email address from the subdirectory name. @@ -1218,7 +1219,7 @@ def lookup_teams(_the_config: config.Config, team_dir: pathlib.Path): teams = [ team for team in _the_config.teams - if any(submission_email in student for student in team.members) + if any(student.email == submission_email for student in team.members) ] return team_id, teams diff --git a/krummstab/config.py b/krummstab/config.py index 58b2ee5..58ae939 100644 --- a/krummstab/config.py +++ b/krummstab/config.py @@ -39,7 +39,8 @@ def __init__(self, config_paths: list[Path]) -> None: team.sort() self.teams.sort() - self.teams = [Team(team, None) for team in self.teams] + self.teams = [Team([Student(*student) for student in team], None) + for team in self.teams] _validate_teams(self.teams, self.max_team_size) logging.info("Processed config successfully.") @@ -50,7 +51,8 @@ def get_relevant_teams(self) -> list[Team]: access relevant teams via `get_relevant_submissions()`. """ if self.marking_mode == "static": - return [Team(team, None) for team in self.classes[self.tutor_name]] + return [Team([Student(*student) for student in team], None) + for team in self.classes[self.tutor_name]] elif self.marking_mode == "exercise": return self.teams else: @@ -68,9 +70,9 @@ def _validate_teams(teams: list[Team], max_team_size) -> None: if len(team.members) > max_team_size: logging.critical(f"Team with size {len(team.members)} violates maximal " f"team size.") - for first, last, email in team.members: - all_students.append((first, last)) - all_emails.append(email) + for member in team.members: + all_students.append((member.first_name, member.last_name)) + all_emails.append(member.email) if len(all_students) != len(set(all_students)): logging.critical("There are duplicate students in the config file!") if len(all_emails) != len(set(all_emails)): diff --git a/krummstab/students.py b/krummstab/students.py new file mode 100644 index 0000000..aa17a31 --- /dev/null +++ b/krummstab/students.py @@ -0,0 +1,16 @@ +class Student: + def __init__(self, first_name: str, last_name: str, email: str): + self.first_name = first_name + self.last_name = last_name + self.email = email + + def __eq__(self, other) -> bool: + return (self.first_name == other.first_name + and self.last_name == other.last_name + and self.email == other.email) + + def to_tuple(self) -> tuple[str, str, str]: + """ + Get a tuple of strings representation of a student. + """ + return self.first_name, self.last_name, self.email diff --git a/krummstab/submissions.py b/krummstab/submissions.py index 303f5b6..35b1c8a 100644 --- a/krummstab/submissions.py +++ b/krummstab/submissions.py @@ -5,6 +5,7 @@ import jsonschema from . import config, schemas, sheets +from .students import Student from .teams import Team SUBMISSION_INFO_FILE_NAME = "submission.json" @@ -82,7 +83,9 @@ def _load(self): resources.files(schemas).joinpath("submission-info-schema.json").read_text(encoding="utf-8")) jsonschema.validate(submission_info, submission_info_schema, jsonschema.Draft7Validator) self.team = Team( - submission_info.get("team"), submission_info.get("adam_id") + [Student(*student) for student + in submission_info.get("team")], + submission_info.get("adam_id") ) self.relevant = submission_info.get("relevant") except FileNotFoundError: diff --git a/krummstab/teams.py b/krummstab/teams.py index 87b8545..cf9ae2d 100644 --- a/krummstab/teams.py +++ b/krummstab/teams.py @@ -1,5 +1,10 @@ +from typing import Optional + +from .students import * + + class Team: - def __init__(self, members, adam_id): + def __init__(self, members: list[Student], adam_id: Optional[str] = None): self.members = members self.adam_id = adam_id @@ -10,13 +15,13 @@ def get_first_names(self) -> list[str]: """ Get a list of the first names of all team members. """ - return [member[0] for member in self.members] + return [member.first_name for member in self.members] def get_emails(self) -> list[str]: """ Get a list of the emails of all team members. """ - return [member[2] for member in self.members] + return [member.email for member in self.members] def get_team_key(self) -> str: """ @@ -31,14 +36,16 @@ def last_names_to_string(self) -> str: representation of a team. """ return "_".join( - sorted([member[1].replace(" ", "-") for member in self.members]) + sorted( + [member.last_name.replace(" ", "-") for member in self.members] + ) ) def to_tuples(self) -> list[tuple[str, str, str]]: """ Get a tuples of strings representation of a team. """ - return [(member[0], member[1], member[2]) for member in self.members] + return [member.to_tuple() for member in self.members] def create_email_to_name_dict(teams: list[Team]) -> dict[str, tuple[str, str]]: @@ -47,6 +54,6 @@ def create_email_to_name_dict(teams: list[Team]) -> dict[str, tuple[str, str]]: """ email_to_name = {} for team in teams: - for first_name, last_name, email in team.members: - email_to_name[email] = (first_name, last_name) + for member in team.members: + email_to_name[member.email] = (member.first_name, member.last_name) return email_to_name