Commit ebf2aaca authored by Matti Wiegmann's avatar Matti Wiegmann

Add sampling from a pools.yaml to exam generation.

parent 2d23e008
......@@ -24,6 +24,7 @@ setuptools.setup(
"pandas",
"python-gitlab",
"plotly",
"requests"
"requests",
"pyyaml"
]
)
......@@ -10,8 +10,10 @@ import shutil
import subprocess
import tarfile
import tempfile
import yaml
from tqdm import tqdm
from random import choice
@click.group()
......@@ -19,6 +21,47 @@ def exams():
"""Commands for generating exam variants and importing into Moodle."""
########################################################################
@exams.command()
@click.argument('pool_yaml')
@click.argument('output_dir')
@click.option('--student-list-file', '-f', help='Tab-separated file. '
'defining students for which variants should be sampled. Must have a header. '
'Header names define variable names, rows define their values. '
'ATTENTION: use latin-1 encoding!', required=True)
def generate_variants(pool_yaml, output_dir, student_list_file):
""" Generate a variant-list-file, where each student in the
student-list-file gets a unique sample of problems, one from each pool defined in the pool_yaml
:param pool_yaml: A yaml file that contains the pools and associated problem sets, i.e.:
\b
pool1:
- 'exam-de-db-introduction-general-questions001-var001-5pts'
- ...
:param output_dir: Path to a directory where to put the generated student-list-file
:param student_list_file: An Example student-list-file may look like this:
\b
STUDENT_NAME\\tSTUDENT_EMAIL
Donald Duck\\tdonald@example.com
Mickey Mouse\\tmickey@example.com
"""
pools = yaml.load(open(pool_yaml, 'r', encoding='latin1'), Loader=yaml.Loader)
dialect = csv.Sniffer().sniff(
next(open(student_list_file, encoding='latin1')))
items = list(csv.DictReader(open(student_list_file, encoding='latin1'),
dialect=dialect))
os.makedirs(output_dir, exist_ok=True)
with open('variant-list-file.csv', 'w', newline='', encoding='latin1') as of:
writer = csv.writer(of, delimiter='\t', dialect=dialect)
variable_string = ["VAR" + str(i) for i in range(1, len(pools.keys())+1)]
writer.writerow(['STUDENT_NAME', 'STUDENT_EMAIL'] + variable_string)
for env in tqdm(items, desc='generating variants'):
variant_string = [f'{choice(variants)}' for variants in pools.values()]
writer.writerow([env['STUDENT_NAME'], env['STUDENT_EMAIL']] + variant_string)
########################################################################
......@@ -238,3 +281,6 @@ def insert_moodle(backup_file, student_list, input_pdf_dir, output_pdf_dir,
f" students! left over: {students.students}"
logging.info(f'Output written to {output_file}')
exams()
\ No newline at end of file
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