Skip to main content

Code Runner Sandbox服务的Python SDK

Project description

Code Runner Python SDK

Code Runner服务的Python SDK,提供简单易用的API接口来执行代码和运行测试。

安装

pip install code-runner-sdk

快速开始

初始化客户端

from code_runner_sdk import CodeRunnerClient, ProgrammingLanguage

# 创建客户端实例
client = CodeRunnerClient(
    host="localhost",
    port=8000,
    protocol="http",
    api_key="your-api-key"  # 可选
)

直接运行代码

# 运行Python代码
result = client.run_code(
    code='print("Hello, World!")',
    language=ProgrammingLanguage.PYTHON
)

print(f"输出: {result['output']}")
print(f"执行时间: {result['execution_time']}ms")
print(f"内存使用: {result['memory_usage']}KB")

执行代码并运行测试

from code_runner_sdk import TestCase

# 定义测试用例
test_cases = [
    TestCase(
        input={"a": 1, "b": 2},
        expected_output=3,
        description="1 + 2 = 3"
    ),
    TestCase(
        input={"a": -1, "b": 1},
        expected_output=0,
        description="-1 + 1 = 0"
    )
]

# 执行代码和测试
code = """
def add(a, b):
    return a + b
"""

response = client.execute_code(
    code=code,
    language=ProgrammingLanguage.PYTHON,
    test_cases=test_cases
)

print(f"状态: {response.status}")
print(f"通过测试: {response.passed_tests}/{response.total_tests}")
print(f"执行时间: {response.execution_time}ms")
print(f"内存使用: {response.memory_usage}KB")

# 查看详细测试结果
for result in response.test_results:
    print(f"\n测试用例 {result.test_case}:")
    print(f"输入: {result.input}")
    print(f"预期输出: {result.expected_output}")
    print(f"实际输出: {result.actual_output}")
    print(f"通过: {'是' if result.passed else '否'}")
    if result.error:
        print(f"错误: {result.error}")

支持的编程语言

  • Python
  • JavaScript
  • Java
  • Kotlin
  • C++
  • Go
  • Rust
  • Bash
  • Objective-C
  • Swift

异常处理

from code_runner_sdk.exceptions import (
    APIError,
    ValidationError,
    ConfigurationError,
    TimeoutError
)

try:
    result = client.run_code(
        code='print("Hello, World!")',
        language=ProgrammingLanguage.PYTHON
    )
except ValidationError as e:
    print(f"验证错误: {e}")
except APIError as e:
    print(f"API错误: {e}")
    print(f"状态码: {e.status_code}")
    print(f"响应: {e.response}")
except TimeoutError as e:
    print(f"超时错误: {e}")
except ConfigurationError as e:
    print(f"配置错误: {e}")

配置选项

  • host: API服务器主机地址(默认:localhost)
  • port: API服务器端口(默认:8000)
  • protocol: 协议(http/https,默认:http)
  • api_key: API密钥(可选)
  • timeout: 请求超时时间(秒,默认:30)

开发

  1. 克隆仓库:
git clone https://github.com/yourusername/code-runner-sdk.git
cd code-runner-sdk
  1. 安装依赖:
pip install -r requirements.txt
  1. 运行测试:
python -m pytest tests/

许可证

MIT License

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

code-runner-sdk-0.1.1.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

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

code_runner_sdk-0.1.1-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file code-runner-sdk-0.1.1.tar.gz.

File metadata

  • Download URL: code-runner-sdk-0.1.1.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.1

File hashes

Hashes for code-runner-sdk-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3ea4e614ff4571218656dda895479ec4e686fb30b3044f841fa4efa82df4a44a
MD5 c5143771ce9b13f50f580057f02996ce
BLAKE2b-256 33b30c7d5ef1bc2b7dea43093e64e43e34f929f76f79c7722cc2ccf71fc4a904

See more details on using hashes here.

File details

Details for the file code_runner_sdk-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for code_runner_sdk-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8efd68c1b473a1bd126b03780f2dc51ea9f74ba14a1d9b22e009e5ada7579366
MD5 ff1e1e9bfca566f0aca2830cf1b33e2a
BLAKE2b-256 a41c89ddd2fe46300083399a5b7d041ba5dcf9484b66a696ab86691d5a196837

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