{"id":25932352,"url":"https://github.com/b-kiiskila/py_evalexpr","last_synced_at":"2025-03-04T00:37:18.031Z","repository":{"id":280221330,"uuid":"941329011","full_name":"b-kiiskila/py_evalexpr","owner":"b-kiiskila","description":"A Python extension module written in Rust that provides bindings to the evalexpr crate, enabling quick and safe expression evaluation in Python.","archived":false,"fork":false,"pushed_at":"2025-03-02T04:14:06.000Z","size":50,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-02T04:25:07.310Z","etag":null,"topics":["evalexpr","expression-evaluation","logical-expressions","mathematical-expressions","maturin","pyo3","python","python-bindings","rust","rust-extension"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/b-kiiskila.png","metadata":{"files":{"readme":"README.MD","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-02T02:59:42.000Z","updated_at":"2025-03-02T04:14:06.000Z","dependencies_parsed_at":"2025-03-02T04:35:12.200Z","dependency_job_id":null,"html_url":"https://github.com/b-kiiskila/py_evalexpr","commit_stats":null,"previous_names":["b-kiiskila/py_evalexpr"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/b-kiiskila%2Fpy_evalexpr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/b-kiiskila%2Fpy_evalexpr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/b-kiiskila%2Fpy_evalexpr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/b-kiiskila%2Fpy_evalexpr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/b-kiiskila","download_url":"https://codeload.github.com/b-kiiskila/py_evalexpr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241465049,"owners_count":19967240,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["evalexpr","expression-evaluation","logical-expressions","mathematical-expressions","maturin","pyo3","python","python-bindings","rust","rust-extension"],"created_at":"2025-03-04T00:36:55.205Z","updated_at":"2025-03-04T00:37:18.009Z","avatar_url":"https://github.com/b-kiiskila.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# py_evalexpr\n\n[![PyPI version](https://img.shields.io/pypi/v/py-evalexpr.svg)](https://pypi.org/project/py-evalexpr/)\n[![CI](https://github.com/b-kiiskila/py_evalexpr/actions/workflows/maturin-ci.yml/badge.svg)](https://github.com/b-kiiskila/py_evalexpr/actions/workflows/maturin-ci.yml)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nA Python extension module written in Rust that provides bindings to the [`evalexpr`](https://crates.io/crates/evalexpr) crate for expression evaluation.\n\n## 🌟 Features\n\n- 🧮 Evaluate mathematical and logical expressions from Python\n- 🔒 Three evaluation contexts:\n  - **StatelessContext**: Simple expressions without variables\n  - **ImmutableContext**: Expressions with predefined variables\n  - **MutableContext**: Expressions that can update variables\n- 📊 Type-specific evaluation methods for:\n  - Integers\n  - Floats\n  - Strings\n  - Booleans\n  - Tuples\n- ⚡ High-performance expression evaluation\n- 🚀 Built using Rust's `evalexpr` crate (version 12.0.2)\n- 🐍 Python bindings via `PyO3`\n\n## ⚡ Performance Benchmarks\n\nPyEvalExpr was benchmarked against several popular Python expression evaluation libraries:\n\n| Test Case | py_evalexpr | sympy | asteval | simpleeval |\n|------------|------------|--------|---------|------------|\n| simple_arithmetic | 0.0047 ms | 0.2339 ms | 0.0116 ms | 0.0083 ms |\n| complex_arithmetic | 0.0066 ms | 0.2329 ms | 0.0186 ms | 0.0118 ms |\n| variables | 0.0046 ms | 0.1996 ms | 0.0126 ms | 0.0083 ms |\n| math_functions | 0.0077 ms | 0.2403 ms | 0.0230 ms | 0.0117 ms |\n| complex_expression | 0.0095 ms | 0.2268 ms | 0.0244 ms | 0.0141 ms |\n| boolean_logic | 0.0059 ms | N/A | 0.0159 ms | 0.0103 ms |\n\n*Benchmarks performed on AMD Ryzen 9 7950X with 32GB RAM running Python 3.11 using 50,000 iterations per test. Lower is better.*\n\n## 🔧 Prerequisites\n\n- Python 3.11 or higher\n- Rust toolchain (cargo, rustc)\n- Compatible C compiler\n\n## 📦 Installation\n\n```bash\npip install py-evalexpr\n```\n\n## 💡 Usage Examples\n\n### Quick Evaluation\n\n```python\nfrom py_evalexpr import evaluate, evaluate_int, evaluate_float\n\n# Basic arithmetic\nresult = evaluate(\"2 + 3 * 4\")         # Returns 14\nresult = evaluate(\"sqrt(16) + 2\")      # Returns 6.0\n\n# Type-specific evaluation\nint_val = evaluate_int(\"42\")           # Returns 42 as int\nfloat_val = evaluate_float(\"3.14159\")  # Returns 3.14159 as float\nbool_val = evaluate_boolean(\"5 \u003e 3\")   # Returns True\n```\n\n### Context Usage\n\n#### Stateless Context\n\n```python\nfrom py_evalexpr import StatelessContext\n\ncontext = StatelessContext()\nresult = context.evaluate(\"42\").value          # Integer: 42\nresult = context.evaluate(\"sin(0.5)\").value    # Uses built-in math functions\n```\n\n#### Immutable Context\n\n```python\nfrom py_evalexpr import ImmutableContext\n\ncontext = ImmutableContext()\ncontext.variables[\"x\"] = 10\ncontext.variables[\"y\"] = 20\n\nresult = context.evaluate(\"x + y\").value       # Returns 30\n```\n\n#### Mutable Context\n\n```python\nfrom py_evalexpr import MutableContext\n\ncontext = MutableContext()\ncontext.evaluate_empty(\"x = 10\")\ncontext.evaluate_empty(\"y = 20\")\ncontext.evaluate_empty(\"sum = x + y\")\nresult = context.evaluate(\"sum * 2\").value     # Returns 60\n```\n\n## 🛡️ Error Handling\n\n```python\nfrom py_evalexpr import evaluate\nfrom py_evalexpr.exceptions import EvaluationError\n\ntry:\n    result = evaluate(\"undefined_var + 5\")\nexcept EvaluationError as e:\n    print(f\"Evaluation error: {e}\")\n```\n\n## 🤝 Contributing\n\nContributions are welcome:\n- Open an issue to discuss proposed changes\n- Submit pull requests\n- Follow existing code style\n\n## 🙏 Acknowledgments\n\n- [evalexpr](https://crates.io/crates/evalexpr) - Rust crate that powers this project\n- [PyO3](https://pyo3.rs) - Python extensions framework\n- [maturin](https://maturin.rs) - Python packaging tool\n- [devbox](https://www.jetify.com/devbox) - Predictable development environments\n- [uv](https://github.com/astral-sh/uv) - Python package installer\n\n---\nMade in 🇨🇦🍁 by [Benjamin Kiiskila](https://github.com/b-kiiskila)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fb-kiiskila%2Fpy_evalexpr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fb-kiiskila%2Fpy_evalexpr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fb-kiiskila%2Fpy_evalexpr/lists"}