log data to csv
This commit is contained in:
5 files changed
+114
-57
No files matched your search
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env python3.11
|
||||
import json
|
||||
import csv
|
||||
from app.util.estimateGrade import getGradeFromCourseCode
|
||||
|
||||
# Get all requested courses from data
|
||||
def getCourses(
|
||||
data_dir: str,
|
||||
log: int=False,
|
||||
log_dir: str='./output/raw/courses.json',
|
||||
) -> dict[dict[str: any]]:
|
||||
|
||||
courses = {}
|
||||
|
||||
with open(data_dir, newline='') as csvfile:
|
||||
reader = csv.DictReader(csvfile)
|
||||
for row in reader:
|
||||
exists = False
|
||||
for course in courses:
|
||||
exists = True if courses[course]["CrsNo"] == row["CrsNo"] else False
|
||||
if exists: break
|
||||
if not exists:
|
||||
courses[row["CrsNo"]] = {
|
||||
"CrsNo": row["CrsNo"],
|
||||
"Requests": 0,
|
||||
"Description": row["Description"],
|
||||
"Sem1": 0,
|
||||
"Sem2":0,
|
||||
"Grade": getGradeFromCourseCode(row["CrsNo"])
|
||||
}
|
||||
|
||||
if log:
|
||||
with open(log_dir, "w") as outfile:
|
||||
json.dump(courses, outfile, indent=2)
|
||||
|
||||
return courses
|
||||
|
||||
# Writes all course data to csv file
|
||||
def writeCoursesToCSV(courses: dict, output_dir: str='./output/raw/csv/courses.csv') -> None:
|
||||
with open(output_dir, 'w') as file:
|
||||
writer = csv.writer(file)
|
||||
# Write header
|
||||
data = ("CrsNo", "Description", "Grade", "Requests", "First sem. # of Classes", "Second sem. # of Classes", "Total # of Classes")
|
||||
writer.writerow(data)
|
||||
|
||||
for course in courses:
|
||||
courseData = (
|
||||
courses[course]["CrsNo"],
|
||||
courses[course]["Description"],
|
||||
courses[course]["Grade"],
|
||||
courses[course]["Requests"],
|
||||
courses[course]["Sem1"],
|
||||
courses[course]["Sem2"],
|
||||
(courses[course]["Sem1"] + courses[course]["Sem2"])
|
||||
)
|
||||
writer.writerow(courseData)
|
||||
@@ -1,32 +0,0 @@
|
||||
#!/usr/bin/env python3.11
|
||||
import json
|
||||
import csv
|
||||
|
||||
# Get all requested courses from data
|
||||
def getCourses(
|
||||
data_dir: str,
|
||||
log: int=False,
|
||||
log_dir: str='./output/raw/courses.json',
|
||||
) -> dict[dict[str: any]]:
|
||||
|
||||
courses = {}
|
||||
|
||||
with open(data_dir, newline='') as csvfile:
|
||||
reader = csv.DictReader(csvfile)
|
||||
for row in reader:
|
||||
exists = False
|
||||
for course in courses:
|
||||
exists = True if courses[course]["CrsNo"] == row["CrsNo"] else False
|
||||
if exists: break
|
||||
if not exists:
|
||||
courses[row["CrsNo"]] = {
|
||||
"CrsNo": row["CrsNo"],
|
||||
"Requests": 0,
|
||||
"Description": row["Description"]
|
||||
}
|
||||
|
||||
if log:
|
||||
with open(log_dir, "w") as outfile:
|
||||
json.dump(courses, outfile, indent=2)
|
||||
|
||||
return courses
|
||||
@@ -57,3 +57,15 @@ def getStudents(
|
||||
json.dump(students, outfile, indent=2)
|
||||
|
||||
return students
|
||||
|
||||
# Writes all students data to csv file
|
||||
def writeStudentsToCSV(students: dict, output_dir: str='./output/raw/csv/students.csv') -> None:
|
||||
with open(output_dir, 'w') as file:
|
||||
writer = csv.writer(file)
|
||||
# Write header
|
||||
data = ("Pupil #", "# of Classes", "Grade")
|
||||
writer.writerow(data)
|
||||
|
||||
for student in students:
|
||||
studentData = (student["Pupil #"], student["classes"], student["gradelevel"])
|
||||
writer.writerow(studentData)
|
||||
Reference in new issue
Block a user