Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from simpleeval import simple_eval
print(simple_eval("21 + 21")) # 42
print(simple_eval("100 - 1")) # 99
print(simple_eval("2 / 3")) # 0.6666666666666666
print(simple_eval("2 // 3")) # 0
print(simple_eval("10 * 10")) # 100
print(simple_eval("2 ** 10")) # 1024
print(simple_eval("15 % 4")) # 3
print()
print(simple_eval("15 == 4")) # False
print(simple_eval("15 != 4")) # True
print(simple_eval("1 < 4")) # True
print(simple_eval("1 > 4")) # False
print(simple_eval("1 <= 4")) # True
print(simple_eval("1 >= 4")) # False
print()
print(simple_eval("'ell' in 'Hello'")) # True
print(simple_eval("'123' in 'ab123c'")) # True
print(simple_eval("'ell' not in 'Hello'")) # False
# pip install simpleeval
from simpleeval import simple_eval
print(simple_eval("21 + 21")) # 42
print(simple_eval("100 - 1")) # 99
print(simple_eval("2 / 3")) # 0.6666666666666666
print(simple_eval("2 // 3")) # 0
print(simple_eval("10 * 10")) # 100
print(simple_eval("2 ** 10")) # 1024
print(simple_eval("15 % 4")) # 3
print()
print(simple_eval("15 == 4")) # False
print(simple_eval("15 != 4")) # True
print(simple_eval("1 < 4")) # True
print(simple_eval("1 > 4")) # False
print(simple_eval("1 <= 4")) # True
print(simple_eval("1 >= 4")) # False
print()
print(simple_eval("'ell' in 'Hello'")) # True
print(simple_eval("'123' in 'ab123c'")) # True
print(simple_eval("'ell' not in 'Hello'")) # False
def test_bytes_array_test(self):
self.t("'20000000000000000000'.encode() * 5000",
'20000000000000000000'.encode() * 5000)
with self.assertRaises(simpleeval.IterableTooLong):
self.t("'123121323123131231223'.encode() * 5000", 20)
def test_default_functions(self):
self.assertEqual(simple_eval('rand() < 1.0 and rand() > -0.01'), True)
self.assertEqual(simple_eval('randint(200) < 200 and rand() > 0'), True)
def setUp(self):
self.s = EvalWithCompoundTypes()
def setUp(self):
self.s = EvalWithCompoundTypes()
def test_use_func(self):
self.s = EvalWithCompoundTypes(functions={"map": map, "str": str})
self.t('list(map(str, [-1, 0, 1]))', ['-1', '0', '1'])
with self.assertRaises(NameNotDefined):
self.s.eval('list(map(bad, [-1, 0, 1]))')
with self.assertRaises(FunctionNotDefined):
self.s.eval('dir(str)')
with self.assertRaises(FeatureNotAvailable):
self.s.eval('str.__dict__')
self.s = EvalWithCompoundTypes(functions={"dir": dir, "str": str})
self.t('dir(str)', dir(str))
def test_none(self):
""" what to do when names isn't defined, or is 'none' """
with self.assertRaises(NameNotDefined):
self.t("a == 2", None)
self.s.names["s"] = 21
with self.assertRaises(NameNotDefined):
self.t("s += a", 21)
self.s.names = None
with self.assertRaises(InvalidExpression):
self.t('s', 21)
self.s.names = {'a': {'b': {'c': 42}}}
with self.assertRaises(AttributeDoesNotExist):
self.t('a.b.d**2', 42)
def _eval_call(self, node):
if isinstance(node.func, ast.Attribute):
raise simpleeval.FeatureNotAvailable("No methods please, we're British")
return super(EvalNoMethods, self)._eval_call(node)
def test_string_format(self):
# python has so many ways to break out!
with self.assertRaises(simpleeval.FeatureNotAvailable):
self.t('"{string.__class__}".format(string="things")', 0)
with self.assertRaises(simpleeval.FeatureNotAvailable):
self.s.names['x'] = {"a": 1}
self.t('"{a.__class__}".format_map(x)', 0)
if sys.version_info >= (3, 6, 0):
self.s.names['x'] = 42
with self.assertRaises(simpleeval.FeatureNotAvailable):
self.t('f"{x.__class__}"', 0)
self.s.names['x'] = lambda y: y
with self.assertRaises(simpleeval.FeatureNotAvailable):
self.t('f"{x.__globals__}"', 0)