Skip to main content

Generic code evaluation in isolated Docker containers

Project description

docker-evaluator

A code evaluation backend for competitive programming judges. Runs untrusted submissions in isolated Docker containers, enforces time and memory limits, and checks output against expected results — similar to how Codeforces/CodeChef judge submissions.

Supports Python 2/3, C, and C++.

Requirements

  • Docker (running and accessible to the current user)
  • Python 3.9+

Installation

pip install docker-evaluator

Usage

from docker_evaluator import DockerEvaluator

evaluator = DockerEvaluator()

result = evaluator.evaluate(
    code='n = int(input()); print(n * 2)',
    input="21",
    expected_output="42",
    language="py3",
    time_limit=1,
    memory_limit=256 * 1024,  # 256 MB in KB
)

print(result)
# {'correct': True, 'details': 'OK (8ms)'}

evaluator.close()

Parameters

Parameter Type Default Description
code str Submission source code
input str Problem input (stdin or file content)
expected_output str Correct output to compare against
language str "py3", "py2", "c", or "cpp"
time_limit int Time limit in seconds
input_type str "stdin" "stdin" or "file"
file_io_name str "" File name when using file I/O (e.g. "input.txt")
memory_limit int 1024 Memory limit in KB (minimum enforced: 256 MB)

Return value

{"correct": bool, "details": str}

details is one of:

  • OK (Xms) — accepted
  • Wrong Answer
  • Time Limit Exceeded
  • Memory Limit Exceeded
  • Runtime Error (exit code N)
  • Compilation Error

Supported languages

Key Language
py3 Python 3
py2 Python 2
c C
cpp C++

Docker images are built automatically on first use and cached for subsequent runs.

File I/O

For problems that read/write files instead of stdin/stdout:

result = evaluator.evaluate(
    code=open("solution.cpp").read(),
    input="5\n1 2 3 4 5",
    expected_output="15",
    language="cpp",
    time_limit=2,
    input_type="file",
    file_io_name="input.txt",
)

Configuration

Create a .env file in your working directory:

Variable Default Description
KEEP_EVAL_CONTAINERS 0 Set to 1 to keep containers after a run (useful for debugging)
ENVIRONMENT If set, also loads .env.<ENVIRONMENT>

Compilation cache

C and C++ submissions are cached by source hash — repeated evaluations of the same code skip recompilation. To clear the cache:

from docker_evaluator import clear_cache

clear_cache()

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

docker_evaluator-0.1.1.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

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

docker_evaluator-0.1.1-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file docker_evaluator-0.1.1.tar.gz.

File metadata

  • Download URL: docker_evaluator-0.1.1.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for docker_evaluator-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d92f8281224a8a4a84e3942b2dad58f3231247cd351f0c5e338594e03e16a483
MD5 5c6c43a8a968775c55a12492dc2a58e6
BLAKE2b-256 b88cc61ea61d8423923218b7fc96ef93ca44dc5acc49bc6067d27e5cbfdcfaed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for docker_evaluator-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 72b56f3d18eeff4d042320ddbdeed2c208838a78084c376548a64137020d3ead
MD5 8d9e2e230434c4e75a808fd9887ce3b3
BLAKE2b-256 6c983f773697fea5b8a52c1b7a08b24db4932b983197f73f6b92bf64a994b340

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