DockerInterpreter

class DockerInterpreter(BaseInterpreter):

A class for executing code files or code strings in a docker container.

This class handles the execution of code in different scripting languages (currently Python and Bash) within a docker container, capturing their stdout and stderr streams, and allowing user checking before executing code strings.

Parameters:

  • require_confirm (bool, optional): If True, prompt user before running code strings for security. Defaults to True.
  • print_stdout (bool, optional): If True, print the standard output of the executed code. Defaults to False.
  • print_stderr (bool, optional): If True, print the standard error of the executed code. Defaults to True.

init

def __init__(
    self,
    require_confirm: bool = True,
    print_stdout: bool = False,
    print_stderr: bool = True
):

del

def __del__(self):

Destructor for the DockerInterpreter class.

This method ensures that the Docker container is removed when the interpreter is deleted.

_initialize_if_needed

def _initialize_if_needed(self):

_create_file_in_container

def _create_file_in_container(self, content: str):

_run_file_in_container

def _run_file_in_container(self, file: Path, code_type: str):

run

def run(self, code: str, code_type: str):

Executes the given code in the container attached to the interpreter, and captures the stdout and stderr streams.

Parameters:

  • code (str): The code string to execute.
  • code_type (str): The type of code to execute (e.g., ‘python’, ‘bash’).

Returns:

str: A string containing the captured stdout and stderr of the executed code.

_check_code_type

def _check_code_type(self, code_type: str):

supported_code_types

def supported_code_types(self):

Provides supported code types by the interpreter.

update_action_space

def update_action_space(self, action_space: Dict[str, Any]):

Updates action space for python interpreter