Skip to main content

Simple formula evaluator.

Project description

Formula

Simple formula-like expression solver.

Parses formula into a reverse polish notation. Evaluates parsed to get result.

Installation

pip install px-formula

Usage

Uou can calculate formulas with different operators, and function calls. But it's not a python code, that executes - only simple maths expressions.

from px_formula import Formula, defaults


formula = Formula(
  # Can change operators:
  operators=defaults.operators,
  # And available functions:
  functions=defaults.functions,
)

parsed = formula.parse('2/8+45*6+var1')
# > ['2', '8', '/', '45', '6', '*', '+', 'var1', '+']

# You can evaluate any formula you have/stored easily:
formula.evaluate(parsed, {'var1': 5})
# > 275.25

# Or in one shot:
formula('2/8+abs(45*6)+var1', {'var1': 5})
# > 275.25

Supported

Operators:

  • ~ - Inverse
  • ! - Not
  • ** - Pow
  • * - Multiply
  • / - Divide
  • // - Floor divide
  • % - Modulo
  • + - Plus
  • - - Minus
  • | - Bitwise Or
  • & - Bitwise And
  • ^ - Bitwise Xor
  • <= - LTE
  • >= - GTE
  • == - Equals
  • < - LT
  • > - GT

Functions:

  • abs - operator.abs
  • sin - math.sin
  • cos - math.cos
  • tan - math.tan
  • exp - math.exp
  • abs - abs
  • trunc - Removes decimal part
  • round - round
  • hypot - math.hypot
  • all - all(True, False, True) -> False
  • any - any(True, False, False) -> True

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

[Unreleased]

[0.1.0]

Fixed

  • Issue: Stored stack turns empty after first evaluation.

[0.1.0]

Initial version.

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

px-formula-0.1.1.tar.gz (6.6 kB view details)

Uploaded Source

File details

Details for the file px-formula-0.1.1.tar.gz.

File metadata

  • Download URL: px-formula-0.1.1.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.12

File hashes

Hashes for px-formula-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e4ee301d173107a329c88bfb154dee04fdd2194937df5f4367ec72a8322d50c0
MD5 23d9001a78a8f5dfd222d8b8f4f0ff0d
BLAKE2b-256 916ce937ed502a2b8fddf216012e6bf84724f35383d2b70823badeb8a3986aa7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page