I am working on a python script that generates pdfs for math-tests via pylatex. So far I am using
str(random.randint(8,32)) +' + ' + str(random.randint(8,121))
and similar for generating randomized assignments.
I don't want to hard-code everything like above but I want to use a database approach to store possible assignments. My idea in this direction is a relational database
(student) --n (assignment_type)
where the students each get personilized assignments, for instance Peter will get assigments of the type
( (random sign) (random number 2-8) x (+ or -) (random sign) (random number)) (*)
and
( (random number 2323-9999):11)
I am unsure on how to save this datastructure in a database and how to translate it into python code. My idea here is for the database table 'assignment_type':
INT number of randomized vars
INT number of signs
ARRAY List of signs
ARRAY (lower und upper bound for each random number)
STRING '(s1r1s2r1)'
Where '(s1r1s2(s3r1))' would be translated in python to the above problem (*) ("( sign1 random1 sign2 (sign3)random2)") I am very unsure if this way of handling the problem is a stupid idea. I ask you hereby to review my approach and if possible point me in a better direction of handling this kind of data-structure.
A quick idea:
First, define a few random functions for later use.
Then, make a
dict
with reference to all these functions:Then, write your parsing functions:
Now you can call them on template strings:
There is a ton of room for improvement. In particular, the function
fill_in
should be written in a way that handles variable number of arguments.