From 3fb3a07c65ce89396586efa657208a2f981da64d Mon Sep 17 00:00:00 2001 From: lukew3 Date: Thu, 17 Dec 2020 16:29:38 -0500 Subject: [PATCH] Added style kwarg --- mathgenerator/__init__.py | 9 +++++++-- mathgenerator/funcs/basic_math/addition.py | 16 +++++++++++----- test.py | 7 ++----- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/mathgenerator/__init__.py b/mathgenerator/__init__.py index cb899b3..2b69d24 100644 --- a/mathgenerator/__init__.py +++ b/mathgenerator/__init__.py @@ -32,8 +32,13 @@ class Generator: ) + " " + self.title + " " + self.generalProb + " " + self.generalSol def __call__(self, *args, **kwargs): - return self.func(*args, **kwargs) - + try: + return self.func(*args, **kwargs) + except TypeError: + # If an error is thrown from kwargs, remove the style element + # This happens if someone trys to get style='latex' for an + del kwargs['style'] + return self.func(*args, **kwargs) def getGenList(): correctedList = genList[-1:] + genList[:-1] diff --git a/mathgenerator/funcs/basic_math/addition.py b/mathgenerator/funcs/basic_math/addition.py index 561c9ce..0cd7f61 100644 --- a/mathgenerator/funcs/basic_math/addition.py +++ b/mathgenerator/funcs/basic_math/addition.py @@ -1,17 +1,23 @@ from .__init__ import * -def additionFunc(maxSum=99, maxAddend=50): - print(maxSum) +def additionFunc(maxSum=99, maxAddend=50, style='raw'): if maxAddend > maxSum: maxAddend = maxSum a = random.randint(0, maxAddend) # The highest value of b will be no higher than the maxsum minus the first number and no higher than the maxAddend as well b = random.randint(0, min((maxSum - a), maxAddend)) c = a + b - problem = str(a) + "+" + str(b) + "=" - solution = str(c) - return problem, solution + + if style == 'latex': + problem = "\(" + str(a) + '+' + str(b) + "\)" + solution = str(c) + return problem, solution + else: + problem = str(a) + "+" + str(b) + "=" + solution = str(c) + return problem, solution + addition = Generator("Addition", 0, "a+b=", "c", additionFunc) diff --git a/test.py b/test.py index a29b04c..33ca0af 100644 --- a/test.py +++ b/test.py @@ -10,9 +10,6 @@ from mathgenerator import mathgen # print(item[2]) # print(mathgen.getGenList()) -#print(mathgen.genById(10)) -#Make a pdf with 10 problems of generator id 1 -# mathgen.makePdf(0, 10) - -print(mathgen.genById(0, maxSum=20)) +print(mathgen.genById(0, maxSum=20, style='latex')[0]) +print(mathgen.genById(1, style='latex'))