Skip to main content

Fast Excel-formula tooling (Python bindings)

Project description

Formualizer — Python Bindings

A blazing‑fast Excel formula tokenizer, parser, and evaluator powered by Rust, exposed through a clean, Pythonic API. These bindings wrap the core formualizer‑core and formualizer‑eval crates and let you work with spreadsheet logic at native speed while writing idiomatic Python.


Key Features

Capability Description
Tokenization Breaks a formula string into structured Token objects, preserving exact byte spans and operator metadata.
Parsing → AST Produces a rich Abstract Syntax Tree (ASTNode) that normalises references, tracks source tokens, and fingerprints structure.
Reference Model First‑class CellRef, RangeRef, TableRef, NamedRangeRef objects with helpers like .normalise() / .to_excel().
Pretty‑printing Canonical formatter — returns Excel‑style string with consistent casing, spacing, and minimal parentheses.
Visitor utilities walk_ast, collect_references, collect_function_names, and more for ergonomic tree traversal.
Evaluation (opt‑in) Bring in formualizer‑eval to execute the AST with a pluggable workbook/resolver interface.
Rich Errors Typed TokenizerError / ParserError that annotate byte positions for precise diagnostics.

Installation

Pre‑built wheels (recommended)

pip install formualizer

Build from source

You need a recent Rust toolchain (≥ 1.70) and maturin:

# one‑off – install maturin
pip install maturin

# from repo root
cd bindings/python
maturin develop  # builds the native extension and installs an editable package

This compiles the Rust crates (formualizer‑*) into a CPython extension named formualizer.


Quick‑start

from formualizer import tokenize, parse
from formualizer.visitor import collect_references

formula = "=SUM(A1:B2) + 3%"

# 1️⃣ Tokenize
for tok in tokenize(formula):
    print(tok)

# 2️⃣ Parse → AST
ast = parse(formula)
print(ast.pretty())           # indented tree
print(ast.to_formula())       # canonical Excel string
print(ast.fingerprint())      # 64‑bit structural hash

# 3️⃣ Analyse
refs = collect_references(ast)
print([r.to_excel() for r in refs])  # ['A1:B2']

Tip: You can build your own visitor by returning VisitControl.SKIP or STOP to short‑circuit traversal.


Public API Surface

Convenience helpers

tokenize(formula: str) -> Tokenizer
parse(formula: str, include_whitespace: bool = False) -> ASTNode

Core classes (excerpt)

  • Tokenizer — iterable collection of Token; .render() reconstructs the original string.
  • Token.value, .token_type, .subtype, .start, .end, .is_operator().
  • Parser — OO interface when you need to parse the same Tokenizer twice.
  • ASTNode.pretty(), .to_formula(), .children(), .walk_refs()
  • Reference typesCellRef, RangeRef, TableRef, NamedRangeRef, UnknownRef.
  • ErrorsTokenizerError, ParserError (carry .message and .position).

Visitor helpers (formualizer.visitor)

  • walk_ast(node, fn) — DFS with early‑exit control.
  • collect_nodes_by_type(node, "Function") → list[ASTNode]
  • collect_references(node) → list[ReferenceLike]
  • collect_function_names(node) → list[str]

Workspace Layout

formualizer/
│
├─ crates/               # Pure‑Rust core, common types, evaluator, macros
│   ├─ formualizer-core      (tokenizer + parser + pretty)
│   ├─ formualizer-eval      (optional interpreter + built‑ins)
│   ├─ formualizer-common    (shared literal / error / arg specs)
│   └─ formualizer-macros    (proc‑macro helpers)
│
└─ bindings/python/      # This package (native module + Python helpers)

The Python wheel links directly against the crates — there is no runtime FFI overhead beyond the initial C→Rust boundary.


Development & Testing

# run Rust tests
cargo test --workspace

# TODO: add pytest once Python‑side tests exist

When hacking on the Rust side, you can rebuild the extension in place:

maturin develop --release  # faster extension; omit --release for debug builds

Roadmap

  • Full coverage of Excel 365 functions via formualizer‑eval
  • SIMD‑accelerated bulk range operations
  • ChatGPT‑powered formula explanations 🎯

Have an idea or found a bug? Open an issue or PR — contributions are welcome!


License

Dual‑licensed under MIT or Apache‑2.0 — choose whichever you prefer.

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

formualizer-0.1.0.tar.gz (54.4 kB view details)

Uploaded Source

Built Distribution

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

formualizer-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

File details

Details for the file formualizer-0.1.0.tar.gz.

File metadata

  • Download URL: formualizer-0.1.0.tar.gz
  • Upload date:
  • Size: 54.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.8.3

File hashes

Hashes for formualizer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7f890d9d10bc6bed140505cf4d8fc1a79c063e7e0c27b801629a1182ccc7a545
MD5 2555506b3df2531a22c78b75d97d13ff
BLAKE2b-256 254240524fad7ff5ffc2d6b18d0ddc80d40731f2a21fe11ae3c440220236c89d

See more details on using hashes here.

File details

Details for the file formualizer-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for formualizer-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 c1cbcfe8e77927f2391567fd7d50e6d0ae55ea12adfd129bef0652962c32cac0
MD5 d166237398eaee846b97620fbf224114
BLAKE2b-256 f6a61dfe204c67f9d4e5b6851331c7d934e6cdda653c02eaa26ada8dff9cfc30

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