diff --git a/stats.py b/stats.py index 60d276b..62bcf53 100644 --- a/stats.py +++ b/stats.py @@ -32,17 +32,27 @@ """ -import json, csv, requests +def num(s): + if s == '': return 0 + try: + return int(s) + except ValueError: + return float(s) + +import json, csv, requests, sys, re +from statistics import mean, median, stdev from pipelines import fetch, url from courses import getCoursesInTerm from collections import defaultdict +all_grades_file = f"cache/grades_all.csv" +all_courses_file = f"cache/course_grades_all.csv" def get_all(): terms = '178 177 176 175 174 173 172 171 168 65 64 62 63 61 60 25 26 23 22 21'.split(' ') sems = '202330 202310 202270 202250 202230 202210 202170 202150 202130 202070 202050 202030 202010 201970 201950 201930 201910 201870 201850 201830'.split(' ') # Save grades to a CSV file - with open(f"cache/grades_all.csv", "w", newline="") as csvfile: + with open(all_grades_file, "w", newline="") as csvfile: writer = csv.writer(csvfile) writer.writerow(["crn", "sem", "coursecode", "s_can_id","g","name", "current", "final"]) for (term,sem) in zip(terms,sems): @@ -74,9 +84,56 @@ def grades(writer, sem, COURSE_ID, course_code): print("Exception:", e) -get_all() -#grades() +def count_above_70(li): + pass +def process_one_course_grades(block, output): + fxns = [mean, median, stdev, min, max, len] + c_id = block[0][0] + sem = block[0][1] + course_code = block[0][2] + cur_scores = [num(x[6]) for x in block] + final_scores = [num(x[7]) for x in block] + #print(cur_scores) + #print(final_scores) + try: + (cur_mean, cur_median, cur_stdev, cur_min, cur_max, cur_count) = [round(f(cur_scores)) for f in fxns] + (final_mean, final_median, final_stdev, final_min, final_max, final_count) = [round(f(final_scores)) for f in fxns] + + print("Course mean median stdev min max count") + print("{:>12} {: 6.0f} {: 6.0f} {: 6.0f} {: 6.0f} {: 6.0f} {:6d} ".format(course_code, cur_mean, cur_median, cur_stdev, cur_min, cur_max, cur_count)) + print("{:>12} {: 6.0f} {: 6.0f} {: 6.0f} {: 6.0f} {: 6.0f} {:6d} ".format(course_code, final_mean, final_median, final_stdev, final_min, final_max, final_count)) + print() + output.writerow( [course_code, "current score", cur_mean, cur_median, cur_stdev, cur_min, cur_max, cur_count] ) + output.writerow( [course_code, "final score", final_mean, final_median, final_stdev, final_min, final_max, final_count] ) + except Exception as e: + print("Exception:", e) + +def process_grades(): + with open(all_courses_file, "w", newline="") as output_f: + output = csv.writer(output_f) + output.writerow("Course mean median stdev min max count".split(" ")) + + with open(all_grades_file, newline="") as csvfile: + csvreader = csv.reader(csvfile) + block = [] + current_index = None + + next(csvreader) + + for row in csvreader: + index = row[0] + + if index != current_index: + if block: + process_one_course_grades(block, output) + block = [] + current_index = index + + block.append(row) + + if block: + process_one_course_grades(block, output) @@ -297,4 +354,26 @@ def externaltool(): # a list t = url + '/api/v1/accounts/1/external_tools/' while(t): t = fetch(t) print(results) - \ No newline at end of file + + + +if __name__ == "__main__": + options = { 1: ['get all historical grades from ilearn',get_all] , + 2: ['process grades csv file',process_grades] , + } + print ('') + + if len(sys.argv) > 1 and re.search(r'^\d+',sys.argv[1]): + resp = int(sys.argv[1]) + print("\n\nPerforming: %s\n\n" % options[resp][0]) + + else: + print ('') + for key in options: + print(str(key) + '.\t' + options[key][0]) + + print('') + resp = input('Choose: ') + + # Call the function in the options dict + options[ int(resp)][1]()