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.4.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.4-cp312-cp312-win_amd64.whl (15.0 MB view details)

Uploaded CPython 3.12Windows x86-64

ouros-0.0.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (14.0 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

ouros-0.0.4-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (12.9 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

ouros-0.0.4-cp312-cp312-macosx_11_0_arm64.whl (12.7 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

ouros-0.0.4-cp312-cp312-macosx_10_12_x86_64.whl (13.7 MB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: ouros-0.0.4.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.4.tar.gz
Algorithm Hash digest
SHA256 dcf58bdd3b39dcaf4bc1af0b2eb17d1e85ca3e476974f6eb3da198b489f896f8
MD5 83a1d89a662f5b26377fc1d699c2e1a4
BLAKE2b-256 d5fc10f587040b06541a8950d5c83d4e8d89645d1308c4593766392ac46724b5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ouros-0.0.4-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 15.0 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.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 2151c620b427664e167e88cbdcfc6bb02c51761718f7a5d218efbb5b174eb21d
MD5 71d47275ce6462935c02d98676f3f508
BLAKE2b-256 e0d98990a84fdb8840cf0baecefd627b085677e1450de4a2c9e3a97b1ac975df

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ouros-0.0.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e9a7cfdcc0bef2ec3dbe40bf6e61a1b610bb58df452f924ed2834c3ade3451bf
MD5 2fb32a6d718632043a19d1cdbf754729
BLAKE2b-256 6e0318e944a87db10b0f5441713fbbeb8342399d1ec73f8d46ac5e10416ae1b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ouros-0.0.4-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 57fe12673704717bae36ebea58640cdbe16c101464acb805d89a1faf25e52926
MD5 99f0b650fbc3c1eac917bc87308f0a28
BLAKE2b-256 18f7df568f6131cb1925756a86960b15aebf6c83917ec7ecc67c1fd9cb8040bd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ouros-0.0.4-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5dcb473f88f72e87e9ab82d97d55de969fc7955647291421e1385e45052bd11f
MD5 a09110a4a738ede7413cfaad5cccab81
BLAKE2b-256 8445d4c863e8f3933f6a719992b0437af80fedcaa1775353f7c646275cd2dff6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ouros-0.0.4-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 536a0e643b36524e938aa95e7f45ac7b7cbab1a6c314b1576031c2362da6338b
MD5 b5e5f8e07d85a177b558b8758eec98c8
BLAKE2b-256 b835621786c02b2f20e2a844b2b7a37e8b4df06bbf8d796ef9c4a722e7c5e5fc

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