Deleted gradaudit.py and moved contents to util.py

parent c36e754c
"""
This file is part of Graduation Audit System.
Copyright (C) 2016 Saikiran Srirangapalli <saikiran1096@gmail.com>
Graduation Audit System is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Graduation Audit System is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Graduation Audit System. If not, see <http://www.gnu.org/licenses/>.
"""
"""
Contains various useful functions for use in
main_writer.py and req_writer.py.
"""
import re
import csv
import util
def get_courses():
with open('course.lp', 'r') as fil:
valid_courses = re.findall('[a-z]+[0-9][0-9V][0-9]{2}', fil.read())
return valid_courses
def get_required_courses(degree, major, year):
directory = util.get_package_dir()
filename = '{0}/{1}/{2}/{3}/{2}{3}req.lp'.format(
directory, degree, major, year)
try:
contents = open(filename).read()
except IOError:
raise IOError('error: could not read {}'.format(filename))
req_courses = re.findall('[a-z]+[0-9][0-9V][0-9]{2}', contents)
return req_courses
def parse_transcript(transcript):
if isinstance(transcript, str):
transcript = transcript.splitlines()
csvr = list(csv.reader(transcript))
student = csvr[0][0]
(degree, major, year, spec) = csvr[1]
courses = csvr[2] # list of courses student has taken
return (student, degree, major, year, spec, courses)
def gen_main(student, degree, major, year, spec, courses):
student_fact = "student({},{}).".format(student,major)
if spec != 'none':
spec_string = '_specialization({0},{1}).\n'.format(student, spec)
else:
spec_string = str()
# the _taken(<student>,[c1,c2...cn]). atom
taken_list = '_taken({},['.format(student)
has_taken = str() # series of _hasTaken/2 atoms
try:
req = get_required_courses(degree, major, year)
except IOError as err:
raise err
for course in courses:
# include only those courses that could possibly
# counted as electives i.e. those that are not required.
# in the 'taken list'
if not '_req(%s,required).' % course in req:
taken_list += course + ','
has_taken += '_hasTaken({0},{1}).\n'.format(student, course)
# strip trailing comma
taken_list = taken_list[:-1]
taken_list += ']).\n'
directory = util.get_package_dir()
postamble = "#include '{0}/counting.lp'.\n"\
"#include '{0}/{1}/{2}/{3}/{2}{3}rules.lp'.\n\n"\
'?- _main({4}).\n'.format(directory, degree, major, year, student)
mainlp = '\n'.join(
[student_fact, spec_string, has_taken, taken_list, postamble])
return mainlp
"""
This file is part of Graduation Audit System.
Copyright (C) 2016 Saikiran Srirangapalli <saikiran1096@gmail.com>
Graduation Audit System is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Graduation Audit System is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Graduation Audit System. If not, see <http://www.gnu.org/licenses/>.
"""
from collections import namedtuple
import re
import csv
import os.path as path
# Container for student academic information
Transcript = namedtuple('Transcript', ['name', 'degree', 'major', 'year', 'spec', 'courses'])
def get_package_dir():
"""
Returns the directory that this package is located.
"""
return path.dirname(path.realpath(__file__))
def parse_transcript(transcript):
"""
Parses a transcript in the .csv format specified in the README
and returns and equivalent Transcript object.
"""
try:
csvr = list(csv.reader(transcript))
student = csvr[0][0] # the student's name is on the first line
# extract contents on the second line
(degree, major, year, spec) = csvr[1]
# list of courses student has taken are on the third line
courses = csvr[2]
except IOError:
raise IOError('could not read transcript {}'.format(transcript))
return Transcript(student, degree, major, year, spec, courses)
def get_courses():
"""
Returns a list of all valid courses
"""
with open('course.lp', 'r') as fil:
valid_courses = re.findall('[a-z]+[0-9][0-9V][0-9]{2}', fil.read())
return valid_courses
def get_required_courses(degree, major, year):
"""
Returns a list of courses required to graduate
according to the degree, major, and year.
"""
directory = get_package_dir()
filename = '{0}/{1}/{2}/{3}/{2}{3}req.lp'.format(
directory, degree, major, year)
try:
contents = open(filename).read()
except IOError:
raise IOError('error: could not read {}'.format(filename))
req_courses = re.findall(
r'_req\(([a-z]+[0-9][0-9V][0-9]{2}),required\).', contents)
return req_courses
def gen_main(transcript):
"""
Returns the contents of a s(ASP) program that can
be run to perform a graduation audit on a student
with the provided transcript
"""
student_fact = "student({},{}).".format(transcript.name, transcript.major)
if transcript.spec != 'none':
spec_string = '_specialization({0},{1}).\n'.format(
transcript.name, transcript.spec)
else:
spec_string = str()
# the _taken(<name>,[c1,c2...cn]). atom
taken_list = '_taken({},['.format(transcript.name)
has_taken = str() # series of _hasTaken/2 atoms
try:
req = get_required_courses(
transcript.degree, transcript.major, transcript.year)
except IOError as err:
raise err
for course in transcript.courses:
# include only those courses that could possibly
# counted as electives i.e. those that are not required.
# in the 'taken list' (improves performance)
if not course in req:
taken_list += course + ','
has_taken += '_hasTaken({0},{1}).\n'.format(transcript.name, course)
# strip trailing comma
taken_list = taken_list[:-1]
taken_list += ']).\n'
directory = get_package_dir()
postamble = "#include '{0}/counting.lp'.\n"\
"#include '{0}/{1}/{2}/{3}/{2}{3}rules.lp'.\n\n"\
'?- _main({4}).\n'.format(directory, transcript.degree,
transcript.major, transcript.year, transcript.name)
mainlp = '\n'.join(
[student_fact, spec_string, has_taken, taken_list, postamble])
return mainlp
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