Refactored

parent 24dbbea2
import re
from tempfile import NamedTemporaryFile
import gradaudit.util as util
from subprocess import check_output
import re
import gradaudit.util as util
class Response(object):
""" GAS advising information """
_missing_class_re = r'hasNotTaken\({},([a-z]+[1-4][0-9V][0-9][0-9])\)'
_missing_hours_re = r'needsHours\({},([a-zA-Z]+),([0-9]+)\)'
_missing_electives_re = r'needsElectiveHours\({},([0-9]+)\)'
_grad = r'ableToGraduate\({}\)'
class Response:
def __init__(self, transcript):
self.transcript = transcript
main_contents = util.gen_main(transcript)
answer_set = Response._run(main_contents)
advice = Response._extract_advice(answer_set, transcript.name)
self.missing_classes = advice[0]
self.missing_hours = advice[1]
self.missing_electives = advice[2]
self.can_grad = advice[3]
@classmethod
def _run(cls, main_contents):
""" Run the program under s(ASP) and return the answer set """
with NamedTemporaryFile() as fil:
fil.write(main_contents)
fil.flush()
answer_set = check_output(['sasp',fil.name])
answer_set = check_output(['sasp', fil.name])
return answer_set
self.missing_classes = re.findall('hasNotTaken\({},([a-z]+[1-4][0-9V][0-9][0-9])\)'
.format(transcript.name),answer_set)
self.missing_hours = re.findall('needsHours\({},([a-zA-Z]+),([0-9]+)\)'.format(transcript.name),answer_set)
self.missing_electives = re.findall('needsElectiveHours\({},([0-9])\)'.format(transcript.name),answer_set)
self.can_grad = 'ableToGraduate({0})'.format(transcript.name) in answer_set
@classmethod
def _extract_advice(cls, answer_set, name):
""" Parses answer set for advising information """
class_re = Response._missing_class_re.format(name)
hours_re = Response._missing_hours_re.format(name)
electives_re = Response._missing_electives_re.format(name)
grad = Response._grad.format(name)
if self.missing_electives == []:
self.missing_electives = 0
classes = re.findall(class_re, answer_set)
hours = re.findall(hours_re, answer_set)
electives = re.findall(electives_re, answer_set)
if electives == []:
electives = 0
else:
self.missing_electives = self.missing_electives[0]
electives = int(electives[0])
can_grad = grad in answer_set
return (classes, hours, electives, can_grad)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment