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_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)
__author__ = 'ipetrash'
# pip install simpleeval
from simpleeval import simple_eval
print(simple_eval("21 + 21")) # 42
print(simple_eval("'21' + '21'")) # '2121'
print(simple_eval("int('21' + '21')")) # 2121
print()
print(simple_eval("2 + 2 * 2")) # 6
print(simple_eval('10 ** 123')) # 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
print(simple_eval("21 + 19 / 7 + (8 % 3) ** 9")) # 535.7142857142857
print()
# Call methods
print(simple_eval("'1,2,3,4'.split(',')")) # ['1', '2', '3', '4']
print(simple_eval("'+'.join('1234')")) # 1+2+3+4
print()
from simpleeval import EvalWithCompoundTypes
print(EvalWithCompoundTypes().eval('list("Hello").count("l")')) # 2
print(simple_eval('list("Hello").count("l")', functions={'list': list})) # 2
print()
# User functions
print(simple_eval("square(11)", functions={"square": lambda x: x * x})) # 121
import math
# +------------+---------------------------------------+
# | rand() | Return a random float between 0 and 1 |
# +------------+---------------------------------------+
# | int(x) | Convert x to an int. |
# +------------+---------------------------------------+
# | float(x) | Convert x to a float. |
# +------------+---------------------------------------+
# | str(x) | Convert x to a str (unicode in py2) |
# +------------+---------------------------------------+
# pip install simpleeval
from simpleeval import simple_eval
print(simple_eval("square(11)", functions={"square": lambda x: x * x})) # 11
print(simple_eval("double(21)", functions={"double": lambda x: x * 2})) # 42
print(simple_eval("my_pow(11, 2)", functions={"my_pow": lambda a, b: a ** b})) # 121
print()
def double(x):
return x * 2
my_functions = {
"d": double,
"double": double
}
print(simple_eval("d(100) + double(1)", functions=my_functions)) # 202
# +------------+---------------------------------------+
# | int(x) | Convert x to an int. |
# +------------+---------------------------------------+
# | float(x) | Convert x to a float. |
# +------------+---------------------------------------+
# | str(x) | Convert x to a str (unicode in py2) |
# +------------+---------------------------------------+
# pip install simpleeval
from simpleeval import simple_eval
print(simple_eval("square(11)", functions={"square": lambda x: x * x})) # 11
print(simple_eval("double(21)", functions={"double": lambda x: x * 2})) # 42
print(simple_eval("my_pow(11, 2)", functions={"my_pow": lambda a, b: a ** b})) # 121
print()
def double(x):
return x * 2
my_functions = {
"d": double,
"double": double
}
print(simple_eval("d(100) + double(1)", functions=my_functions)) # 202
import simpleeval
my_functions = simpleeval.DEFAULT_FUNCTIONS.copy()
def check_row(self, cells):
# Prepare names
names = {}
for cell in cells:
if None not in [cell.get('header'), cell.get('value')]:
try:
names[cell['header']] = float(cell['value'])
except ValueError:
pass
# Check constraint
try:
# This call should be considered as a safe expression evaluation
# https://github.com/danthedeckie/simpleeval
assert simple_eval(self.__constraint, names=names)
except Exception:
row_number = cells[0]['row-number']
message = 'Custom constraint "{constraint}" fails for row {row_number}'
message_substitutions = {
'constraint': self.__constraint,
}
error = Error(
'custom-constraint',
row_number=row_number,
message=message,
message_substitutions=message_substitutions
)
return [error]
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'ipetrash'
# SOURCE: https://github.com/danthedeckie/simpleeval/blob/master/README.rst#names
# pip install simpleeval
from simpleeval import simple_eval, SimpleEval
print(simple_eval("a + b", names={"a": 11, "b": 100})) # 111
print(simple_eval("a + b * 2", names={"a": 2, "b": 2})) # 6
print(simple_eval("a + b * 2", names=dict(a=2, b=2))) # 6
print()
# Hand the handling of names over to a function
def name_handler(node):
return ord(node.id.lower()) - 96
# a -- 1, b -- 2, c -- 3
print(simple_eval('A + b + c', names=name_handler)) # 6
# Hand the handling of names over to a function
def name_handler_dict(node):
def eval_datetime_formula(string):
''' evaluate a simple date/time formula, returning a unix datetime stamp '''
replacements = [('WEEKS', '* 604800'),
('WEEK', '* 604800'),
('DAYS', '* 86400'),
('DAY', '* 86400'),
('MONTHS', '* 2592000'), # 30 day month...
('MONTH', '* 2592000'),
]
for rep_str, out_str in replacements:
string = string.replace(rep_str, out_str)
return simple_eval(string, names={'NOW': time()})
def _is_condition_include(self, json_match):
""" return true if a condition doesn't exists or if the condition is evaluation is true else false """
if isinstance(json_match, dict) and "condition" in json_match:
condition = json_match["condition"]
if condition != None and isinstance(condition, str) and simple_eval != None:
return simple_eval(condition)
else:
return True
# TODO property to manage
return True