Skip to main content

Compile serialized math.js expression trees into fast, reusable Python callables.

Project description

mathjs-to-func

PyPI Ruff Black ty CI Ask DeepWiki

A tiny Python library that compiles serialized math.js expression trees into fast, reusable Python callables. The generated function respects dependency ordering, validates inputs, and mirrors a subset of math.js operators (+, -, *, /, ^, %, unary plus/minus) and functions (min, max, sum, ifnull).

Key Features

  • Execute without reparsing or repeatedly walking the JSON graph.
  • Detect dependency cycles and missing identifiers early.
  • Keep execution sandboxed by compiling a controlled Python AST.
  • Work well with scalars or NumPy arrays for vectorised workloads.

Installation

The project uses uv for dependency and virtualenv management. From the repository root:

uv add mathjs-to-func

An optional parse extra installs a JSON-to-math.js parser powered by Pydantic:

uv add mathjs-to-func --extra parse

Compiling A Function

from mathjs_to_func import build_evaluator

def main():
    mathjs_payload = {
        "expressions": {
            # z = (x + y) / 2
            "sum_xy": {
                "type": "OperatorNode",
                "fn": "add",
                "args": [
                    {"type": "SymbolNode", "name": "x"},
                    {"type": "SymbolNode", "name": "y"},
                ],
            },
            "mean": {
                "type": "OperatorNode",
                "fn": "divide",
                "args": [
                    {"type": "SymbolNode", "name": "sum_xy"},
                    {"type": "ConstantNode", "value": "2", "valueType": "number"},
                ],
            },
        },
        "inputs": ["x", "y"],
        "target": "mean",
    }

    evaluator = build_evaluator(**mathjs_payload, include_source=True)

    result = evaluator({"x": 10, "y": 6})
    print(result)  # -> 8.0

    # Introspection helpers
    print(evaluator.__mathjs_required_inputs__)     # ('x', 'y')
    print(evaluator.__mathjs_evaluation_order__)    # ('sum_xy', 'mean')
    print(evaluator.__mathjs_source__)              # Generated Python source

Parameters

build_evaluator accepts keyword parameters (or a single payload mapping containing the same keys):

Argument Type Description
expressions Mapping[str, Mapping[str, Any]] math.js AST JSON keyed by expression id. Each id becomes a local variable in the compiled function.
inputs Iterable[str] Whitelisted identifiers that may be supplied when the function is invoked.
target str Name of the expression whose computed value should be returned.
include_source bool (optional) Attach the generated Python source code as __mathjs_source__ on the returned callable.

The returned callable always expects a single mapping argument with the provided inputs. It returns the evaluated target value and may be reused across invocations.

Supported math.js nodes

Node Notes
ConstantNode numeric (number), boolean, or null literals
SymbolNode validated identifiers; must be alphanumeric/underscore, starting with a letter/underscore
OperatorNode add, subtract, multiply, divide, pow, mod, unary unaryPlus, unaryMinus
FunctionNode min, max, sum, ifnull
ParenthesisNode forwards to the wrapped expression
ArrayNode materialised to Python lists/NumPy arrays

Unknown node types, invalid identifiers, or disallowed functions raise InvalidNodeError during compilation.

Error handling

  • ExpressionError: base class for configuration mistakes.
  • MissingTargetError: requested target id does not exist.
  • UnknownIdentifierError: an expression references a symbol that is neither an input nor another expression.
  • CircularDependencyError: dependency graph contains a cycle.
  • InvalidNodeError: AST contains unsupported structures or invalid literals.
  • InputValidationError: the compiled function received inputs that are missing, unexpected, or not a mapping.

All exceptions provide enough context (expression name, offending identifier, cycle list, etc.) to surface descriptive UI errors.

Parsing math.js JSON

With the extra installed you can turn serialized math.js nodes into evaluator-ready mappings:

from mathjs_to_func import build_evaluator
from mathjs_to_func.parse import parse

expression = parse(
    """{
    "type": "OperatorNode",
    "fn": "add",
    "args": [
        {"type": "SymbolNode", "name": "x"},
        {"type": "ConstantNode", "value": "2", "valueType": "number"}
    ]
}"""
)

evaluator = build_evaluator(
    expressions={"total": expression},
    inputs=["x"],
    target="total",
)

result = evaluator({"x": 40})  # -> 42

All examples below assume commands are wrapped with uv run ... to execute inside the managed environment.

Implementation Notes

  1. AST translationMathJsAstBuilder walks the math.js JSON and emits Python ast.AST nodes. Identifiers are validated via a strict regex to prevent sneaky names like __import__.
  2. Dependency graph – A topological sorter (graphlib.TopologicalSorter) runs over expression references to produce a safe evaluation order while catching cycles and missing references upfront.
  3. Code generation – The generated function validates the provided scope, binds required inputs to local variables, evaluates expressions in order, and returns the target. Intermediate values are stored as local variables named after their expression id.
  4. Execution sandbox – The compiled module is executed with a tightly scoped globals dictionary: helper math functions, NumPy, and a few safe built-ins only. There is no ambient __builtins__ exposure.
  5. Helper functions – math.js functions map onto small Python helpers (_mj_min, _mj_max, _mj_sum, _mj_ifnull) that understand scalars and NumPy arrays.

Testing

Run the full suite (178 tests) with:

uv run pytest

The tests cover operator translation, helper semantics, dependency validation, error conditions, numpy-friendly behaviour, and public API ergonomics.

Project Structure

src/mathjs_to_func/
├── __init__.py          # build_evaluator public API and export list
├── ast_builder.py       # math.js JSON → Python AST translation
├── compiler.py          # dependency graph, code generation, compilation
├── errors.py            # structured exception hierarchy
├── helpers.py           # runtime helpers for min/max/sum/ifnull
└── py.typed             # PEP 561 marker for type-aware consumers

Additional documentation lives in docs/api_design.md, outlining the initial design considerations.

Limitations & Future Work

  • Only a subset of math.js functions/operators are implemented today.
  • Units, user-defined functions, and incremental recomputation are intentionally out of scope for this milestone.
  • Arrays are handled via NumPy; if you need bigints, complex numbers, or matrices, the helper layer will require extension.

Contributions and bug reports are welcome!

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

mathjs_to_func-0.3.0.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mathjs_to_func-0.3.0-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file mathjs_to_func-0.3.0.tar.gz.

File metadata

  • Download URL: mathjs_to_func-0.3.0.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.12 {"installer":{"name":"uv","version":"0.11.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for mathjs_to_func-0.3.0.tar.gz
Algorithm Hash digest
SHA256 9a3795602ec4129db86b156a9f84358a95fe325c12083eff85a1927d8464ca9f
MD5 5dc1a1880c82959fe4f7626bb229bb9a
BLAKE2b-256 5a5adaf440881450c142fa1932715b671bc2126c83122710ec8f5668ab5788d2

See more details on using hashes here.

File details

Details for the file mathjs_to_func-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: mathjs_to_func-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.12 {"installer":{"name":"uv","version":"0.11.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for mathjs_to_func-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ed372b5195ad137cf4f374e45576af5f553b8f2344db7f0f3c0d4edcdf3bce35
MD5 839c898f08e466f982f6758ccc61bb3c
BLAKE2b-256 ee168fbf7b93f5c32e8d111231128fa9482078ba42b8719863c2f09f857a885b

See more details on using hashes here.

Supported by

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