{ "info": { "author": "vero4ka", "author_email": "vero4ka.ru@gmail.com", "bugtrack_url": null, "classifiers": [], "description": "# Python Mathematical Expression Evaluator\n\n[![Build Status](https://travis-ci.org/Axiacore/py-expression-eval.svg?branch=master)](https://travis-ci.org/Axiacore/py-expression-eval)\n\n[![PyPi version](https://img.shields.io/pypi/v/py_expression_eval.svg)](https://pypi.python.org/pypi/py_expression_eval/)\n[![PyPi downloads](https://img.shields.io/pypi/dm/py_expression_eval.svg)](https://pypi.python.org/pypi/py_expression_eval/)\n\n[![Coverage Status](https://coveralls.io/repos/github/Axiacore/py-expression-eval/badge.svg?branch=master)](https://coveralls.io/github/Axiacore/py-expression-eval?branch=master)\n\nBased on js-expression-eval, by Matthew Crumley (email@matthewcrumley.com, http://silentmatt.com/)\nhttps://github.com/silentmatt/js-expression-eval\n\nPorted to Python and modified by Vera Mazhuga (ctrl-alt-delete@live.com, http://vero4ka.info/)\n\nYou are free to use and modify this code in anyway you find useful. Please leave this comment in the code\nto acknowledge its original source. If you feel like it, I enjoy hearing about projects that use my code,\nbut don't feel like you have to let me know or ask permission.\n\n## Installation\n\n pip install py_expression_eval\n\n## Tests\n\n python setup.py test\n\n## Documentation\n\nAll the classes and methods of ``py-expression-eval`` were written as similar as possible to their analogues from [js-expression-eval](https://github.com/silentmatt/js-expression-eval) to make it easier to use for validation on back-end side.\n\n### Parser\n\n\n``Parser`` is 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:\n\n```python\n> from py_expression_eval import Parser\n> parser = Parser()\n```\n\nOnce you instantiated ``Parser`` class, you can create ``Expression`` object using ``parse`` method:\n\n```python\n> parser.parse('2 * 3')\nOut: \n```\n\n### Parser.Expression\n\n``evaluate()`` takes a dictionary with variables as a parameter and returns the value of the expression:\n\n```python\n> parser.parse('2 * 3').evaluate({})\nOut: 6\n> parser.parse('2 * 3.0').evaluate({})\nOut: 6.0\n> parser.parse('2 * x').evaluate({'x': 7})\nOut: 14\n> parser.parse('2 * x').evaluate({'x': 7.0})\nOut: 14.0\n```\n\n``substitute()`` creates a new expression where specified variables are replaces with a new expression. For example, to replace ``x`` with ``3 + x`` in ``2 * x`` expression we use the following code:\n\n```python\n> parser.parse('2 * x').substitute('x', '3 + x').toString()\nOut: '(2*(3+x))'\n```\n\n``variables()`` returns a list of the variables for the expression:\n\n```python\n> parser.parse('2 * x + y').variables()\nOut: ['x', 'y']\n```\n\n``simplify()`` simplifies the expression. For example,\n\n```python\n> parser.parse('2 * 3 * x + y').simplify({}).toString()\nOut: '((6*x)+y)'\n> parser.parse('2 * 3 * x + y').simplify({'x': -1}).toString()\nOut: '(-6+y)'\n> parser.parse('cos(PI) + x').simplify({}).toString()\nOut: '(-1.0+x)'\n```\n\n``toString()`` converts the expression to a string.\n\n### Available operators, constants and functions\n\n| Expression | Example | Output\n| ---------- | ------- | ------ \n| + | ``parser.parse('2 + 2').evaluate({})`` | 4\n| - | ``parser.parse('3 - 1').evaluate({})`` | 2\n| `*` | ``parser.parse('2 * 3').evaluate({})`` | 6\n| / | ``parser.parse('5 / 2').evaluate({})`` | 2.5\n| % | ``parser.parse('5 % 2').evaluate({})`` | 1\n| ^ | ``parser.parse('5 ^ 2').evaluate({})`` | 25.0\n| PI | ``parser.parse('PI').evaluate({})`` | 3.141592653589793\n| E | ``parser.parse('E').evaluate({})`` | 2.718281828459045\n| sin(x) | ``parser.parse('sin(0)').evaluate({})`` | 0.0\n| cos(x) | ``parser.parse('cos(PI)').evaluate({})`` | - 1.0\n| tan(x) | ``parser.parse('tan(0)').evaluate({})`` | 0.0\n| asin(x) | ``parser.parse('asin(0)').evaluate({})`` | 0.0\n| acos(x) | ``parser.parse('acos(-1)').evaluate({})`` | 3.141592653589793\n| atan(x) | ``parser.parse('atan(PI)').evaluate({})`` | 1.2626272556789118\n| log(x) | ``parser.parse('log(1)').evaluate({})`` | 0.0\n| log(x, base) | ``parser.parse('log(16, 2)').evaluate({})`` | 4.0\n| abs(x) | ``parser.parse('abs(-1)').evaluate({})`` | 1\n| ceil(x) | ``parser.parse('ceil(2.7)').evaluate({})`` | 3.0\n| floor(x) | ``parser.parse('floor(2.7)').evaluate({})`` | 2.0\n| round(x) | ``parser.parse('round(2.7)').evaluate({})`` | 3.0\n| exp(x) | ``parser.parse('exp(2)').evaluate({})`` | 7.38905609893065\n\n## Examples\n\n```python\nfrom py_expression_eval import Parser\n\nparser = Parser()\nparser.parse('2 * 3').evaluate({}) # 6\nparser.parse('2 ^ x').evaluate({'x': 3}) # 8.0\nparser.parse('2 * x + 1').evaluate({'x': 3}) # 7\nparser.parse('2 + 3 * x').evaluate({'x': 4}) # 14\nparser.parse('(2 + 3) * x').evaluate({'x': 4}) # 20\nparser.parse('2-3^x').evaluate({'x': 4}) # -79.0\nparser.parse('-2-3^x').evaluate({'x': 4}) # -83.0\nparser.parse('-3^x').evaluate({'x': 4}) # -81.0\nparser.parse('(-3)^x').evaluate({'x': 4}) # 81.0\nparser.parse('2*x + y').evaluate({'x': 4, 'y': 1}) # 9\nparser.parse('round(log(2.7))').evaluate({}) # 1.0\n\n# substitute\nexpr = parser.parse('2 * x + 1')\nexpr2 = expr.substitute('x', '4 * x') # ((2*(4*x))+1)\nexpr2.evaluate({'x': 3}) # 25\n\n# simplify\nexpr = parser.parse('x * (y * atan(1))').simplify({'y': 4})\nexpr.toString() # x*3.141592\nexpr.evaluate({'x': 2}) # 6.283185307179586\n\n# get variables\nexpr = parser.parse('x * (y * atan(1))')\nexpr.variables() # ['x', 'y']\nexpr.simplify({'y': 4}).variables() #\u00a0['x']\n```\n\nAvailable operations\n--------------------\n\n```python\nfrom py_expression_eval import Parser\n\nparser = Parser()\nparser.parse('2 + 3').evaluate({}) # 5\nparser.parse('2 - 3').evaluate({}) # -1\nparser.parse('2 * 3').evaluate({}) # 6\nparser.parse('2 / 3').evaluate({}) # 0.6666666666666666\nparser.parse('2 % 3').evaluate({}) # 2\nparser.parse('-2').evaluate({}) # -2\nparser.parse('abs(-2)').evaluate({}) # 2\n\nparser.parse('ceil(1.4)').evaluate({}) # 2.0\nparser.parse('floor(1.4)').evaluate({}) # 1.0\nparser.parse('round(1.4)').evaluate({}) # 1.0\n\nparser.parse('2^3').evaluate({}) # 8.0\nparser.parse('sqrt(16)').evaluate({}) # 4.0\n\nparser.parse('sin(3.14)').evaluate({}) # 0.0015926529164868282\nparser.parse('cos(3.14)').evaluate({}) # -0.9999987317275395\nparser.parse('tan(3.14)').evaluate({}) # -0.0015926549364072232\n\nparser.parse('asin(1)').evaluate({}) # 1.5707963267948966\nparser.parse('acos(1)').evaluate({}) # 0.0\nparser.parse('atan(1)').evaluate({}) # 0.7853981633974483\n\nparser.parse('log(2.7)').evaluate({}) # 0.9932517730102834\nparser.parse('exp(1)').evaluate({}) # 2.718281828459045\n\nparser.parse('log(E)').evaluate({}) # 1.0\nparser.parse('cos(PI)').evaluate({}) # -1.0\n\nparser.parse('x||y').evaluate({'x': 2, 'y': 3}) # '23'\n```\n\n## Upload package to PyPi\n\n\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/AxiaCore/py-expression-eval/", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "py-expression-eval", "package_url": "https://pypi.org/project/py-expression-eval/", "platform": "", "project_url": "https://pypi.org/project/py-expression-eval/", "project_urls": { "Homepage": "https://github.com/AxiaCore/py-expression-eval/" }, "release_url": "https://pypi.org/project/py-expression-eval/0.3.9/", "requires_dist": null, "requires_python": "", "summary": "Python Mathematical Expression Evaluator", "version": "0.3.9" }, "last_serial": 5547474, "releases": { "0.1": [ { "comment_text": "", "digests": { "md5": "8184dd2027952146e65e3c24558a524a", "sha256": "938146a6d8aac7d3e85505e342c87c046dd1462375268fc91d1a58ed50a15ca9" }, "downloads": -1, "filename": "py_expression_eval-0.1.tar.gz", "has_sig": false, "md5_digest": "8184dd2027952146e65e3c24558a524a", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5564, "upload_time": "2014-05-20T13:09:59", "url": "https://files.pythonhosted.org/packages/8b/b9/7a93cb2658811c32d16b83684a9373cdf700307af35d86bc6853641c2e05/py_expression_eval-0.1.tar.gz" } ], "0.2": [ { "comment_text": "", "digests": { "md5": "650d8bca139db2f8e42eda6b3a55ad0b", "sha256": "250ca2f0dddf21cb2e08defa228d739298ffdd70abeb282ada80e281a3164b10" }, "downloads": -1, "filename": "py_expression_eval-0.2.tar.gz", "has_sig": false, "md5_digest": "650d8bca139db2f8e42eda6b3a55ad0b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5559, "upload_time": "2014-09-23T19:20:49", "url": "https://files.pythonhosted.org/packages/5a/ab/9da0421559a0a7ba9bc4fa10eba2ddee7217174f303507da9eef925a5d97/py_expression_eval-0.2.tar.gz" } ], "0.3": [ { "comment_text": "", "digests": { "md5": "cd354f2e30b8d5c9dd5c7c8e9b813042", "sha256": "c4dbba24dfb7a45efff567bb5206a2feaf1cd5859c2f55f5650175199fb589a9" }, "downloads": -1, "filename": "py_expression_eval-0.3.tar.gz", "has_sig": false, "md5_digest": "cd354f2e30b8d5c9dd5c7c8e9b813042", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5564, "upload_time": "2014-10-27T14:30:05", "url": "https://files.pythonhosted.org/packages/9d/77/71d183ef09fa01800bebc1548fde28297890ca132edeea124e2fabe2f2b5/py_expression_eval-0.3.tar.gz" } ], "0.3.1": [], "0.3.2": [ { "comment_text": "", "digests": { "md5": "614eab9c09bb5590922f6b949b39b3e9", "sha256": "a4015ea14e1f1a8a5a07cb3e0afe21f4b35c874a5ba7442d8df4bf660314c150" }, "downloads": -1, "filename": "py_expression_eval-0.3.2.tar.gz", "has_sig": false, "md5_digest": "614eab9c09bb5590922f6b949b39b3e9", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6059, "upload_time": "2016-05-24T18:09:18", "url": "https://files.pythonhosted.org/packages/4a/19/56532caeb639f2f0b8efe22d1e9dd9a34e9d271400b8ba8a3ad08a0faae5/py_expression_eval-0.3.2.tar.gz" } ], "0.3.3": [ { "comment_text": "", "digests": { "md5": "589e18d7d1eb6dc9a2f76b2854f3d672", "sha256": "e084664cd8a7ae3b6b4548bcd65b26b886f50cfc7b46afe7ff1d2f396ee81451" }, "downloads": -1, "filename": "py_expression_eval-0.3.3.tar.gz", "has_sig": false, "md5_digest": "589e18d7d1eb6dc9a2f76b2854f3d672", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6781, "upload_time": "2016-08-19T13:55:59", "url": "https://files.pythonhosted.org/packages/d8/74/3e20f93fa9fd98d5209f37871a202ea36ccde84c2d5aa6770a393b54feb7/py_expression_eval-0.3.3.tar.gz" } ], "0.3.4": [ { "comment_text": "", "digests": { "md5": "ca186734545a70010484d011bc5cc2e6", "sha256": "b9662c58f8835f6fa3380990f870429fe1176008718a47ce054a7867c4091ad8" }, "downloads": -1, "filename": "py_expression_eval-0.3.4.tar.gz", "has_sig": false, "md5_digest": "ca186734545a70010484d011bc5cc2e6", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6840, "upload_time": "2016-09-06T15:00:28", "url": "https://files.pythonhosted.org/packages/af/12/5e71b0d075aaf727650c4c2d10bb98d9c243bf1de85dc61df0c7a2628707/py_expression_eval-0.3.4.tar.gz" } ], "0.3.5": [ { "comment_text": "", "digests": { "md5": "ea6c6a529f61d89f1ed52a11f200203e", "sha256": "cf5b475921fcb865f1f54b012b0a1ab5c578d0cd25aa61b3c31077e8addc82a6" }, "downloads": -1, "filename": "py_expression_eval-0.3.5-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "ea6c6a529f61d89f1ed52a11f200203e", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 8132, "upload_time": "2018-09-20T22:38:41", "url": "https://files.pythonhosted.org/packages/3d/71/c8566568df5ff9db77cd97d507d8a61597acf639f0cf0534681602a40009/py_expression_eval-0.3.5-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "959d8d767895c0c02c8c31c864498140", "sha256": "f55dde461885ddacbf7b59c934d708fea197556fe1406adcf784b334aeaa510b" }, "downloads": -1, "filename": "py_expression_eval-0.3.5-py3-none-any.whl", "has_sig": false, "md5_digest": "959d8d767895c0c02c8c31c864498140", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 8126, "upload_time": "2018-09-20T22:44:11", "url": "https://files.pythonhosted.org/packages/93/1a/18d4cf7271c9d4d4f308ddfbf3ece19b831394cc8a88bad595a40d54ca3f/py_expression_eval-0.3.5-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "bd3ffe333960f07932c9250409ace781", "sha256": "a405dff69ac96a0bd29f087605e5fc379ef50e99bdcd543f384d76ac4897bed1" }, "downloads": -1, "filename": "py_expression_eval-0.3.5.tar.gz", "has_sig": false, "md5_digest": "bd3ffe333960f07932c9250409ace781", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8566, "upload_time": "2018-09-20T22:44:12", "url": "https://files.pythonhosted.org/packages/3a/1e/41d16a3320c484dfa92ddcb905a61bf6dbcad3299144d02d608c398171bf/py_expression_eval-0.3.5.tar.gz" } ], "0.3.6": [ { "comment_text": "", "digests": { "md5": "45871dd5531386889f6ca8d9ca5d1cf5", "sha256": "dba4278c5068cbcd42cb5e5c3c813d820cc85cdd1e655fa99f76af73d1779dc3" }, "downloads": -1, "filename": "py_expression_eval-0.3.6-py3-none-any.whl", "has_sig": false, "md5_digest": "45871dd5531386889f6ca8d9ca5d1cf5", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 8211, "upload_time": "2019-01-18T19:26:07", "url": "https://files.pythonhosted.org/packages/72/30/c8e30ecfb469da8ea5badb26cabbe99c569c80990a188c0696d0e66dc21e/py_expression_eval-0.3.6-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "553c272d2c35417242059441046a7eb2", "sha256": "0907cef96a7c370b2a58ea2179f88e48f421f5a41821b32f7defb81ca4e5247e" }, "downloads": -1, "filename": "py_expression_eval-0.3.6.tar.gz", "has_sig": false, "md5_digest": "553c272d2c35417242059441046a7eb2", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8646, "upload_time": "2019-01-18T19:26:08", "url": "https://files.pythonhosted.org/packages/76/b9/46f5c3194dd431f6c445a67da1461417fc2557e8f2f1a218e6b12cc357de/py_expression_eval-0.3.6.tar.gz" } ], "0.3.7": [ { "comment_text": "", "digests": { "md5": "4e48ec3bfe6ebb9a6ab207a207b03454", "sha256": "e8663e4c603145014df142f0f9014787fab2586bf97a23c6f17d3744c84e64ea" }, "downloads": -1, "filename": "py_expression_eval-0.3.7.tar.gz", "has_sig": false, "md5_digest": "4e48ec3bfe6ebb9a6ab207a207b03454", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8973, "upload_time": "2019-05-06T15:19:06", "url": "https://files.pythonhosted.org/packages/d1/3e/bc5a9279747feb32667549293d88c82d26de3628d0c90df540de0bab9360/py_expression_eval-0.3.7.tar.gz" } ], "0.3.9": [ { "comment_text": "", "digests": { "md5": "5b15dfa910e98033b512531b0a89b776", "sha256": "0cb83733da955281044193f68fccabca5a34633c366e2fda93d584ebe711b588" }, "downloads": -1, "filename": "py_expression_eval-0.3.9-py3-none-any.whl", "has_sig": false, "md5_digest": "5b15dfa910e98033b512531b0a89b776", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 10787, "upload_time": "2019-07-17T19:33:42", "url": "https://files.pythonhosted.org/packages/44/fe/89d13fb89bf424c38040ceaa50ce076aa4661b54086cb3292fd550e0ce24/py_expression_eval-0.3.9-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "85ff4be7ffff258b9f160d8bd7715ec9", "sha256": "d80a948f91f78d08f789b0a7c3fb2bd9a34ad625f5ce88c262a6c91189a4abb9" }, "downloads": -1, "filename": "py_expression_eval-0.3.9.tar.gz", "has_sig": false, "md5_digest": "85ff4be7ffff258b9f160d8bd7715ec9", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11447, "upload_time": "2019-07-17T19:33:43", "url": "https://files.pythonhosted.org/packages/2c/e6/3e38dddde6e1b1ce361fcaec9c2f04cd60e76df7c0045e0376c49b1b283f/py_expression_eval-0.3.9.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "5b15dfa910e98033b512531b0a89b776", "sha256": "0cb83733da955281044193f68fccabca5a34633c366e2fda93d584ebe711b588" }, "downloads": -1, "filename": "py_expression_eval-0.3.9-py3-none-any.whl", "has_sig": false, "md5_digest": "5b15dfa910e98033b512531b0a89b776", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 10787, "upload_time": "2019-07-17T19:33:42", "url": "https://files.pythonhosted.org/packages/44/fe/89d13fb89bf424c38040ceaa50ce076aa4661b54086cb3292fd550e0ce24/py_expression_eval-0.3.9-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "85ff4be7ffff258b9f160d8bd7715ec9", "sha256": "d80a948f91f78d08f789b0a7c3fb2bd9a34ad625f5ce88c262a6c91189a4abb9" }, "downloads": -1, "filename": "py_expression_eval-0.3.9.tar.gz", "has_sig": false, "md5_digest": "85ff4be7ffff258b9f160d8bd7715ec9", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11447, "upload_time": "2019-07-17T19:33:43", "url": "https://files.pythonhosted.org/packages/2c/e6/3e38dddde6e1b1ce361fcaec9c2f04cd60e76df7c0045e0376c49b1b283f/py_expression_eval-0.3.9.tar.gz" } ] }