Created
April 4, 2014 01:37
-
-
Save iomz/9966365 to your computer and use it in GitHub Desktop.
Grade report parser for Keio University web system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
from HTMLParser import HTMLParser | |
from re import match | |
Grades = { | |
u'\uff21': 'A', | |
u'\uff22': 'B', | |
u'\uff23': 'C', | |
u'\uff30': 'P' | |
} | |
class GPAParser(HTMLParser): | |
def __init__(self): | |
HTMLParser.__init__(self) | |
self.in_td = False | |
self.in_tr = False | |
self.elc = 0 | |
self.course = None | |
self.grade = None | |
self.credit = None | |
self.gpas = [] | |
def handle_starttag(self, tag, attrs): | |
if tag == 'td': | |
self.in_td = True | |
elif tag == 'tr': | |
self.in_tr = True | |
self.elc = 0 | |
def handle_data(self, data): | |
if self.in_tr and self.elc == 0: | |
self.course = data.strip().decode('shift-jis') | |
if self.in_tr and self.elc == 2: | |
tmp = data.strip().decode('shift-jis') | |
if tmp in Grades: | |
self.grade = Grades[tmp] | |
else: | |
self.grade = None | |
if self.in_tr and self.elc == 3 and match(r"\d\.\d",data) is not None: | |
self.credit = float(data) | |
if self.in_tr and self.elc == 7 and self.grade is not None and self.credit is not None: | |
self.gpas.append([self.course, self.grade, self.credit]) | |
def handle_endtag(self, tag): | |
if tag == 'td': | |
self.elc += 1 | |
elif tag == 'tr': | |
self.in_tr = False | |
self.course = None | |
self.grade = None | |
self.credit = None | |
def main(): | |
parser = GPAParser() | |
try: | |
parser.feed(open(u'成績表システム.html').read()) | |
except AssertionError: | |
pass | |
gsum = 0 | |
gc_dict = {} | |
for g in Grades.values(): | |
gc_dict[g] = 0 | |
gpas = parser.gpas | |
gpas.sort(key=lambda row: (row[1],-row[2])) | |
for el in gpas: | |
print el[2], el[1], el[0] | |
gsum += el[2] | |
gc_dict[el[1]] += el[2] | |
print 'Total credits: %.1f, GPA = (4*A[%.1f] + 3*B[%.1f] + 2*C[%.1f])/Total credits[%.1f] = %.2f[/4.0]' % (gsum, gc_dict['A'], gc_dict['B'], gc_dict['C'], gsum, (4*gc_dict['A']+3*gc_dict['B']+2*gc_dict['C'])/gsum) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment