Skip to main content

Python bindings for the Ouros sandboxed Python interpreter

Project description

ouros

Python bindings for the Ouros sandboxed Python interpreter.

Installation

pip install ouros

Usage

Basic Expression Evaluation

import ouros

# Simple code with no inputs
m = ouros.Sandbox('1 + 2')
print(m.run())
#> 3

Using Input Variables

import ouros

# Create with code that uses input variables
m = ouros.Sandbox('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 ouros

m = ouros.Sandbox('x + y', inputs=['x', 'y'])

# With resource limits
limits = ouros.ResourceLimits(max_duration_secs=1.0)
result = m.run(inputs={'x': 1, 'y': 2}, limits=limits)
assert result == 3

External Functions

import ouros

# Code that calls an external function
m = ouros.Sandbox('double(x)', inputs=['x'], external_functions=['double'])

# 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 ouros

code = """
data = fetch(url)
len(data)
"""

m = ouros.Sandbox(code, inputs=['url'], external_functions=['fetch'])

# Start execution - pauses when fetch() is called
result = m.start(inputs={'url': 'https://example.com'})

print(type(result))
#> <class 'ouros.Snapshot'>
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 'ouros.Complete'>
print(result.output)
#> 11

Serialization

Both Sandbox and Snapshot can be serialized to bytes and restored later. This allows caching parsed code or suspending execution across process boundaries:

import ouros

# Serialize parsed code to avoid re-parsing
m = ouros.Sandbox('x + 1', inputs=['x'])
data = m.dump()

# Later, restore and run
m2 = ouros.Sandbox.load(data)
print(m2.run(inputs={'x': 41}))
#> 42

Execution state can also be serialized mid-flight:

import ouros

m = ouros.Sandbox('fetch(url)', inputs=['url'], external_functions=['fetch'])
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 = ouros.Snapshot.load(state)
result = progress2.resume(return_value='response data')
print(result.output)
#> response data

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

ouros-0.0.6.tar.gz (2.0 MB view details)

Uploaded Source

Built Distributions

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

ouros-0.0.6-cp312-cp312-win_amd64.whl (14.9 MB view details)

Uploaded CPython 3.12Windows x86-64

ouros-0.0.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (13.9 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

ouros-0.0.6-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (12.8 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

ouros-0.0.6-cp312-cp312-macosx_11_0_arm64.whl (12.6 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

ouros-0.0.6-cp312-cp312-macosx_10_12_x86_64.whl (13.6 MB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

File details

Details for the file ouros-0.0.6.tar.gz.

File metadata

  • Download URL: ouros-0.0.6.tar.gz
  • Upload date:
  • Size: 2.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ouros-0.0.6.tar.gz
Algorithm Hash digest
SHA256 de66c6c9ecc19cedb1dd93163d1fa1890afe32c06414b2d1fbbf8dab0a2f39ed
MD5 65d7a6f347ffb270aa32f0ce9a389f32
BLAKE2b-256 213c8f54198da5f42265620cc50fb59fa2fce58cba84da34773cd7054fa4a1f4

See more details on using hashes here.

File details

Details for the file ouros-0.0.6-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: ouros-0.0.6-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 14.9 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ouros-0.0.6-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 ec1e6d7eebd2dc274c6403b9d9d03c96f06f5abd8f88b180bbaed265e73acfdc
MD5 8f809f8c8ba1b6aa1c5d9bc1cc617f6f
BLAKE2b-256 7a32164cddfd4a110c65b1e6d225e789bb4261a9abe5deaf81de5e82a7425d56

See more details on using hashes here.

File details

Details for the file ouros-0.0.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ouros-0.0.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5e3eec9285c6d9266ce4cf61d336690a1dec7ad05e10d8284fac860f7a993b0f
MD5 d7826e33be0a2e8e9773ca9a1cc50159
BLAKE2b-256 0b46db95e961399a39e8b2d221adf892330836479d8575ee664cc95c1bb0d0cd

See more details on using hashes here.

File details

Details for the file ouros-0.0.6-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for ouros-0.0.6-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 eca5fe73871af78e42c9efee227260f32ed29b94f1a408af15b6df5c0dc62bb7
MD5 f0c4709dd5994a1b4608ec1a5d6bfc36
BLAKE2b-256 b38f5236252b8f37d970476275e6dfcb7eaa4522d59b581f72b64191c2f4310e

See more details on using hashes here.

File details

Details for the file ouros-0.0.6-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ouros-0.0.6-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 aab37c0107868ec768866d1f1dfafdec4b9bbe2de9e598fb3f1628dedcc85f6d
MD5 f49fe8a46d10a4dce6aa68aaee50814c
BLAKE2b-256 1d13df05897e7eebc29682763b452f14fe45fade565005a7ad5f0f6e5ca0f6e5

See more details on using hashes here.

File details

Details for the file ouros-0.0.6-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for ouros-0.0.6-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e138a3960e0a191ab184aa43821aa08d107f9873c9e566bd565a484c834fe8cb
MD5 e94235798a1e01c26c15524ebe0218ef
BLAKE2b-256 337849d0dade09f008baa995888e9950422596f9233f09b96719c72492ede780

See more details on using hashes here.

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