XPR expression language - Python runtime
Project description
xpr-python — XPR Expression Language for Python
XPR is a sandboxed cross-language expression language for data pipeline transforms. This is the Python runtime.
Install
pip install xpr-lang
Requires Python 3.10+.
Quick Start
from xpr import Xpr
engine = Xpr()
engine.evaluate('items.filter(x => x.price > 50).map(x => x.name)', {
'items': [
{'name': 'Widget', 'price': 25},
{'name': 'Gadget', 'price': 75},
{'name': 'Doohickey', 'price': 100},
]
})
# → ["Gadget", "Doohickey"]
API
evaluate(expression, context=None)
Evaluates an XPR expression against an optional context object.
from xpr import Xpr
engine = Xpr()
engine.evaluate('1 + 2') # → 3
engine.evaluate('user.name', {'user': {'name': 'Alice'}}) # → "Alice"
engine.evaluate('items.length', {'items': [1, 2, 3]}) # → 3
Returns the result as object. Raises XprError on parse or evaluation errors.
add_function(name, fn)
Register a custom function callable from expressions:
from xpr import Xpr
engine = Xpr()
engine.add_function('double', lambda x: x * 2)
engine.add_function('greet', lambda name: f'Hello, {name}!')
engine.evaluate('double(21)') # → 42
engine.evaluate('greet("World")') # → "Hello, World!"
engine.evaluate('items.map(x => double(x))', {'items': [1, 2, 3]}) # → [2, 4, 6]
Built-in Functions
Math: round, floor, ceil, abs, min, max
Type: type, int, float, string, bool
String methods: .len(), .upper(), .lower(), .trim(), .startsWith(), .endsWith(), .contains(), .split(), .replace(), .slice(), .indexOf(), .repeat(), .trimStart(), .trimEnd(), .charAt(), .padStart(), .padEnd()
Array methods: .map(), .filter(), .reduce(), .find(), .some(), .every(), .flatMap(), .sort(), .reverse(), .length, .includes(), .indexOf(), .slice(), .join(), .concat(), .flat(), .unique(), .zip(), .chunk(), .groupBy()
Object methods: .keys(), .values(), .entries(), .has()
Utility: range()
v0.2 Features
Let Bindings: Immutable scoped bindings allow you to define and reuse values within expressions:
engine.evaluate('let x = 1; let y = x + 1; y') # → 2
engine.evaluate('let items = [1, 2, 3]; items.map(x => x * 2)', {}) # → [2, 4, 6]
Spread Operator: Spread syntax for arrays and objects enables composition and merging:
engine.evaluate('[1, 2, ...[3, 4]]') # → [1, 2, 3, 4]
engine.evaluate('{...{a: 1}, b: 2}') # → {'a': 1, 'b': 2}
Conformance
This runtime supports Level 1–3 (all conformance levels):
- Level 1: Literals, arithmetic, comparison, logic, ternary, property access, function calls
- Level 2: Arrow functions, collection methods, string methods, template literals
- Level 3: Pipe operator (
|>), optional chaining (?.), nullish coalescing (??)
v0.2 additions: Let bindings, spread operator, 20 new built-in methods (10 array, 7 string, 2 object, 1 global)
Passes all conformance tests (Levels 1–3 plus v0.2 features).
Specification
See the XPR Language Specification for the full EBNF grammar, type system, operator precedence, and conformance test suite.
License
MIT
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file xpr_lang-0.2.1.tar.gz.
File metadata
- Download URL: xpr_lang-0.2.1.tar.gz
- Upload date:
- Size: 34.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
236e8443f0b356237361951559d5a84c8b7a0fc95781059abbde0006f95c7dfb
|
|
| MD5 |
bddee1c9386a92bf8fac4d2ee513fbb9
|
|
| BLAKE2b-256 |
049658e876012c7dc3585b689384e1cdab1013db3a4f6504324c598388104254
|
Provenance
The following attestation bundles were made for xpr_lang-0.2.1.tar.gz:
Publisher:
publish.yml on xpr-lang/xpr-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
xpr_lang-0.2.1.tar.gz -
Subject digest:
236e8443f0b356237361951559d5a84c8b7a0fc95781059abbde0006f95c7dfb - Sigstore transparency entry: 1102673904
- Sigstore integration time:
-
Permalink:
xpr-lang/xpr-python@1e938c7185a5e8f6003930cff9a10450e68db7d0 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/xpr-lang
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@1e938c7185a5e8f6003930cff9a10450e68db7d0 -
Trigger Event:
push
-
Statement type:
File details
Details for the file xpr_lang-0.2.1-py3-none-any.whl.
File metadata
- Download URL: xpr_lang-0.2.1-py3-none-any.whl
- Upload date:
- Size: 15.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
894b951d2d459952a99dc59978dbe2427c38c745b52120f9f34469ebd3f96a0b
|
|
| MD5 |
2716f3973e3c93b68f409b907d800c72
|
|
| BLAKE2b-256 |
0fff6a9f058f64c8940bea94ee72f3ba19a5e0cd34d608f6f6bf7f90a70885e6
|
Provenance
The following attestation bundles were made for xpr_lang-0.2.1-py3-none-any.whl:
Publisher:
publish.yml on xpr-lang/xpr-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
xpr_lang-0.2.1-py3-none-any.whl -
Subject digest:
894b951d2d459952a99dc59978dbe2427c38c745b52120f9f34469ebd3f96a0b - Sigstore transparency entry: 1102673955
- Sigstore integration time:
-
Permalink:
xpr-lang/xpr-python@1e938c7185a5e8f6003930cff9a10450e68db7d0 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/xpr-lang
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@1e938c7185a5e8f6003930cff9a10450e68db7d0 -
Trigger Event:
push
-
Statement type: