An equation parser/calculator module for python.
Project description
Kharazmi
Kharazmi, persian pronunciation of Khwarizmi (al-Khwarizmi, Algorithmi), is an equation parser/calculator module for python.
It's main purpose is to provide an easy method to get equations from user and run them on your own data without using
eval
and having the freedom of using it on different data types (number, numpy arrays, etc.)
Currently it supports:
- Basic math operations (+-*/^)
- Numeric Variables (int, float, complex)
- Plugable functions
Installation
Simply install using pip:
pip install kharazmi
Keep in mind that Kharazmi uses Python type annotations (PEP 484), and f-Strings (PEP 498) so you'll need Python 3.6 or higher.
Usage
Basic Usage
Kharazmi provides a simple yet powerful API. First of all you have EquationParser
, the core of the Kharazmi.
It's the main class that you need to worry about. Simply instantiate it and pass your user's input to its parse
method:
from kharazmi import EquationParser
parser = EquationParser()
user_input = "2*x / (2 - y)"
expression = parser.parse(user_input)
All expressions are subclass of kharazmi.models.BaseNumericalExpression
class. You can work with them as if they were python
variables containing integers, e.g:
expression1 = parser.parse("3*x - y")
expression2 = parser.parse("z")
expression3 = expression1 / expression2
You can even build your expressions using normal python code:
x = parser.parse("x")
expression4 = 2*x + 4
BaseNumericalExpression
class provides a evaluate
method. It allows you to evaluate value of an expression given the values
you provides for variables:
expression5 = parser.parse("2*x + 4")
expression5.evaluate(x=2)
Because Kharazmi
uses python mathematical operators (+-*/^) under the hood, you can pass any value that supports these
operations and still get the correct result, e.g: you can pass numpy arrays:
import numpy as np
expression6 = parser.parse("2*x + y")
x = np.ones((2,2))
y = np.identity(2)
expression6.evaluate(x=x, y=y)
Finally, if you have an expression which you don't know its variables, you can get a list of them using variables
property of the expression.
Using functions
What if you want to create a more complex expressions, like sin(x)^2 + cos(x)^2
.
Well, first of all, you'll have to tell kharazmi
the list of your functions:
from math import sin, cos
from kharazmi import register_function
register_function("sin", sin)
register_function("cos", cos)
expression7 = parser.parse("sin(x)^2 + cos(x)^2")
You can even have functions with multiple inputs:
register_function("min", min)
expression7 = parser.parse("1 + min(x, y)")
If you want to use python's builtin math function, you can use kharazmi.activate_builtin_math
. Calling this function
will register all builtin functions from math
, along with min
, max
, round
, and abs
.
from kharazmi import activate_builtin_math
activate_builtin_math()
expression8 = parser.parse("log2(8)")
Using as a module
You can run kharazmi
as a module using python -m kharazmi
, this will run a REPL like program that lets you enter
expressions, see how you can do the same thing in code, in other words how kharazmi
is understanding your input, and
evaluate it.
It supports all math function either.
It's mostly for debugging and testing purposes, but it's there if you want to understand how kharazmi
is working,
I suggest start from there.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file kharazmi-0.1.4.tar.gz
.
File metadata
- Download URL: kharazmi-0.1.4.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d43e7d2a036c3bc880a64760c50252bf6c4fc5e990ee3e4db624e8682b7dbb3e |
|
MD5 | 87e1015d4ddc3c3bae61d24bbfd75e06 |
|
BLAKE2b-256 | 830d91c4af5c7283aad11dcb81f14e7698feffe2773305067b052cde6a4966f3 |
File details
Details for the file kharazmi-0.1.4-py3-none-any.whl
.
File metadata
- Download URL: kharazmi-0.1.4-py3-none-any.whl
- Upload date:
- Size: 11.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 503b559834765b8c51d373fe915c2ad930a84481ce794ff95a89780982fadf45 |
|
MD5 | 0323143c778fffce57cb24a7451791c2 |
|
BLAKE2b-256 | 3cb1988cb760f32d6684bf65a74d83ff13aa78c02f9ec8daf7d820b2f17c8948 |