forked from UTDNebula/api-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradeLoader.go
147 lines (124 loc) · 3.54 KB
/
gradeLoader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package parser
import (
"encoding/csv"
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"strings"
)
func loadGrades(csvDir string) map[string]map[string][]int {
// MAP[SEMESTER] -> MAP[SUBJECT + NUMBER + SECTION] -> GRADE DISTRIBUTION
gradeMap := make(map[string]map[string][]int)
if csvDir == "" {
log.Print("No grade data CSV directory specified. Grade data will not be included.")
return gradeMap
}
dirPtr, err := os.Open(csvDir)
if err != nil {
panic(err)
}
defer dirPtr.Close()
csvFiles, err := dirPtr.ReadDir(-1)
if err != nil {
panic(err)
}
for _, csvEntry := range csvFiles {
if csvEntry.IsDir() {
continue
}
csvPath := fmt.Sprintf("%s/%s", csvDir, csvEntry.Name())
csvFile, err := os.Open(csvPath)
if err != nil {
panic(err)
}
defer csvFile.Close()
// Create logs directory
if _, err := os.Stat("./logs/grades"); err != nil {
os.Mkdir("./logs/grades", os.ModePerm)
}
// Create log file [name of csv].log in logs directory
basePath := filepath.Base(csvPath)
csvName := strings.TrimSuffix(basePath, filepath.Ext(basePath))
logFile, err := os.Create("./logs/grades/" + csvName + ".log")
if err != nil {
log.Panic("Could not create CSV log file.")
}
defer logFile.Close()
// Put data from csv into map
gradeMap[csvName] = csvToMap(csvFile, logFile)
}
return gradeMap
}
func csvToMap(csvFile *os.File, logFile *os.File) map[string][]int {
reader := csv.NewReader(csvFile)
records, err := reader.ReadAll() // records is [][]strings
if err != nil {
log.Panicf("Error parsing %s: %s", csvFile.Name(), err.Error())
}
// look for the subject column and w column
subjectCol := -1
catalogNumberCol := -1
sectionCol := -1
wCol := -1
aPlusCol := -1
headerRow := records[0]
for j := 0; j < len(headerRow); j++ {
switch {
case headerRow[j] == "Subject":
subjectCol = j
case headerRow[j] == "Catalog Number" || headerRow[j] == "Catalog Nbr":
catalogNumberCol = j
case headerRow[j] == "Section":
sectionCol = j
case headerRow[j] == "W" || headerRow[j] == "Total W" || headerRow[j] == "W Total":
wCol = j
case headerRow[j] == "A+":
aPlusCol = j
}
if wCol == -1 || subjectCol == -1 || catalogNumberCol == -1 || sectionCol == -1 || aPlusCol == -1 {
continue
} else {
break
}
}
if wCol == -1 {
logFile.WriteString("could not find W column")
//log.Panicf("could not find W column")
}
if sectionCol == -1 {
logFile.WriteString("could not find Section column")
log.Panicf("could not find Section column")
}
if subjectCol == -1 {
logFile.WriteString("could not find Subject column")
log.Panicf("could not find Subject column")
}
if catalogNumberCol == -1 {
logFile.WriteString("could not find catalog # column")
log.Panicf("could not find catalog # column")
}
if aPlusCol == -1 {
logFile.WriteString("could not find A+ column")
log.Panicf("could not find A+ column")
}
distroMap := make(map[string][]int)
for _, record := range records {
// convert grade distribution from string to int
intSlice := [14]int{}
for j := 0; j < 13; j++ {
intSlice[j], _ = strconv.Atoi(record[aPlusCol+j])
}
// add w number to the grade_distribution slice
if wCol != -1 {
intSlice[13], _ = strconv.Atoi(record[wCol])
}
// add new grade distribution to map, keyed by SUBJECT + NUMBER + SECTION
// Be sure to trim left padding on section number
trimmedSectionNumber := strings.TrimLeft(record[sectionCol], "0")
distroKey := record[subjectCol] + record[catalogNumberCol] + trimmedSectionNumber
distroMap[distroKey] = intSlice[:]
}
return distroMap
}