Skip to main content

molang

Tests PyPI Python Downloads Status Code style: black Issues Code style: black

Molang to Python Translator & interpreter written in pure Python.

Installation

Install the module with pip:

pip3 install molang

Update existing installation: pip3 install molang --upgrade

Links

Requirements

Name Description
pydantic Data validation using Python type hints

Features

  • Pure-Python Molang parser and AST representation
  • Interpreter: evaluate Molang expressions against a runtime context
  • Compiler / emitter: convert parsed AST back to Molang source
  • DSL convenience objects: query, variable, math, temp, array, etc.
  • Assignment support for dotted variables (e.g. variable.x = 1, temp.a.b = 2)
  • Full math.* function implementations and callable math functions (e.g. math.sqrt(4))
  • Function-call support in expressions and argument evaluation
  • CLI to run Molang files or execute inline Molang code

See the docs for more information.

Example

Parse / Compile / Eval

Parse, compile and evaluate with a context

import molang

expr = "math.sqrt(16) + variable.x"
node = molang.parse(expr)
print(molang.compile(expr))
ctx = molang.create_context()
ctx['variable']['x'] = 9
print(molang.eval(expr, ctx))  # prints 13.0

DSL usage

from molang.dsl import variable, math, query

variable.x = 4
print(variable.x)           # MolangExpr("variable.x = 4;") when used as assignment
print(math.sqrt(variable.x))

expr = query.block_state("minecraft:cardinal_direction") == "north"
print(expr) # MolangExpr("query.block_state('minecraft:cardinal_direction') == 'north'")

Assignment to dotted variables

import molang

ctx = molang.create_context()
molang.eval('variable.health = 20', ctx)
print(ctx['variable']['health'])  # 20.0

Function calls and math helpers

import molang

ctx = molang.create_context()
print(molang.eval('math.lerp(0, 10, 0.5)', ctx))  # 5.0

Pydantic Support

from pydantic import BaseModel
from molang.dsl import MolangExpr, query

class Test(BaseModel):
  condition: MolangExpr

x = Test(condition=query.block_state("facing") == "north")
print(x.model_dump()) # {"condition": "query.block_state('facing') == 'north'"}

Command-line interface

usage: molang [-h] [-c <cmd>] [-V] [<file>]

Run molang files

positional arguments:
  <file>                execute the molang code contained in a file.

options:
  -h, --help            show this help message and exit
  -c <cmd>              execute the molang code in cmd. cmd can be one or more statements separated by newlines, with significant leading whitespace as in normal module code.
  -V, --version         print the molang version number and exit.

Metadata

Release files for molang 1.0.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 molang 1.0.1
File Size Uploaded
molang-1.0.1.tar.gz 19.5 kB Details

Release files / molang-1.0.1.tar.gz

Download URL molang-1.0.1.tar.gz
Size 19.5 kB
Tags Source
SHA-256 checksum
How to use checksums
948ce2cb4298e0fae33e5542e08970537f56017599c7de650e8921cec1565224
BLAKE2b-256 checksum
How to use checksums
6a077100907c0c5e7dfdd89122dc00898055259307ded89dcdd954a38e3c4857
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

1.0.1 This release

1 release file

1.0.0

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

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