import inspect
import re


def getSubjDict(in_content, examPat):
    """
    Finds the subjects and subject code from in_content.
    Returns a dict with the subject code as key and subject as
    value.
    """

    err_msg = ("%s expected argument of type 'list','str'; %s,%s given" %
                (inspect.stack()[0][3], type(in_content), type(examPat)))
    assert (type(in_content) is list and type(examPat) is str), err_msg

    # Only handles 2008 pattern files for now.
    if examPat != '2008':
        return []

    subj_re = (r'((?:[0-9]{3}|[0-9]{2}[A-Z]{1}|[0-9]{2})\s*\.\s*'
                    r'[-A-Z1-3\s\.&\(\)\,\/]+)\s*(?=PP|PR|OR|TW)')
    subjects = []
    for line in in_content:
        ret_obj = re.findall(subj_re, line)
        if ret_obj != []:
            ret_obj = [s.strip() for s in ret_obj]
            [subjects.append(s_i) for s_i in ret_obj if s_i not in subjects]

    # Split subject code and the subject name in list of subjects
    subj_code_re = (r'^([0-9]{3}|[0-9]{2}[A-Z]{1}|[0-9]{2})\s*\.\s*'
                            r'([-A-Z1-3\s\.&\(\)\,\/]+)$')
    subjects = [re.findall(subj_code_re, s_i) for s_i in subjects]

    # Flatten 'subjects' to make a dict out of it.
    subjects = dict([s_pair for sublist in subjects for s_pair in sublist])

    return subjects