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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# -*- coding: utf-8 -*-
"""
@author Alexander Corey
@file   scores.py
@date   10/05/2020
@brief  This program reads a specifically formatted csv file
        for an instructor's student grades and creates a web page
        with the scores.
"""



import csv

def main():
    # list definitions
    rows               = []
    title              = []              
    assignments        = []
    tests              = []
    assignments_weight = []
    tests_weight       = []
    columns            = []
    possible_scores    = []
    students           = []
    class_avg          = []
    
    # variables
    counter    = 0
    r          = 7
    counter2   = 0
    x          = 0
    y          = 2
    assign_sum = 0.0
    test_sum   = 0.0
    assign_avg = 0.0
    test_avg   = 0.0
    total_avg  = 0.0
    
    # INPUT
    # with keyword closes file on its own, even if logic abruptly stopped
    with open('scores1.csv') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
    # with csv module, each row returned as a list of strings
        for row in csv_reader:
            rows.append(row)
    
    title.append(rows[0][1])
    
    assignments.append(rows[1][1])
    assignments.append(rows[1][2])
    
    tests.append(rows[2][1])
    tests.append(rows[2][2])
    
    assignments_weight.append(rows[3][1])
    assignments_weight.append(rows[3][2])
    
    tests_weight.append(rows[4][1])
    tests_weight.append(rows[4][2])
    
    while counter < len(rows[5]):
        columns.append(rows[5][counter])
        possible_scores.append(rows[6][counter])
        
        counter  += 1
        
    columns.append("Avg")
    # populating students row
    while r < 36:
        students.append(rows[r])
        r += 1

    while x < 29:  
        while y < 12:
            if len(students[x][y]) == 0:
                students[x][y] = int(students[x][y].replace('','0'))
            y += 1    
        y = 2
        x += 1
            
    x = 0  # reset vars
    y = 2  # reset vars
    
    while x < 29:  
        while y < 12:
            if students[x][y] != 0:
                students[x][y] = int(students[x][y])
            y += 1    
        y = 2
        x += 1
             
    x = 0  # reset vars
    y = 2  # reset vars
    
    # avg logic for student avg column
    for student in students:
        while y < 12:
            assign_sum += students[x][y]
            y += 1
        y = 2  # reset counter for items in row
        assign_avg = assign_sum / 8  
        
        test_sum   += (int(students[x][10]) + int(students[x][11]))
        test_avg   = test_sum / 2
        total_avg  = (40 * assign_avg + 60 * test_avg) / 100.0           
        students[x].append(total_avg)
        assign_sum = 0 # reset var for next student row
        test_sum   = 0 # reset var for next student row
        x += 1  # inc counter for new student row

    # OUTPUT
    with open('scores.html', 'w', newline='') as output:
        output.write("""<html><head><style>                 
table {{                                               
  font-family: Helvetica, sans-serif;                  
  border-collapse: collapse;
  width: 100%;
}}

td, th {{
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}}

tr:nth-child(even) {{
  background-color: #dddddd;
}}
</style><h1> {0} </h1></head><body>""".format(title[0]))
        output.write("<table>")
        
        #Populate columns
        output.write("<tr>") #opening tag for column row
        for column in columns:
            output.write("<th>")
            output.write(columns[counter2])
            counter2 += 1
            output.write("</th>")
        output.write("</tr>") #end of columns html
        
        counter  = 0          # reset counter var for reuse
        counter2 = 0          # reset counter var for reuse
        
        
        #while counter < len(students):
        for student in students:
            output.write("<tr>")  #create student row
            while counter2 < 13:
                output.write("<td>") #create table cell
                output.write(str(students[counter][counter2]))
                counter2 += 1
                output.write("</td>")
            counter2 = 0  # reset counter for items in row
            counter += 1  # inc counter for new student row
            output.write("</tr>") #<tr>
            
        
        counter = 0 #recycle counter var
        #Possible scores row
        output.write("<tr>") #opening tag for column row
        for possible in possible_scores:
            output.write("<th>")
            output.write(possible_scores[counter])
            counter += 1
            output.write("</th>")
        output.write("</tr>") #end of columns html


        output.write("</table></body></html>") #close table    

if __name__ == "__main__": main()