Advertisement
Ihmemies

6-14 MOOC 2022

Jun 23rd, 2022
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.79 KB | None | 0 0
  1. import math
  2.  
  3. # read data from the provided list of files and add it to a dict
  4. # dict key: student id (opnro)
  5. # dict values in a nested dict:
  6. #       name [firstname, surname] as a list)
  7. #       assignments [amount of completed assignments] as a list)
  8. #       exams [points from exams] as a list)
  9.  
  10. def read_data_from_files(filelist: list):
  11.     student_data = {}
  12.     for file in filelist:        
  13.         with open(file) as tiedosto:
  14.             for rivi in tiedosto:
  15.                 osat = rivi.split(';')
  16.  
  17.                 if osat[0] == "opnro":
  18.                     continue
  19.  
  20.                 if "opiskelijat" in file:
  21.                     # check if student opnro exists already, add values
  22.                     if osat[0] in student_data.keys():
  23.                         student_data[osat[0]]["name"] = [osat[1], osat[2].strip()]
  24.                     # if student key doesn't exist, add it & values
  25.                     else:
  26.                         student_data[osat[0]] = {"name": [osat[1], osat[2].strip()]}
  27.  
  28.                 # add assignment key and values to student data
  29.                 if "tehtavat" in file:
  30.                     # strips line changes etc. from list
  31.                     stripped = [int(s.strip()) for s in osat]
  32.                     student_data[osat[0]]["assignments"] = stripped[1:]
  33.  
  34.                 # add exam key and values to student data
  35.                 if "koepisteet" in file:
  36.                     stripped = [int(s.strip()) for s in osat]
  37.                     student_data[osat[0]]["exams"] = stripped[1:]
  38.  
  39.                 # add course key, and course name and ECTS as a list
  40.                 if "kurssi" in file:  
  41.                     #print(osat)
  42.                     if "course" in student_data.keys():
  43.                         student_data["course"].append(osat[0].strip())
  44.                     else:
  45.                         student_data["course"] = [osat[0].strip()]
  46.  
  47.     return student_data
  48.  
  49. def save_data_to_file(filename, data):    
  50.     with open(filename, "w") as file:
  51.         for row in data:
  52.             file.write(f"{row}\n")
  53.  
  54. def print_student_data(student_data: dict, format: str):
  55.     # init lists with column names
  56.     student_ids = ["student_id"]
  57.     names = ["nimi"]
  58.     ass_pcs = ["teht_lkm"]
  59.     ass_pts = ["teht_pist"]
  60.     exa_pts = ["koe_pist"]
  61.     total_pts = ["yht_pist"]
  62.     grade = ["arvosana"]
  63.  
  64.     # populate lists with student values
  65.     for student_id, val in student_data.items():        
  66.         if student_id != "course":
  67.             cur_ass_pts = math.floor(sum(val["assignments"])/40*10)
  68.             cur_exa_pts = sum(val["exams"])
  69.  
  70.             student_ids.append(student_id)
  71.             names.append(val["name"][0] + " " + val["name"][1])
  72.             ass_pcs.append(sum(val["assignments"]))
  73.             ass_pts.append(cur_ass_pts)
  74.             exa_pts.append(cur_exa_pts)
  75.             total_pts.append(cur_ass_pts + cur_exa_pts)
  76.             grade.append(check_grade(cur_ass_pts + cur_exa_pts))
  77.  
  78.     # print data from the lists
  79.     if format == "screen":        
  80.         for i in range(0,len(names)):            
  81.             print(f"{names[i]:30}{ass_pcs[i]:<10}{ass_pts[i]:<10}{exa_pts[i]:<10}{total_pts[i]:<10}{grade[i]:<10}")
  82.  
  83.     if format == "txt":
  84.         data = []
  85.         course_name = val[0].replace("nimi: ", "")
  86.         course_ects = val[1].replace("laajuus opintopisteina: ", "") + " opintopistettä"
  87.         data.append(f"{course_name}, {course_ects}")
  88.         data.append("="*len(course_name+course_ects)+"==")
  89.  
  90.         for i in range(0,len(names)):    
  91.             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}")
  92.        
  93.         save_data_to_file("tulos.txt", data)
  94.  
  95.     if format == "csv":
  96.         data = []
  97.         for i in range(0,len(names)):
  98.             if i > 0:    
  99.                 data.append(f"{student_ids[i]};{names[i]};{grade[i]}")
  100.        
  101.         save_data_to_file("tulos.csv", data)
  102.  
  103. def check_grade(points: int):
  104.     # 0 = fail, rest are grades 1-5
  105.     grade_limits = [0, 15, 18, 21, 24, 28]
  106.  
  107.     for grade in range(5, -1, -1):
  108.         if points >= grade_limits[grade]:
  109.             return grade
  110.  
  111. def input_from_user(questions: list):
  112.     answers = []
  113.     for question in questions:
  114.         answers.append(input(question))
  115.     return answers
  116.  
  117. def main():
  118.     questions = ["opiskelijatiedot:", "tehtävätiedot:", "koepisteet:", "kurssin tiedot:"]
  119.     if True:
  120.         filelist = input_from_user(questions)
  121.     else:
  122.         filelist = ["opiskelijat1.csv", "tehtavat1.csv", "koepisteet1.csv", "kurssi4.txt"]
  123.     student_data = read_data_from_files(filelist)
  124.     #print_student_data(student_data, "screen")
  125.     print_student_data(student_data, "txt")
  126.     print_student_data(student_data, "csv")
  127.     #print(answers)
  128.  
  129. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement