Python Jupyter Environment

class oscopilot.environments.py_jupyter_env.AddLinePrints[source]

Bases: NodeTransformer

Transformer to insert print statements indicating the line number before every executable line in the AST.

insert_print_statement(line_number)[source]

Inserts a print statement for a given line number.

Parameters:

line_number (int) – The line number.

Returns:

The print statement AST node.

Return type:

ast.Expr

process_body(body)[source]

Processes a block of statements, adding print calls.

Parameters:

body (list) – List of AST nodes representing statements.

Returns:

List of modified AST nodes.

Return type:

list

visit(node)[source]

Visits and transforms nodes in the AST.

Parameters:

node – The current AST node.

Returns:

The modified AST node.

Return type:

ast.Node

class oscopilot.environments.py_jupyter_env.PythonJupyterEnv[source]

Bases: BaseEnv

A class representing an environment for executing Python code in a Jupyter environment.

This class manages the execution of Python code using IPython kernel, providing methods for preprocessing code, executing code steps, handling output messages, and terminating the kernel.

It inherits from BaseEnv, which provides basic environment functionality.

aliases = ['py', 'API']
detect_active_line(line)[source]

Detects active line markers in the output line.

Parameters:

line (str) – The output line from the IPython kernel.

Returns:

The modified line and active line number, if detected.

Return type:

tuple

file_extension = 'py'
name = 'Python'
preprocess_code(code)[source]

Preprocesses the Python code before execution.

Parameters:

code (str) – The Python code to preprocess.

Returns:

The preprocessed code.

Return type:

str

step(code)[source]

Executes a step of Python code.

Parameters:

code (str) – The Python code to execute.

Yields:

dict – Output messages generated during execution.

stop()[source]

Stops the execution of code by setting the finish flag.

terminate()[source]

Terminates the IPython kernel and stops its channels.

oscopilot.environments.py_jupyter_env.add_active_line_prints(code)[source]

Adds print statements indicating line numbers to a Python string.

Parameters:

code (str) – The Python code.

Returns:

The code with added print statements.

Return type:

str

oscopilot.environments.py_jupyter_env.main()[source]
oscopilot.environments.py_jupyter_env.string_to_python(code_as_string)[source]

Parses Python code from a string and extracts function definitions.

Parameters:

code_as_string (str) – The Python code as a string.

Returns:

A dictionary mapping function names to their code.

Return type:

dict

oscopilot.environments.py_jupyter_env.wrap_in_try_except(code)[source]

Wraps Python code in a try-except block to catch exceptions.

Parameters:

code (str) – The Python code.

Returns:

The code wrapped in a try-except block.

Return type:

str