-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanIn.py
161 lines (147 loc) · 5.05 KB
/
scanIn.py
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
import mysql.connector
from lumina import Lumina
REPLACEMENT = 0.0
STATIC_ATTRS = ["UNITID", "INSTNM", "CITY", "STABBR"]
USERNAME = 'usr'
PASSWORD = ''
def convert(x):
try:
return float(x)
except ValueError:
return REPLACEMENT
def dollarToNum(x):
return x[1:].replace(",","")
def readSuitable():
f = open("suitable.csv")
toReturn = f.readline().split(',')
f.close()
toReturn[-1] = toReturn[-1][:-1]
return toReturn
def scanIn(filename, dynAttrs, staticAttrs = None):
f = open(filename, 'r')
headers = f.readline().split(',')
headers[0] = "UNITID"
dynIndex = []
for a in dynAttrs:
try:
dynIndex.append(headers.index(a))
except ValueError:
print a, "not found"
if staticAttrs is not None:
staticIndex = []
for a in staticAttrs:
try:
staticIndex.append(headers.index(a))
except ValueError:
print a, "not found"
if staticAttrs is not None:
staticData = []
dynData = []
for l in f:
tmp = l.split(',')
dynData.append([convert(tmp[a]) for a in dynIndex])
if staticAttrs is not None:
staticData.append([tmp[a] for a in staticIndex])
f.close()
if staticAttrs is not None:
return dynData, staticData
else:
return dynData
def loadLocations(data):
cnx = mysql.connector.connect(user=USERNAME, password=PASSWORD,
host='127.0.0.1', database="MCM")
cursor = cnx.cursor()
for d in data:
u_id, name, city, state = d
# Insert location (fail silently if exists)
locInsert = ("INSERT INTO Location"
"(city, state)"
"VALUES (%s, %s)"
"ON DUPLICATE KEY UPDATE city=city")
cursor.execute(locInsert, (city, state))
cnx.commit()
cursor.close()
cnx.close()
def loadSchools(data):
cnx = mysql.connector.connect(user=USERNAME, password=PASSWORD,
host='127.0.0.1', database="MCM")
cursor = cnx.cursor()
for d in data:
u_id, name, city, state = d
lookup = ("SELECT loc_id FROM Location WHERE city=%s"
"AND state=%s")
cursor.execute(lookup, (city, state))
loc_id = cursor.fetchone()
if loc_id is None:
pass
print u_id, name, "Location not found"
else:
loc_id = loc_id[0]
insert = ("INSERT INTO School"
"(unit_id, name, loc_id)"
"VALUES (%s, %s, %s)")
cursor.execute(insert, (u_id, name, loc_id))
cnx.commit()
cursor.close()
cnx.close()
def loadTimeSlices(data, year):
cnx = mysql.connector.connect(user=USERNAME, password=PASSWORD,
host='127.0.0.1', database="MCM")
cursor = cnx.cursor()
counter = 0
for d in data:
check = ("SELECT COUNT(*) FROM School WHERE unit_id=%s")
cursor.execute(check, tuple([d[0]]))
if cursor.fetchone()[0] == 0:
counter += 1
else:
tmp_str = ",".join(["%s" for _ in range(39)])
d.append(year)
insert = ("INSERT IGNORE INTO TimeSlice"
"(unit_id, ug, ug_nra, ug_unkn, ug_whitenh, ug_blacknh,"
" ug_api, ug_aianold, ug_hispold, tuition_in, tuition_out,"
" tuitfte, inexpfte, d150_l4, yr2cmp, yr2wdr, fyr2cmp,"
" fyr2wdr, yr3cmp, yr3wdr, yr4cmp, yr4wdr, findep, frstgen,"
" parms, parhs, parps, indavg, depavg, debtgrad, debtngrad,"
" debtdep, debtind, debtfem, debtmal, debtfrst, debtnfrst,"
" repaydebt, year)"
"VALUES (" + tmp_str + ")")
cursor.execute(insert, tuple(d))
cnx.commit()
cursor.close()
cnx.close()
print str(counter), "schools not matched."
def loadDonations(data):
cnx = mysql.connector.connect(user=USERNAME, password=PASSWORD,
host='127.0.0.1', database="MCM")
cursor = cnx.cursor()
for d in data:
print d
lookup = ("SELECT unit_id, loc_id FROM School WHERE name=%s")
cursor.execute(lookup, tuple([d[0]]))
res = cursor.fetchone()
if res is None:
print d[0], "not found."
else:
insert = ("INSERT INTO Donation"
"(unit_id, loc_id, start_year, end_year, amount)"
"VALUES (%s,%s,%s,%s,%s)")
params = (res[0], res[1], d[4], d[5], dollarToNum(d[2]))
cursor.execute(insert, map(str, params))
cnx.commit()
cursor.close()
cnx.close()
def loadAll():
dyn, stat = scanIn("CollegeScorecard_Raw_Data/MERGED2013_PP.csv",
readSuitable(), STATIC_ATTRS)
loadLocations(stat)
loadSchools(stat)
l = Lumina()
loadDonations(l.findGrants())
loadTimeSlices(dyn, 2013)
for yr in range(1996, 2013):
dyn = scanIn("CollegeScorecard_Raw_Data/MERGED" + str(yr)
+ "_PP.csv", readSuitable())
loadTimeSlices(dyn, yr)
if __name__ == '__main__':
loadAll()