forked from panyang/MyJobs
/
mocparse.py
222 lines (200 loc) · 6.87 KB
/
mocparse.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
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
#! /usr/bin/python
import csv
from django.utils import simplejson
from slugify import slugify
"""
CSV parser for MOC --> Onet codes
"""
MOC_BRANCH_ORDINAL = 0
MOC_CODE_ORDINAL = 2
MOC_TITLE_ORDINAL = 3
ONET1_ORDINAL = 4
ONET2_ORDINAL = 5
ONET3_ORDINAL = 6
ONET4_ORDINAL = 7
def parse_moc_data(file_in, file_out):
"""
Parses the csv file.
file_in: csv formatted file
file_out: outputted JSON
Returns JSON of this format:
[
"model": "seo.moc",
"pk": 1,
"fields": {
"code": 0107,
"title": "Undersea Medical Officer",
"branch": "navy",
"title_slug": "undersea-medical-officer",
"onets": "29106300"
},
"model": "seo.moc",
"pk": 2,
"fields": {
"code": 1234,
"title": "Radiologist",
"branch": "army",
"title_slug": "radiologist",
"onets": "29134300"
}
]
"""
branch_translations = {
"N": "navy",
"F": "air-force",
"M": "marines",
"C": "coast-guard",
"A": "army"
}
mocs = []
moc_file = open(file_in, "rU")
csv_reader = csv.reader(moc_file)
i = 1
dupe_list = []
for line in csv_reader:
branch = branch_translations[line[MOC_BRANCH_ORDINAL]]
code = line[MOC_CODE_ORDINAL]
title = line[MOC_TITLE_ORDINAL]
# we dont want to reprocess any moc/branches that might have
# already been mapped earlier in the file
dupe_string = "{code}-{branch}".format(code=code, branch=branch)
if dupe_string not in dupe_list:
onets = []
# This all just needs to be factored out into its own function.
# This is just terrible.
if line[ONET1_ORDINAL]:
try:
onet_code = line[ONET1_ORDINAL]
# we dont use the .99 convention anymore and it was still
# in the Excel crosswalk, so we change it to .00 instead
onet_code = onet_code.replace('.99','.00')
# the next two lines just strip the - and . out of the
# onet code so that we just use it as an integer
onet_code = onet_code.replace('-','')
onet_code = onet_code.replace('.', '')
onets.append(onet_code)
except KeyError, e:
print e
if line[ONET2_ORDINAL]:
try:
onet_code = line[ONET2_ORDINAL]
onet_code = onet_code.replace('.99','.00')
onet_code = onet_code.replace('-','')
onet_code = onet_code.replace('.', '')
onets.append(onet_code)
except KeyError, e:
print e
if line[ONET3_ORDINAL]:
try:
onet_code = line[ONET3_ORDINAL]
onet_code = onet_code.replace('.99','.00')
onet_code = onet_code.replace('-','')
onet_code = onet_code.replace('.', '')
onets.append(onet_code)
except KeyError, e:
print e
if line[ONET4_ORDINAL]:
try:
onet_code = line[ONET4_ORDINAL]
onet_code = onet_code.replace('.99','.00')
onet_code = onet_code.replace('-','')
onet_code = onet_code.replace('.', '')
onets.append(onet_code)
except KeyError, e:
print e
moc = {
"model": "seo.moc",
"pk": i,
"fields": {
"code": code,
"title": title,
"branch": branch,
"title_slug": slugify(title),
"onets": onets
}
}
mocs.append(moc)
dupe_list.append(dupe_string)
i += 1
moc_file.close()
# The crosswalk file size is pretty large, so we just create a file
# with the JSON in it.
fixture_file = open(file_out, 'w')
fixture_file.write(simplejson.dumps(mocs))
fixture_file.close()
def parse_onet_codes(file_name):
"""
Parses a tab delimited file of:
Onet_Code: Onet_Title
We reformat the ONET code to an integer:
12-3456.78 ---> 12345678
Returns JSON of this format:
[
"model": "seo.onet",
"pk": 12345678,
"fields": {
"title": "job title #1"
},
"model": "seo.onet",
"pk": 23456781,
"fields": {
"title": "job title #2"
}
]
"""
mappings = []
map_file = open(file_name, "rU")
i = 1
for line in map_file:
onet_code, onet_title = line.split('\t')
onet_code = onet_code.replace('-','')
onet_code = onet_code.replace('.', '')
mapping = {
"model": "seo.onet",
"pk": onet_code,
"fields": {
"title": onet_title.rstrip()
}
}
mappings.append(mapping)
i = i+1
map_file.close()
return simplejson.dumps(mappings)
def find_moc_with_non_matching_onet(onet_file, moc_file):
"""
This function is used to find any MOCs, from the Excel spreadsheet
I was given, that have an ONET code mapped to it that does NOT exist
in the database that Kyle uses to classify jobs in the XML feed.
Outputs a list of ONETs that do not exist.
"""
non_matches = []
# we create the onet list from the file Kyle gave us
onets = []
map_file = open(onet_file, "rU")
for line in map_file:
onet_code, onet_title = line.split('\t')
# Do we really need these two commented lines in the code?
#onet_code = onet_code.replace('-','')
#onet_code = onet_code.replace('.', '')
onets.append(onet_code)
map_file.close()
# parse the Excel spreadsheet, looking for not matching ONETs
mocs = open(moc_file, "rU")
csv_reader = csv.reader(mocs)
# This for loop can be cleaned up considerably given how repetitive
# it is.
for line in csv_reader:
onet1 = line[ONET1_ORDINAL]
onet2 = line[ONET2_ORDINAL]
onet3 = line[ONET3_ORDINAL]
onet4 = line[ONET4_ORDINAL]
if onet1 and onet1 not in onets + non_matches:
non_matches.append(onet1)
if onet2 and onet2 not in onets + non_matches:
non_matches.append(onet2)
if onet3 and onet3 not in onets + non_matches:
non_matches.append(onet3)
if onet4 and onet4 not in onets + non_matches:
non_matches.append(onet4)
for match in non_matches:
print match