Skip to main content

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.

Metadata

Release files for kharazmi 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kharazmi 0.1.4
File Size Uploaded Provenance Actions
kharazmi-0.1.4.tar.gz 13.3 kB Without provenance Details for kharazmi-0.1.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for kharazmi 0.1.4
File Interpreter ABI Platform Actions
kharazmi-0.1.4-py3-none-any.whl Python 3 none any Details for kharazmi-0.1.4-py3-none-any.whl

Total release size: 24.9 kB

Release files / kharazmi-0.1.4.tar.gz

Download URL kharazmi-0.1.4.tar.gz
Size 13.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d43e7d2a036c3bc880a64760c50252bf6c4fc5e990ee3e4db624e8682b7dbb3e
BLAKE2b-256 checksum
How to use checksums
830d91c4af5c7283aad11dcb81f14e7698feffe2773305067b052cde6a4966f3
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / kharazmi-0.1.4-py3-none-any.whl

Download URL kharazmi-0.1.4-py3-none-any.whl
Size 11.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
503b559834765b8c51d373fe915c2ad930a84481ce794ff95a89780982fadf45
BLAKE2b-256 checksum
How to use checksums
3cb1988cb760f32d6684bf65a74d83ff13aa78c02f9ec8daf7d820b2f17c8948
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for kharazmi 0.1.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page