-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerateData.py
More file actions
288 lines (247 loc) · 10.2 KB
/
generateData.py
File metadata and controls
288 lines (247 loc) · 10.2 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# Name: generateData.py
# Purpose: Make the data work...
#
# Authors: Michael Scott Asato Cuthbert
#
# Copyright: Copyright © 2016-23 Michael Scott Asato Cuthbert and cuthbertLab
# License: MIT, see LICENSE file
#-------------------------------------------------------------------------------
'''
generates the HTML Files
'''
import locale
locale.setlocale(locale.LC_ALL, 'en_US')
import collegeCosts as cc
states = {
'AK': 'Alas[ka]',
'AL': 'Ala[bama]',
'AR': '[Ark]ansas',
'AS': 'American[Samoa]',
'AZ': 'Ari[zona]',
'CA': '[Cali]fornia',
'CO': 'Col[o]rado',
'CT': '[Conn]ecticut',
'DC': 'Washington[DC]',
'DE': '[Del]aware',
'FL': '[Flor]ida',
'GA': 'Ge[or]gia',
'GU': 'Guam',
'HI': 'Ha[wai]i',
'IA': 'I[o]wa',
'ID': '[Ida]ho',
'IL': 'Illi[nois]',
'IN': 'Ind[iana]',
'KS': '[Kan]sas',
'KY': 'Ken[tucky]',
'LA': '[Lou]isiana',
'MA': '[Mass]achusetts',
'MD': 'Mary[land]',
'ME': 'Maine',
'MI': 'Michi[gan]',
'MN': 'Minne[sota]',
'MO': 'Mis[souri]',
'MP': 'N.Mariana[Islands]',
'MS': '[Miss]issippi',
'MT': 'Mon[tana]',
'NC': 'North[Carolina]',
'ND': 'North[Dakota]',
'NE': 'Nebraska',
'NH': 'New[Hampshire]',
'NJ': 'New[Jersey]',
'NM': 'New[Mexico]',
'NV': 'Neva[da]',
'NY': 'New[York]',
'OH': 'O[hi]o',
'OK': '[Ok]lahoma',
'OR': '[Ore]gon',
'PA': '[Penn]sylvania',
'PR': 'Puerto[Rico]',
'RI': 'Rhode[Island]',
'SC': 'South[Carolina]',
'SD': 'South[Dakota]',
'TN': '[Tenn]essee',
'TX': '[Tex]as',
'UT': '[U]tah',
'VA': '[Vir]ginia',
'VI': 'Virgin[Islands]',
'VT': 'Ver[mont]',
'WA': 'Washing[ton]',
'WI': '[Wisc]onsin',
'WV': 'West[Virginia]',
'WY': '[Wyo]ming',
None: 'College[Costs]'
}
stateNames = {}
for abbrev in states:
newState = states[abbrev].replace('[', '<span class="accent">').replace(']', '</span>')
stateNames[abbrev] = newState
del(states)
satRanges = [(400, 800, "SAT 25th percentile below 800"),
(800, 900, "SAT 25th percentile 800 to 899"),
(900, 1000, "SAT 25th percentile 900 to 999"),
(1000, 1100, "SAT 25th percentile 1000 to 1099"),
(1100, 1200, "SAT 25th percentile 1100 to 1199"),
(1200, 1350, "SAT 25th percentile 1200 to 1349"),
(1350, 1601, "SAT 25th percentile above 1350"),
(None, None, "Without SAT Data"),
]
actRanges = [(2, 18, "ACT 25th percentile: 17 or below"),
(18, 20, "ACT 25th percentile: 18 or 19"),
(20, 22, "ACT 25th percentile: 20 or 21"),
(22, 24, "ACT 25th percentile: 22 or 23"),
(24, 27, "ACT 25th percentile: 24, 25, or 26"),
(27, 31, "ACT 25th percentile: 27 to 30"),
(31, 37, "ACT 25th percentile: 31 or higher"),
(None, None, "Without ACT Data"),
]
class FileGenerator:
yearStr = '2022'
outdir = 'data/'
dataTemplateFile = 'dataTemplate.html'
markPub = '*Pub:'
def __init__(self):
self.r = cc.readFile()
self.template = "{dataGoesHere}"
self.quiet = False
self.cachedInfo = {}
historical_data = cc.readFile('college_data_2016.csv.gz')
historical_by_id = {sch.unitid: sch for sch in historical_data}
for sch in self.r:
if sch.unitid in historical_by_id:
old_data = historical_by_id[sch.unitid]
if sch.satvrmid is None and old_data.satvrmid is not None:
# get older SAT data for this school
# print(sch)
sch.historical_data = old_data
self.getTemplate()
def getTemplate(self):
with open(self.dataTemplateFile) as dtf:
self.template = ''.join(dtf.readlines())
def outFilePath(self, incomeLevel, stateAbbr, testType='SAT'):
stateAbbrStr = str(stateAbbr) # for "None"
if testType == 'SAT':
testType = ''
outfp = self.outdir + self.yearStr + "_" + stateAbbrStr + str(
incomeLevel) + testType + '.html'
return outfp
def oneLink(self, row, incomeLevel=5, testType='SAT'):
pub = ""
if row.isPublic:
pub = self.markPub + row.STABBR
shortName = row.shortName(30)
testScore = None
testIsEstimated = False
if testType == 'SAT':
testScore, testIsEstimated = row.sat25_data()
else:
testScore = row.act25
costStr = locale.format_string("%d", row.cost(incomeLevel), grouping=True)
scoreStr = ('~' if testIsEstimated else '') + str(testScore)
out = (" $%7s %5s %3d%% %7s" %
(costStr, scoreStr, int(row.gradRate*100), pub))
out = ('<a target=_new href="https://collegescorecard.ed.gov/school/?%d">%30s</a> %s' %
(row.unitid, shortName, out))
return out
def generateOneTestHeader(self, incomeLevel, testExplain):
'''
this used to be complex enough to warrant its own method...
'''
header = "<h2>" + testExplain + "</h2>\n"
return header
def generateOneTestRange(self, incomeLevel, testData, testType='SAT'):
testMin, testMax, testExplain = testData # unpack them tuples
header = self.generateOneTestHeader(incomeLevel, testExplain)
out = []
out.append(header)
out.append("<pre class='cost'>")
if testType == 'SAT':
out.append("<b> Cost SAT Grad </b>")
rOut = cc.filterRows(self.r,
satMin=testMin,
satMax=testMax,
costMax=None,
costLevel=incomeLevel,
)
elif testType == 'ACT':
out.append("<b> Cost ACT Grad </b>")
rOut = cc.filterACTRows(self.r,
actMin=testMin,
actMax=testMax,
costMax=None,
costLevel=incomeLevel,
)
for rr in rOut:
if rr.belowCostLevel(incomeLevel):
out.append(self.oneLink(rr, incomeLevel, testType))
out.append("</pre>")
moreExpensiveExists = False
moreExpensive = []
moreExpensive.append("<button class='btn btn-default btn-lg btn-showCost' id='" +
str(incomeLevel) + "_" + str(testMin) + "'>Show More Expensive</button>")
moreExpensive.append('<pre class="cost hiddenPre" id="pre' + str(incomeLevel) + "_" +
str(testMin) + '">')
for rr in rOut:
if not rr.belowCostLevel(incomeLevel) and rr.belowExtremeCostLevel(incomeLevel):
moreExpensive.append(self.oneLink(rr, incomeLevel, testType))
moreExpensiveExists = True
elif not rr.belowExtremeCostLevel(incomeLevel):
moreExpensive.append("<span class='danger'>" + self.oneLink(rr, incomeLevel, testType) + "</span>")
moreExpensiveExists = True
moreExpensive.append("</pre>")
if moreExpensiveExists:
out.extend(moreExpensive)
out.append(" <br> <br> <br>")
return out
def stateFilteredTestRange(self, incomeLevel, stateAbbr, testData, testType='SAT'):
cacheKey = (incomeLevel, testData, testType)
if cacheKey in self.cachedInfo:
allRows = self.cachedInfo[cacheKey][:]
else:
allRows = self.generateOneTestRange(incomeLevel, testData, testType)
self.cachedInfo[cacheKey] = allRows[:]
filteredRows = []
stateMark = ""
if stateAbbr is not None:
stateMark = self.markPub + stateAbbr
for r in allRows:
if stateAbbr is not None and self.markPub in r and stateMark not in r:
continue
if stateAbbr is not None and stateMark in r:
r = r.replace(stateMark, '(publ.)')
elif stateAbbr is None and self.markPub in r:
r = r.replace(self.markPub, ' *')
filteredRows.append(r)
return '\n'.join(filteredRows)
def generateOneFile(self, incomeLevel=1, stateAbbr='', testType='SAT'):
if self.quiet is not True:
print("Generating: ", stateAbbr, incomeLevel, testType)
outFilePath = self.outFilePath(incomeLevel, stateAbbr, testType)
allRanges = []
if testType == 'SAT':
testRanges = satRanges
else:
testRanges = actRanges
for testData in testRanges:
oneRangeStr = self.stateFilteredTestRange(incomeLevel, stateAbbr, testData, testType)
allRanges.append(oneRangeStr)
allAllStr = '\n'.join(allRanges)
abbrevNice = stateAbbr
if abbrevNice is None:
abbrevNice = 'All US'
writeOut = self.template.format(dataGoesHere=allAllStr,
stateAbbr=abbrevNice,
stateName=stateNames[stateAbbr],
incomeLevel=cc.incomeLevels[incomeLevel])
with open(outFilePath, 'w', encoding='utf-8') as ofp:
ofp.write(writeOut)
def generateAll(self):
for testType in ('ACT', 'SAT'):
for incomeLevel in range(1, 6):
for stateAbbr in cc.stateList:
self.generateOneFile(incomeLevel, stateAbbr, testType)
if __name__ == '__main__':
fg = FileGenerator()
fg.generateAll()
#fg.generateOneFile(2, None)