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)— acceptedWrong AnswerTime Limit ExceededMemory Limit ExceededRuntime 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file docker_evaluator-0.1.0.tar.gz.
File metadata
- Download URL: docker_evaluator-0.1.0.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a671ffe1291075abf881e71848bc4ffa530742a47adaa456bcdd3910b209abc
|
|
| MD5 |
e46157c3e216bbbf2a83d2ef4320e2e6
|
|
| BLAKE2b-256 |
07f06877e8da825ceb2fc319dd866d7c911dff9f866cc3725a9f14b04d87b7f1
|
File details
Details for the file docker_evaluator-0.1.0-py3-none-any.whl.
File metadata
- Download URL: docker_evaluator-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e6b9be6cead588414ea59c16e9155e523daac51ed680701ad4429a16de6f570
|
|
| MD5 |
0af4a73519d5476b384c3a287ca6ea05
|
|
| BLAKE2b-256 |
f88c58df9237255c5452056e9c6df6abad10fd42b4cefa59118f8e6937ab7144
|