{"id":8460,"library":"py-expression-eval","title":"Py-Expression-Eval","description":"Py-expression-eval is a lightweight Python library designed for evaluating mathematical and logical expressions from strings. It supports variables, custom functions, and a wide range of operators, making it suitable for dynamic calculation needs. The current version is 0.3.14. Releases are typically infrequent, focused on bug fixes and minor feature enhancements.","status":"active","version":"0.3.14","language":"en","source_language":"en","source_url":"https://github.com/AxiaCore/py-expression-eval/","tags":["expression evaluation","math","parser","calculator","mathematical expressions"],"install":[{"cmd":"pip install py-expression-eval","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"Parser","correct":"from py_expression_eval import Parser"}],"quickstart":{"code":"from py_expression_eval import Parser\n\n# Initialize the parser\nparser = Parser()\n\n# Evaluate a simple expression with variables\nexpression1 = \"2 * x + y\"\nvariables1 = {\"x\": 3, \"y\": 5}\nresult1 = parser.parse(expression1).evaluate(variables1)\nprint(f\"'{expression1}' with {variables1} evaluates to: {result1}\")\n\n# Evaluate an expression with built-in functions\nexpression2 = \"sqrt(16) + abs(-10)\"\nresult2 = parser.parse(expression2).evaluate()\nprint(f\"'{expression2}' evaluates to: {result2}\")\n\n# Evaluate an expression with a conditional operator (requires v0.3.10+ for 'in')\nexpression3 = \"x in [1, 2, 3] ? 'present' : 'absent'\"\nvariables3 = {\"x\": 2}\nresult3 = parser.parse(expression3).evaluate(variables3)\nprint(f\"'{expression3}' with {variables3} evaluates to: '{result3}'\")\n","lang":"python","description":"This quickstart demonstrates how to initialize the parser, evaluate expressions with variables, use built-in functions, and leverage conditional operators. The `parse()` method returns an expression object which then can be evaluated with an optional dictionary of variables."},"warnings":[{"fix":"Test your existing expressions thoroughly after upgrading to 0.3.14. If issues arise, consider custom operator overriding or adjusting expressions.","message":"In version 0.3.14, `math.pow` was swapped with `self.pow` internally. While usually backward compatible, custom implementations or reliance on specific `math.pow` behavior (e.g., for very large numbers or specific edge cases) might exhibit subtle differences.","severity":"breaking","affected_versions":"0.3.14 and later"},{"fix":"Upgrade to version 0.3.10 or newer to use these operators. Alternatively, implement custom functions or operators for older versions.","message":"New operators like `IN`, `NOT`, `XOR`, and conditional (`? :`) were added in version 0.3.10. Attempting to use these operators in versions prior to 0.3.10 will result in a `BadExpression: 'Undefined operator'` error.","severity":"gotcha","affected_versions":"<0.3.10"},{"fix":"Ensure you are on version 0.3.7 or newer for reliable `**` operator behavior. In older versions, use `pow(base, exponent)` if available, or stick to `^` (if defined differently in that version).","message":"Support for the `**` (power) operator was fixed and improved around version 0.3.7. Using `**` in much older versions might lead to incorrect parsing or evaluation, or it might not have been supported at all.","severity":"gotcha","affected_versions":"<0.3.7"},{"fix":"Upgrade to version 0.3.11 or newer for flexible string literal quoting. In older versions, ensure consistency with the default quote type (typically single quotes).","message":"String literal quote types became configurable from version 0.3.11. Prior to this, the parser might have been more restrictive (e.g., only allowing single quotes). Using double quotes in older versions could result in `BadExpression` errors.","severity":"gotcha","affected_versions":"<0.3.11"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Add `from py_expression_eval import Parser` at the top of your Python file.","cause":"The `Parser` class was not imported before use.","error":"NameError: name 'Parser' is not defined"},{"fix":"Upgrade your `py-expression-eval` library to version 0.3.10 or newer by running `pip install --upgrade py-expression-eval`.","cause":"You are attempting to use the `in` operator (or other new operators like `NOT`, `XOR`, `?:`) with an older version of `py-expression-eval` that does not support it.","error":"py_expression_eval.parser.BadExpression: 'Undefined operator: in'"},{"fix":"Review your expression string for typos, correct syntax, and ensure all operators and functions are valid for your `py-expression-eval` version. Use single quotes for string literals if you are on an older version (<0.3.11).","cause":"The expression string contains a syntax error, an unsupported character, or is malformed (e.g., unbalanced parentheses, unexpected operator placement).","error":"py_expression_eval.parser.BadExpression: 'Unexpected token: '"},{"fix":"Ensure you first parse the expression: `parsed_expression = parser.parse(expression_string)` then call `parsed_expression.evaluate(variables)`.","cause":"You are calling `.evaluate()` directly on the string expression instead of first parsing it. The `evaluate` method belongs to the expression object returned by `parser.parse()`.","error":"AttributeError: 'str' object has no attribute 'evaluate'"}]}