Creating Flashcards

I've been studying for another Maths exam. This time it's the Open University M343 "Applications of Probability" course. It's exam time so I've been making flash-cards to study with.

My current process for making the flashcards is to open the PDF course notes, run my Flashcard Creator program, and copy and paste the PDF exercises / solutions into Flashcard Creator, which then saves the PDF regions to a directory.

I open Terminal.app and transfer to the directory with all the PDF regions of exercises / solutions, and run a script that calls my PdfRectToPng program. The PdfRectToPng program converts the PDF files to PNG files and also trims them down, so they aren't A4-sized.

Finally I run the following script on the directory that creates the Anki text import file.

build_anki_import_file.py

#!/usr/bin/python
import os, sys, glob

class Problem:
    def __init__(self, key):
        self.key = key
        self.questions = []
        self.answers = []

if len(sys.argv) != 2 or not os.path.isdir(sys.argv[1]):
    print("Usage: %s <image_dir>" % (sys.argv[0], ))
    exit(0)

problems = {}

image_dir = sys.argv[1]
files = glob.glob(os.path.join(image_dir, '*.png'))
for filename in files:
    bn = os.path.basename(filename)
    probkey = bn[0:11]
    qa_text = bn[12:13]

    if not probkey in problems:
        problems[probkey] = Problem(probkey)
    prob = problems[probkey]
    if qa_text == 'q':
        prob.questions.append(bn)
    else:
        prob.answers.append(bn)

probkeys = problems.keys()
probkeys.sort()
for probkey in probkeys:
    prob = problems[probkey]

    sectnum = probkey[6:8]
    qnum = probkey[9:11]

    #sys.stdout.write("<h2>Section %s Question %s</h2>\
" % (sectnum,     qnum))

    for q in prob.questions:
        sys.stdout.write('<img src="%s" />' % (q, ))
    sys.stdout.write("\    ")
    for a in prob.answers:
        sys.stdout.write('<img src="%s" />' % (a, ))
    sys.stdout.write("\
")