Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- # read data from the provided list of files and add it to a dict
- # dict key: student id (opnro)
- # dict values in a nested dict:
- # name [firstname, surname] as a list)
- # assignments [amount of completed assignments] as a list)
- # exams [points from exams] as a list)
- def read_data_from_files(filelist: list):
- student_data = {}
- for file in filelist:
- with open(file) as tiedosto:
- for rivi in tiedosto:
- osat = rivi.split(';')
- if osat[0] == "opnro":
- continue
- if "opiskelijat" in file:
- # check if student opnro exists already, add values
- if osat[0] in student_data.keys():
- student_data[osat[0]]["name"] = [osat[1], osat[2].strip()]
- # if student key doesn't exist, add it & values
- else:
- student_data[osat[0]] = {"name": [osat[1], osat[2].strip()]}
- # add assignment key and values to student data
- if "tehtavat" in file:
- # strips line changes etc. from list
- stripped = [int(s.strip()) for s in osat]
- student_data[osat[0]]["assignments"] = stripped[1:]
- # add exam key and values to student data
- if "koepisteet" in file:
- stripped = [int(s.strip()) for s in osat]
- student_data[osat[0]]["exams"] = stripped[1:]
- # add course key, and course name and ECTS as a list
- if "kurssi" in file:
- #print(osat)
- if "course" in student_data.keys():
- student_data["course"].append(osat[0].strip())
- else:
- student_data["course"] = [osat[0].strip()]
- return student_data
- def save_data_to_file(filename, data):
- with open(filename, "w") as file:
- for row in data:
- file.write(f"{row}\n")
- def print_student_data(student_data: dict, format: str):
- # init lists with column names
- student_ids = ["student_id"]
- names = ["nimi"]
- ass_pcs = ["teht_lkm"]
- ass_pts = ["teht_pist"]
- exa_pts = ["koe_pist"]
- total_pts = ["yht_pist"]
- grade = ["arvosana"]
- # populate lists with student values
- for student_id, val in student_data.items():
- if student_id != "course":
- cur_ass_pts = math.floor(sum(val["assignments"])/40*10)
- cur_exa_pts = sum(val["exams"])
- student_ids.append(student_id)
- names.append(val["name"][0] + " " + val["name"][1])
- ass_pcs.append(sum(val["assignments"]))
- ass_pts.append(cur_ass_pts)
- exa_pts.append(cur_exa_pts)
- total_pts.append(cur_ass_pts + cur_exa_pts)
- grade.append(check_grade(cur_ass_pts + cur_exa_pts))
- # print data from the lists
- if format == "screen":
- for i in range(0,len(names)):
- print(f"{names[i]:30}{ass_pcs[i]:<10}{ass_pts[i]:<10}{exa_pts[i]:<10}{total_pts[i]:<10}{grade[i]:<10}")
- if format == "txt":
- data = []
- course_name = val[0].replace("nimi: ", "")
- course_ects = val[1].replace("laajuus opintopisteina: ", "") + " opintopistettä"
- data.append(f"{course_name}, {course_ects}")
- data.append("="*len(course_name+course_ects)+"==")
- for i in range(0,len(names)):
- data.append(f"{names[i]:30}{ass_pcs[i]:<10}{ass_pts[i]:<10}{exa_pts[i]:<10}{total_pts[i]:<10}{grade[i]:<10}")
- save_data_to_file("tulos.txt", data)
- if format == "csv":
- data = []
- for i in range(0,len(names)):
- if i > 0:
- data.append(f"{student_ids[i]};{names[i]};{grade[i]}")
- save_data_to_file("tulos.csv", data)
- def check_grade(points: int):
- # 0 = fail, rest are grades 1-5
- grade_limits = [0, 15, 18, 21, 24, 28]
- for grade in range(5, -1, -1):
- if points >= grade_limits[grade]:
- return grade
- def input_from_user(questions: list):
- answers = []
- for question in questions:
- answers.append(input(question))
- return answers
- def main():
- questions = ["opiskelijatiedot:", "tehtävätiedot:", "koepisteet:", "kurssin tiedot:"]
- if True:
- filelist = input_from_user(questions)
- else:
- filelist = ["opiskelijat1.csv", "tehtavat1.csv", "koepisteet1.csv", "kurssi4.txt"]
- student_data = read_data_from_files(filelist)
- #print_student_data(student_data, "screen")
- print_student_data(student_data, "txt")
- print_student_data(student_data, "csv")
- #print(answers)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement