def get_solution(self): """Uses a parser build out of the ply module, instead of exec, which allows me to return as fractions, instead of performing integer division""" return set([parser.parse(self.problem)])
def to_html(self): """Render the problem in a form that is convertable to TeX by MathJax. Note that inline MathJax equations are bracketed by \(...\). Prepends a prompt""" prompt = "Reduce" fractions = re.findall('\d+/\d+',self.problem) h = '\('+self.problem+'\)' for f in fractions: a,b = f.split('/') text = r'\\frac{%s}{%s}'%(a,b) h = re.sub('\d+/\d+',text,h,count = 1) return [prompt,h] def __str__(self): return self.problem if __name__ == "__main__": assert parser.parse("32 + 4") == 36 a = Arithematic() s = a.solution.pop() print str(a)+' = '+str(s) assert parser.parse(str(a)) == s print a, print a.to_html()[1] print a.new_to_html() #ans = raw_input("> ") #print a.validate(ans)