Skip to main content

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.

Metadata

Release files for px-formula 0.1.1

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

Source distribution (sdist)

Source distribution for px-formula 0.1.1
File Size Uploaded Provenance
px-formula-0.1.1.tar.gz 6.6 kB Without provenance Details

Release files / px-formula-0.1.1.tar.gz

Download URL px-formula-0.1.1.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e4ee301d173107a329c88bfb154dee04fdd2194937df5f4367ec72a8322d50c0
BLAKE2b-256 checksum
How to use checksums
916ce937ed502a2b8fddf216012e6bf84724f35383d2b70823badeb8a3986aa7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.12

Security for px-formula 0.1.1

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.

Release history Release notifications | RSS feed

This release

0.1.1 This release

1 release file No provenance

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