pydantic-monty
Python bindings for the Monty sandboxed Python interpreter.
Installation
pip install pydantic-monty
Usage
Basic Expression Evaluation
import pydantic_monty
# Simple code with no inputs
m = pydantic_monty.Monty('1 + 2')
print(m.run())
#> 3
Using Input Variables
import pydantic_monty
# Create with code that uses input variables
m = pydantic_monty.Monty('x * y', inputs=['x', 'y'])
# Run multiple times with different inputs
print(m.run(inputs={'x': 2, 'y': 3}))
#> 6
print(m.run(inputs={'x': 10, 'y': 5}))
#> 50
Resource Limits
import pydantic_monty
m = pydantic_monty.Monty('x + y', inputs=['x', 'y'])
# With resource limits
limits = pydantic_monty.ResourceLimits(max_duration_secs=1.0)
result = m.run(inputs={'x': 1, 'y': 2}, limits=limits)
assert result == 3
External Functions
import pydantic_monty
# Code that calls an external function
m = pydantic_monty.Monty('double(x)', inputs=['x'])
# Provide the external function implementation at runtime
result = m.run(inputs={'x': 5}, external_functions={'double': lambda x: x * 2})
print(result)
#> 10
Iterative Execution with External Functions
Use start() and resume() to handle external function calls iteratively,
giving you control over each call:
import pydantic_monty
code = """
data = fetch(url)
len(data)
"""
m = pydantic_monty.Monty(code, inputs=['url'])
# Start execution - pauses when fetch() is called
result = m.start(inputs={'url': 'https://example.com'})
print(type(result))
#> <class 'pydantic_monty.FunctionSnapshot'>
print(result.function_name) # fetch
#> fetch
print(result.args)
#> ('https://example.com',)
# Perform the actual fetch, then resume with the result
result = result.resume({'return_value': 'hello world'})
print(type(result))
#> <class 'pydantic_monty.MontyComplete'>
print(result.output)
#> 11
Serialization
Both Monty and FunctionSnapshot can be serialized to bytes and restored later.
This allows caching parsed code or suspending execution across process boundaries:
import pydantic_monty
# Serialize parsed code to avoid re-parsing
m = pydantic_monty.Monty('x + 1', inputs=['x'])
data = m.dump()
# Later, restore and run
m2 = pydantic_monty.Monty.load(data)
print(m2.run(inputs={'x': 41}))
#> 42
Execution state can also be serialized mid-flight:
import pydantic_monty
m = pydantic_monty.Monty('fetch(url)', inputs=['url'])
progress = m.start(inputs={'url': 'https://example.com'})
# Serialize the execution state
state = progress.dump()
# Later, restore and resume (e.g., in a different process)
progress2 = pydantic_monty.load_snapshot(state)
result = progress2.resume({'return_value': 'response data'})
print(result.output)
#> response data
Metadata
Release files for auctor-monty 0.0.18
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| auctor_monty-0.0.18.tar.gz | 1.2 MB | Without provenance | Details for auctor_monty-0.0.18.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| auctor_monty-0.0.18-cp314-cp314-macosx_11_0_arm64.whl | CPython 3.14 | CPython 3.14 | macOS 11.0+ ARM64 | Details for auctor_monty-0.0.18-cp314-cp314-macosx_11_0_arm64.whl |
Total release size: 9.2 MB
Release files / auctor_monty-0.0.18.tar.gz
| Download URL | auctor_monty-0.0.18.tar.gz |
|---|---|
| Size | 1.2 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8ab9ea39b231b87879358442310e0040d202d4aca44cff80c7aefd0545562750
|
|
BLAKE2b-256 checksum How to use checksums |
0f041d7d7345a80eb895d20db4d6d1c7086645a6283752b9ce84960cbb9a9500
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.13.3
|
Release files / auctor_monty-0.0.18-cp314-cp314-macosx_11_0_arm64.whl
| Download URL | auctor_monty-0.0.18-cp314-cp314-macosx_11_0_arm64.whl |
|---|---|
| Size | 8.0 MB |
| Tags | CPython 3.14 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
776c7aa22c84e9fe3e3f2545bf60e9034446b8eeef54fbdfd80768b1c50e3668
|
|
BLAKE2b-256 checksum How to use checksums |
9610b4b3ebaa41581b8a5f32f8be5b5715105c04b5cb19e20c711d4cfc2520ef
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.13.3
|
Security for auctor-monty 0.0.18
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.