Share via


CodeBlock Class

Create a code block.

A code block is a block that usually contains functions to be executed by the kernel. It consists of a list of tokens that can be either a function_id, value, a variable or a named argument.

If the first token is not a function_id but a variable or value, the rest of the tokens will be ignored. Only the first argument for the function can be a variable or value, the rest of the tokens have be named arguments.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Constructor

CodeBlock(*, content: str, tokens: list[Block] = None)

Parameters

Name Description
content
Required

The content of the code block.

tokens
Required

The list of tokens that compose the code block, if empty, will be created by the CodeTokenizer.

Keyword-Only Parameters

Name Description
content
Required
tokens
Required

Methods

check_tokens

Check the tokens in the list.

If the first token is a value or variable, the rest of the tokens will be ignored. If the first token is a function_id, then the next token can be a value,

variable or named_arg, the rest have to be named_args.

parse_content

Parse the content of the code block and tokenize it.

If tokens are already present, skip the tokenizing.

render_code

Render the code block.

If the first token is a function_id, it will call the function from the plugin collection. Otherwise, it is a value or variable and those are then rendered directly.

check_tokens

Check the tokens in the list.

If the first token is a value or variable, the rest of the tokens will be ignored. If the first token is a function_id, then the next token can be a value,

variable or named_arg, the rest have to be named_args.

check_tokens(tokens: list[Block]) -> list[Block]

Parameters

Name Description
tokens
Required

Exceptions

Type Description

If the content does not contain at least one token.

If the first token is a named argument.

If the second token is not a value or variable.

If a token is not a named argument after the second token.

parse_content

Parse the content of the code block and tokenize it.

If tokens are already present, skip the tokenizing.

parse_content(fields: Any) -> Any

Parameters

Name Description
fields
Required

render_code

Render the code block.

If the first token is a function_id, it will call the function from the plugin collection. Otherwise, it is a value or variable and those are then rendered directly.

async render_code(kernel: Kernel, arguments: KernelArguments) -> str

Parameters

Name Description
kernel
Required
arguments
Required

Attributes

tokens

tokens: list[Block]

type

type: ClassVar[BlockTypes] = 3