Coverage for sel_tools/file_parsing/student_group_parser.py: 97%
30 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-04 21:22 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-04 21:22 +0000
1"""Parse group formation for adding students to Gitlab repositories."""
3from dataclasses import dataclass
4from pathlib import Path
6import pandas as pd
7from gitlab.v4.objects import User
9INVALID_GROUP_CHOICES = ["Not answered yet", "Choice"]
12@dataclass
13class Student:
14 """Student information."""
16 first_name: str
17 last_name: str
18 mail_addr: str
19 choice: str
20 gitlab_user: User = None
22 @property
23 def name(self) -> str:
24 return f"{self.first_name} {self.last_name}"
26 @property
27 def valid_choice(self) -> bool:
28 return self.choice not in INVALID_GROUP_CHOICES
30 @property
31 def group_id(self) -> int:
32 if self.valid_choice:
33 return int(self.choice.split(" ")[1])
34 return -1
36 @staticmethod
37 def from_dict(student_dict: dict) -> "Student":
38 return Student(
39 student_dict["First name"],
40 student_dict["Last name"],
41 student_dict["Email address"],
42 student_dict["Choice"],
43 )
46def get_student_groups_from_file(group_formation_file: Path) -> list[Student]:
47 """Parse CSV file for group formation."""
48 data = pd.read_csv(group_formation_file, delimiter=",|;", engine="python")
49 students_arr = [Student.from_dict(row) for _, row in data.iterrows()]
51 return [student for student in students_arr if student.valid_choice]