Package: py-expression-eval
Python Mathematical Expression EvaluatorBased on js-expression-eval, by Matthew Crumley (email@matthewcrumley.com,http://silentmatt.com/)https://github.com/silentmatt/js-expression-evalPorted to Python and modified by @cansadadeserfeliz.You are free to use and modify this code in anyway you find useful. Please leave this comment in the code
to acknowledge its original source. If you feel like it, I enjoy hearing about projects that use my code,
but don't feel like you have to let me know or ask permission.Installationpip install py_expression_evalTestspython setup.py testDocumentationAll the classes and methods ofpy-expression-evalwere written as similar as possible to their analogues fromjs-expression-evalto make it easier to use for validation on back-end side.ParserParseris the main class of the library that contains the methods to parse, evaluate and simplify mathematical expressions. In order to use the library you need to create an instance of this class:>frompy_expression_evalimportParser>parser=Parser()Once you instantiatedParserclass, you can createExpressionobject usingparsemethod:>parser.parse('2 * 3')Out:<py_expression_eval.Expressioninstanceat0x7f40cc4e5ef0>Parser.Expressionevaluate()takes a dictionary with variables as a parameter and returns the value of the expression:>parser.parse('2 * 3').evaluate({})Out:6>parser.parse('2 * 3.0').evaluate({})Out:6.0>parser.parse('2 * x').evaluate({'x':7})Out:14>parser.parse('2 * x').evaluate({'x':7.0})Out:14.0substitute()creates a new expression where specified variables are replaces with a new expression. For example, to replacexwith3 + xin2 * xexpression we use the following code:>parser.parse('2 * x').substitute('x','3 + x').toString()Out:'(2*(3+x))'variables()returns a list of the variables for the expression:>parser.parse('2 * x + y').variables()Out:['x','y']simplify()simplifies the expression. For example,>parser.parse('2 * 3 * x + y').simplify({}).toString()Out:'((6*x)+y)'>parser.parse('2 * 3 * x + y').simplify({'x':-1}).toString()Out:'(-6+y)'>parser.parse('cos(PI) + x').simplify({}).toString()Out:'(-1.0+x)'toString()converts the expression to a string.Available operators, constants and functionsExpressionExampleOutput+parser.parse('2 + 2').evaluate({})4-parser.parse('3 - 1').evaluate({})2*parser.parse('2 * 3').evaluate({})6/parser.parse('5 / 2').evaluate({})2.5%parser.parse('5 % 2').evaluate({})1^parser.parse('5 ^ 2').evaluate({})25.0PIparser.parse('PI').evaluate({})3.141592653589793Eparser.parse('E').evaluate({})2.718281828459045sin(x)parser.parse('sin(0)').evaluate({})0.0cos(x)parser.parse('cos(PI)').evaluate({})- 1.0tan(x)parser.parse('tan(0)').evaluate({})0.0asin(x)parser.parse('asin(0)').evaluate({})0.0acos(x)parser.parse('acos(-1)').evaluate({})3.141592653589793atan(x)parser.parse('atan(PI)').evaluate({})1.2626272556789118log(x)parser.parse('log(1)').evaluate({})0.0log(x, base)parser.parse('log(16, 2)').evaluate({})4.0abs(x)parser.parse('abs(-1)').evaluate({})1ceil(x)parser.parse('ceil(2.7)').evaluate({})3.0floor(x)parser.parse('floor(2.7)').evaluate({})2.0round(x)parser.parse('round(2.7)').evaluate({})3.0exp(x)parser.parse('exp(2)').evaluate({})7.38905609893065andparser.parse('a and b').evaluate({'a':True, 'b':True})Trueorparser.parse('a or b').evaluate({'a':True, 'b':True})Truexorparser.parse('a xor b').evaluate({'a':True, 'b':True})Falsenotparser.parse('a and not b').evaluate({'a':True, 'b':True})Falseinparser.parse('1 in (1,2,3)').evaluate({})TrueExamplesfrompy_expression_evalimportParserparser=Parser()parser.parse('2 * 3').evaluate({})# 6parser.parse('2 ^ x').evaluate({'x':3})# 8.0parser.parse('2 * x + 1').evaluate({'x':3})# 7parser.parse('2 + 3 * x').evaluate({'x':4})# 14parser.parse('(2 + 3) * x').evaluate({'x':4})# 20parser.parse('2-3^x').evaluate({'x':4})# -79.0parser.parse('-2-3^x').evaluate({'x':4})# -83.0parser.parse('-3^x').evaluate({'x':4})# -81.0parser.parse('(-3)^x').evaluate({'x':4})# 81.0parser.parse('2*x + y').evaluate({'x':4,'y':1})# 9parser.parse('round(log(2.7))').evaluate({})# 1.0# substituteexpr=parser.parse('2 * x + 1')expr2=expr.substitute('x','4 * x')# ((2*(4*x))+1)expr2.evaluate({'x':3})# 25# simplifyexpr=parser.parse('x * (y * atan(1))').simplify({'y':4})expr.toString()# x*3.141592expr.evaluate({'x':2})# 6.283185307179586# get variablesexpr=parser.parse('x * (y * atan(1))')expr.variables()# ['x', 'y']expr.simplify({'y':4}).variables()# ['x']Available operationsfrompy_expression_evalimportParserparser=Parser()parser.parse('2 + 3').evaluate({})# 5parser.parse('2 - 3').evaluate({})# -1parser.parse('2 * 3').evaluate({})# 6parser.parse('2 / 3').evaluate({})# 0.6666666666666666parser.parse('2 % 3').evaluate({})# 2parser.parse('-2').evaluate({})# -2parser.parse('abs(-2)').evaluate({})# 2parser.parse('ceil(1.4)').evaluate({})# 2.0parser.parse('floor(1.4)').evaluate({})# 1.0parser.parse('round(1.4)').evaluate({})# 1.0parser.parse('2^3').evaluate({})# 8.0parser.parse('sqrt(16)').evaluate({})# 4.0parser.parse('sin(3.14)').evaluate({})# 0.0015926529164868282parser.parse('cos(3.14)').evaluate({})# -0.9999987317275395parser.parse('tan(3.14)').evaluate({})# -0.0015926549364072232parser.parse('asin(1)').evaluate({})# 1.5707963267948966parser.parse('acos(1)').evaluate({})# 0.0parser.parse('atan(1)').evaluate({})# 0.7853981633974483parser.parse('log(2.7)').evaluate({})# 0.9932517730102834parser.parse('exp(1)').evaluate({})# 2.718281828459045parser.parse('log(E)').evaluate({})# 1.0parser.parse('cos(PI)').evaluate({})# -1.0parser.parse('x||y').evaluate({'x':2,'y':3})# '23'parser.parse('num in (1,2,3)').evaluate({'num':1})# Trueparser.parse('"word" in "word in sentence"').evaluate({})# TrueUpload package to PyPiGenerating distribution archivespython3 setup.py sdist bdist_wheelUpload distributionls -a dist/
twine upload dist/py_expression_eval-0.3.9*Check on:https://pypi.org/project/py-expression-eval/0.3.9/More details:https://packaging.python.org/tutorials/packaging-projects/