Skip to main content

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

Project description

Mahonia

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

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)

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

mahonia-0.1.5.tar.gz (40.5 kB view details)

Uploaded Source

Built Distribution

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

mahonia-0.1.5-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file mahonia-0.1.5.tar.gz.

File metadata

  • Download URL: mahonia-0.1.5.tar.gz
  • Upload date:
  • Size: 40.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mahonia-0.1.5.tar.gz
Algorithm Hash digest
SHA256 7cb78f81382f77118d8051b3a9b44c0d5ab19fa7467627aec61fe54ae9baf476
MD5 bc1fa5a69aacffd387c1bbcc466779e4
BLAKE2b-256 d3e9794905d1de14e7b28e8f86c609dae6d1b4b35391b5f00fa0b6fbbfcfbc80

See more details on using hashes here.

Provenance

The following attestation bundles were made for mahonia-0.1.5.tar.gz:

Publisher: publish.yaml on JPHutchins/mahonia

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mahonia-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: mahonia-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mahonia-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 8cfc9886cc515f95a41e370fd7871faa0ad3205e5d429cd81b668b13840dff35
MD5 b741207e3b1baabd04f8a1a8d4de4e36
BLAKE2b-256 dfbc6eeb1a7b669ce2bccc919a5f90e8a703a68ca56486ba6c2e874d373e9a2f

See more details on using hashes here.

Provenance

The following attestation bundles were made for mahonia-0.1.5-py3-none-any.whl:

Publisher: publish.yaml on JPHutchins/mahonia

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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