def _load(self): ''' Loads the defined models from "modellib.sfm". ''' folder = os.path.dirname(__file__) try: config = ConfigObj(os.path.join(folder, 'modellib.sfm')) except configobj.ConfigObjError as e: txt = 'File "modellib.sfm" has an error: {0}'.format(e) raise configobj.ConfigObjError(txt) if not config: warn('File "modellib.sfm" could not read or is empty. ' 'No models in modellib.', SitforcWarning) for modelname in config: params = dict() comment = '' funcstring = '' for key in config[modelname]: if key == 'func': funcstring = config[modelname][key] elif key == 'comment': comment = config[modelname][key] else: params[key] = config[modelname].as_float(key) if not funcstring: warn('Function not defined for model "{0}" ' '(in "modellib.sfm").'.format(modelname), SitforcWarning) continue try: func, latex, ident_params = parse_func(funcstring) except ParseException as e: warn('Function for model "{0}" in "modellib.sfm" has ' 'an error: {1}'.format(modelname, e), SitforcWarning) continue try: func(1, params) except KeyError as e: warn('Param {0} for model "{1}" is not defined ' 'in "modellib.sfm".'.format(e, modelname), SitforcWarning) continue self.lib[modelname] = Model(modelname, func, funcstring, latex, **params) self.lib[modelname].comment = comment
def test_parse_func(self): # exceptions funcstring = 'open("/etc/passwd").read()' # try injection self.assertRaises(ParseException, parse_func, funcstring) funcstring = '(2.3 + 3' # general syntax error self.assertRaises(ParseException, parse_func, funcstring) funcstring = '2 * x + 3 * y' # using not allowed keyword (1) self.assertRaises(ParseException, parse_func, funcstring) funcstring = '2 * 3 + t["dx"] * x' # using not allowed keyword (2) self.assertRaises(ParseException, parse_func, funcstring) # check resulting values funcstring = 'p["dx"]' f = parse_func(funcstring)[0] for value in [-1, 0, 1, 2, 3]: p = dict() p['dx'] = value self.assertEqual(f(0, p), value) funcstring = 'x' f = parse_func(funcstring)[0] g = lambda x: x for value in xrange(-5, 5): self.assertEqual(f(value, None), g(value)) funcstring = '(x - 3) * (x + 2)' f = parse_func(funcstring)[0] funcstring = 'x**2 - x - 6' # funcs are equivalent g = parse_func(funcstring)[0] for value in xrange(-5, 5): self.assertEqual(f(value, None), g(value, None)) self.assertEqual(f(3, None), 0) # root1 self.assertEqual(f(-2, None), 0) # root2 # check generated latex expressions funcstring = '2 + 3' latex = parse_func(funcstring)[1] self.assertEqual(latex, r'$(2 + 3)$') funcstring = '2 - 3' latex = parse_func(funcstring)[1] self.assertEqual(latex, r'$(2 - 3)$') funcstring = '2 * 3' latex = parse_func(funcstring)[1] self.assertEqual(latex, r'$2 \cdot 3$') funcstring = '2 / 3' latex = parse_func(funcstring)[1] self.assertEqual(latex, r'$\frac{2}{3}$') funcstring = '2 ** 3' latex = parse_func(funcstring)[1] self.assertEqual(latex, r'$(2)^{3}$') funcstring = '-(2 + 3)' latex = parse_func(funcstring)[1] self.assertEqual(latex, r'$-((2 + 3))$') funcstring = '-(x)' latex = parse_func(funcstring)[1] self.assertEqual(latex, r'$-(x)$') funcstring = 'x + p["c"]' latex = parse_func(funcstring)[1] self.assertEqual(latex, r'$(x + c)$') funcstring = 'exp(2)' latex = parse_func(funcstring)[1] self.assertEqual(latex, r'$\mathrm{e}^{2}$') funcstring = 'sin(x)' latex = parse_func(funcstring)[1] self.assertEqual(latex, r'$\sin{x}$') funcstring = 'fac(x)' latex = parse_func(funcstring)[1] self.assertEqual(latex, r'$x\mathrm{!}$') # check identified parameters params = ['dx'] funcstring = 'p["dx"]' identified_params = parse_func(funcstring)[2] for param in params: self.assertTrue(param in identified_params) params = ['t1', 't2', 't3'] funcstring = 'p["t1"] + p["t2"] + p["t3"]' identified_params = parse_func(funcstring)[2] for param in params: self.assertTrue(param in identified_params)