#!/usr/bin/python3 # COMP3311 21T3 Ass2 ... print list of rules for a program or stream import sys import psycopg2 import re from helpers import * # define any local helper functions here # Requires an array with with: name, min_req, max_req, type, definition, defby def printAcademicRequirements(rules, db): print("Academic Requirements:") for (name, min_req, max_req, type, definition, defby) in rules: def_count = str.count(definition, ",") # There is some weirdly specific behaviour to emulate when printing. While # this might pass the tests, it is unlikely to continue doing so. # According to our lecturer we won't be tested on edge cases, so I can only # hope this is enough. if type == "DS": print(f"{min_req} stream(s) from {name}") printStreams(definition, db) elif type == "CC" or type == "PE": print(getCountString(def_count, min_req, max_req, name)) printSubjects(definition, defby, db) elif type == "GE": print(f"{min_req} UOC of {name}") elif type == "FE": print(f"at least {min_req} UOC of Free Electives") else: print(f"UNEXPECTED {name}, {min_req}, {max_req}, {type}, {defby}") ### set up some globals usage = f"Usage: {sys.argv[0]} (ProgramCode|StreamCode)" db = None ### process command-line args argc = len(sys.argv) if argc < 2: print(usage) exit(1) code = sys.argv[1] if len(code) == 4: codeOf = "program" elif len(code) == 6: codeOf = "stream" try: db = psycopg2.connect("dbname=mymyunsw") if codeOf == "program": progInfo = getProgram(db,code) if not progInfo: print(f"Invalid program code {code}") exit() print(f"{progInfo[0]} {progInfo[1]}, {progInfo[2]} UOC, {progInfo[3] / 12.0} years") print(f"- offered by {progInfo[4]}"); cur = db.cursor() query = """ SELECT R.name, R.min_req, R.max_req, R.type, AOG.definition, AOG.defby FROM Program_Rules as PR, Rules as R, Academic_Object_Groups AS AOG WHERE PR.program = %s AND R.id = PR.rule AND AOG.id = R.ao_group """ cur.execute(query, [code]) printAcademicRequirements(cur.fetchall(), db) cur.close() elif codeOf == "stream": strmInfo = getStream(db,code) if not strmInfo: print(f"Invalid stream code {code}") exit() print(f"{code} {strmInfo[0]}"); print(f"- offered by {strmInfo[1]}") cur = db.cursor() query = """ SELECT R.name, R.min_req, R.max_req, R.type, AOG.definition, AOG.defby FROM Streams AS S, Stream_Rules AS SR, Rules AS R, Academic_Object_groups AS AOG WHERE S.code = %s AND SR.stream = S.id AND SR.rule = R.id AND AOG.id = R.ao_group """ cur.execute(query, [code]) printAcademicRequirements(cur.fetchall(), db) cur.close(); except Exception as err: print(err) finally: if db: db.close()