Skip to main content

Mahonia

Mahonia is a domain specific language (DSL) for defining, evaluating, saving, and serializing binary expressions within a Python interpreter.

Test codecov Publish Python 🐍 distribution 📦 to PyPI

Motivation

Say that you are writing an application that conducts some assembly line testing at a manufacturing facility. While the primary requirement is to flag those units that do not meet expectations, a secondary requirement is to record what, why, and how a test has failed.

First, define what is being measured - the "context".

from typing import NamedTuple

from mahonia import Approximately, PlusMinus, Predicate, Var

class Measurements(NamedTuple):
	voltage: float

Next, for each "variable" of the context, we declare a matching Mahonia Var type.

voltage = Var[float, Measurements]("voltage")

Now we can write an expression. This expression defines a named predicate that will evaluate to True if the evaluated voltage is within 0.05 of 5.0.

expr = Predicate(
	"Voltage OK",
	Approximately(
		voltage, PlusMinus("Nominal", 5.0, plus_minus=0.05)
	)
)

Then we'll take the measurement and bind it:

from my_app import get_voltage

voltage_check = expr.bind(MyContext(voltage=get_voltage()))

This creates an immutable expression that Mahonia calls a BoundExpr. We can evaluate it as many times as we like:

voltage_check.unwrap() # True
voltage_check.unwrap() # True

We can inspect the evaluation context:

print(voltage_check.ctx) # Measurements(voltage=5.03)

As well as serialize it for the logs:

str(voltage_check) # or voltage_check.to_string()
# If it was success, for example:
# Voltage OK: True (voltage:5.03 ≈ Nominal:5.0 ± 0.05 -> True)
# Or a fail:
# Voltage OK: False (voltage:4.90 ≈ Nominal:5.0 ± 0.05 -> False)

LaTeX Support

Mahonia expressions can be converted to LaTeX mathematical notation:

from mahonia.latex import latex

# Convert expressions to LaTeX
latex(voltage)  # 'voltage'
latex(expr.expr)  # 'voltage \\approx Nominal \\pm 0.05'

For comprehensive examples of LaTeX output for all supported expression types, see tests/latex_examples.md.

Metadata

Release files for mahonia 0.5.0

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

Source distribution (sdist)

Source distribution for mahonia 0.5.0
File Size Uploaded Provenance Actions
mahonia-0.5.0.tar.gz 156.5 kB With provenance Details for mahonia-0.5.0.tar.gz

Built distribution (wheel)

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

Total release size: 193.0 kB

Release files / mahonia-0.5.0.tar.gz

Download URL mahonia-0.5.0.tar.gz
Size 156.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ab4883c2539f96e0f175ae598cb687b8839f8083c23f5978908583b07cfe325d
BLAKE2b-256 checksum
How to use checksums
086623d40881e42050b14c5896ed2fbd5e70fe97713da22e77c083a175901b46
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 2, 2026.

Transparency log

Release files / mahonia-0.5.0-py3-none-any.whl

Download URL mahonia-0.5.0-py3-none-any.whl
Size 36.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
94caf6ca80d89611c1b8bd7ce7b22ab8421e8d2d8f14328927c60653c795e060
BLAKE2b-256 checksum
How to use checksums
1af9382ff802ef6e808fd1e5f9922f17f1f5ed7f933b7dc255a1b06c2216a930
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 2, 2026.

Transparency log

Security for mahonia 0.5.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for mahonia-0.5.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 2, 2026.

Transparency log

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