add student dataclass
This commit is contained in:
6 files changed
+124
-98
No files matched your search
@@ -44,7 +44,7 @@ def putScheduleToWord(
|
||||
# create an instance of a word doc.
|
||||
doc = docx.Document()
|
||||
|
||||
doc.add_heading(f'Schedule for Student {student["Pupil #"]}')
|
||||
doc.add_heading(f'Schedule for Student {student.PupilNum}')
|
||||
|
||||
# Create a table object
|
||||
table = doc.add_table(rows=1, cols=3)
|
||||
@@ -62,12 +62,12 @@ def putScheduleToWord(
|
||||
table.columns[2].width = Inches(0.75)
|
||||
|
||||
for block in student['schedule']:
|
||||
courseCode = student["schedule"][block][0]
|
||||
courseCode = student.Schedule[block][0]
|
||||
if courseCode in flex: courseName = 'Study'
|
||||
else: courseName = courses[courseCode[:-2]]["Description"]
|
||||
blockNum = int(block.split('block')[1])
|
||||
semester = 2 if blockNum > 5 else 1
|
||||
if blockNum > (len(student["schedule"])/2): blockNum -= (len(student["schedule"])/2)
|
||||
if blockNum > (len(student.Schedule)/2): blockNum -= (len(student.Schedule)/2)
|
||||
row = table.add_row().cells
|
||||
row[0].text = f'{courseName}\n({courseCode})'
|
||||
row[1].text = str(blockNum)
|
||||
@@ -76,4 +76,4 @@ def putScheduleToWord(
|
||||
for row in table.rows:
|
||||
row.height = Inches(0.75)
|
||||
|
||||
doc.save(f'{output_dir}/{student["Pupil #"]}_schedule.docx')
|
||||
doc.save(f'{output_dir}/{student.PupilNum}_schedule.docx')
|
||||
@@ -1,8 +1,15 @@
|
||||
#!/usr/bin/env python3.11
|
||||
import json
|
||||
import csv
|
||||
from dataclasses import dataclass
|
||||
from app.util.estimateGrade import getGrade
|
||||
|
||||
@dataclass
|
||||
class Request:
|
||||
CrsNo: str
|
||||
Description: str
|
||||
Alt: bool
|
||||
|
||||
# Get all requested courses from data
|
||||
def getCourses(
|
||||
data_dir: str,
|
||||
|
||||
@@ -23,10 +23,10 @@ def extractGrade(string: str) -> int:
|
||||
return grade if grade is not None and grade >= 8 else None
|
||||
|
||||
# Estimage students grade based off requests
|
||||
def estimateStudentGrade(pupil: dict) -> int:
|
||||
def estimateStudentGrade(pupil) -> int:
|
||||
grades = [] # List of all possible grades
|
||||
for request in (r for r in pupil["requests"] if r not in flex):
|
||||
extractedGrade = getGrade(request["CrsNo"], request["Description"])
|
||||
for request in (r for r in pupil.Requests if r not in flex):
|
||||
extractedGrade = getGrade(request.CrsNo, request.Description)
|
||||
if extractedGrade is not None: grades.append(extractedGrade)
|
||||
|
||||
return None if len(grades) == 0 else most_frequent(grades) # Final estimate of grade
|
||||
|
||||
+48
-30
@@ -3,6 +3,26 @@ import json
|
||||
import csv
|
||||
from app.util.estimateGrade import estimateStudentGrade
|
||||
from app.util.globals import flex
|
||||
from app.util.courses import Request
|
||||
from dataclasses import dataclass, asdict
|
||||
|
||||
@dataclass
|
||||
class Student:
|
||||
PupilNum: str
|
||||
Requests: list[Request]
|
||||
Schedule: dict[str: list]
|
||||
ExpectedClasses: int
|
||||
Classes: int
|
||||
RemainingAlts: list[Request]
|
||||
StudentIndex: int
|
||||
Gradelevel: int = None # set to None since don't pass gradelevel
|
||||
|
||||
# This function converst Student.Requests to an array of dictionaries from an array of Requests
|
||||
def RequestsToDict(self) -> None: self.Requests = [asdict(r) for r in self.Requests]
|
||||
|
||||
def studentsToDict(students: list[Student]) -> list[dict]:
|
||||
[s.RequestsToDict for s in students]
|
||||
return [asdict(s) for s in students]
|
||||
|
||||
# sort data into usable dictionary
|
||||
def getStudents(
|
||||
@@ -12,56 +32,54 @@ def getStudents(
|
||||
log_dir: str = './output/raw/students.json'
|
||||
) -> list[ dict[ str: any ] ]:
|
||||
|
||||
students: list[ dict[ str: any ] ] = []
|
||||
students: list[Student] = []
|
||||
|
||||
with open(data_dir, newline='') as csvfile:
|
||||
reader = csv.DictReader(csvfile)
|
||||
for row in reader:
|
||||
exists = False
|
||||
for student in students:
|
||||
exists = True if student["Pupil #"] == row["Pupil #"] else False
|
||||
exists = True if student.PupilNum == row["Pupil #"] else False
|
||||
if exists: break
|
||||
alternate = True if row["Alternate?"] == 'TRUE' else False
|
||||
alternate = True if str(row["Alternate?"]).upper() == 'TRUE' else False
|
||||
if exists:
|
||||
if len(students[student["studentIndex"]]["requests"]) >= totalBlocks and not alternate and row["CrsNo"] not in flex: alternate = True
|
||||
students[student["studentIndex"]]["requests"].append({
|
||||
"CrsNo": row["CrsNo"],
|
||||
"Description": row["Description"],
|
||||
"alt": alternate
|
||||
})
|
||||
if row["CrsNo"] not in flex and not alternate and students[student["studentIndex"]]["expectedClasses"] < 10:
|
||||
students[student["studentIndex"]]["expectedClasses"] += 1
|
||||
if len(students[student.StudentIndex].Requests) >= totalBlocks and not alternate and row["CrsNo"] not in flex: alternate = True
|
||||
students[student.StudentIndex].Requests.append(
|
||||
Request(
|
||||
row["CrsNo"],
|
||||
row["Description"],
|
||||
alternate
|
||||
))
|
||||
if row["CrsNo"] not in flex and not alternate and students[student.StudentIndex].ExpectedClasses < 10:
|
||||
students[student.StudentIndex].ExpectedClasses += 1
|
||||
else:
|
||||
newStudent = {
|
||||
"Pupil #": row["Pupil #"],
|
||||
"requests": [{
|
||||
"CrsNo": row["CrsNo"],
|
||||
"Description": row["Description"],
|
||||
"alt": alternate
|
||||
}],
|
||||
"schedule": {},
|
||||
"expectedClasses": 1,
|
||||
"classes": 0,
|
||||
"remainingAlts": [],
|
||||
"studentIndex": len(students)
|
||||
}
|
||||
newStudent["gradelevel"] = row.get("Grade") if row.get("Grade") is not None else row.get("grade")
|
||||
for i in range(1, totalBlocks+1): newStudent["schedule"][f'block{i}'] = []
|
||||
newStudent: Student = Student(
|
||||
row["Pupil #"],
|
||||
[Request(
|
||||
row["CrsNo"],
|
||||
row["Description"],
|
||||
alternate
|
||||
)],
|
||||
{}, 1, 0, [],
|
||||
len(students)
|
||||
)
|
||||
newStudent.Gradelevel = row.get("Grade") if row.get("Grade") is not None else row.get("grade")
|
||||
for i in range(1, totalBlocks+1): newStudent.Schedule[f'block{i}'] = []
|
||||
students.append(newStudent)
|
||||
|
||||
# Estimate student grades
|
||||
for student in students:
|
||||
if student["gradelevel"] is None: student["gradelevel"] = estimateStudentGrade(student)
|
||||
if student.Gradelevel is None: student.Gradelevel = estimateStudentGrade(student)
|
||||
|
||||
if log:
|
||||
with open(log_dir, "w") as outfile:
|
||||
json.dump(students, outfile, indent=2)
|
||||
json.dump(studentsToDict(students), outfile, indent=2)
|
||||
|
||||
return students
|
||||
|
||||
# Writes all students data to csv file
|
||||
def writeStudentsToCSV(
|
||||
students: dict,
|
||||
students: list[Student],
|
||||
output_dir: str = './output/raw/csv/students.csv'
|
||||
) -> None:
|
||||
with open(output_dir, 'w') as file:
|
||||
@@ -71,5 +89,5 @@ def writeStudentsToCSV(
|
||||
writer.writerow(data)
|
||||
|
||||
for student in students:
|
||||
studentData = (student["Pupil #"], student["classes"], student["gradelevel"])
|
||||
studentData = (student.PupilNum, student.Classes, student.Gradelevel)
|
||||
writer.writerow(studentData)
|
||||
Reference in new issue
Block a user