janus.language.block#

Attributes#

log

Classes#

CodeBlock

A class that represents a functional block of code.

TranslatedCodeBlock

A class that represents the translated functional block of code.

Module Contents#

janus.language.block.log#
class janus.language.block.CodeBlock(id, name, node_type, language, text, start_point, end_point, start_byte, end_byte, tokens, children, embedding_id=None, affixes=('', ''))#

A class that represents a functional block of code.

Parameters:
  • id (Hashable) –

  • name (Optional[str]) –

  • node_type (janus.language.node.NodeType) –

  • language (str) –

  • text (Optional[str]) –

  • start_point (Optional[Tuple[int, int]]) –

  • end_point (Optional[Tuple[int, int]]) –

  • start_byte (Optional[int]) –

  • end_byte (Optional[int]) –

  • tokens (int) –

  • children (list[ForwardRef('CodeBlock')]) –

  • embedding_id (Optional[str]) –

  • affixes (Tuple[str, str]) –

id#

The id of the code block in the AST

name#

Descriptive name of node

node_type#

The type of the code block (‘function’, ‘module’, etc.). Defined in the language-specific modules.

language#

The language of the code block.

text#

The code block.

start_point#

The line and column numbers of the first line of the code block.

end_point#

The line and column numbers of the last line of the code block.

start_byte#

starting byte offset into file

end_byte#

ending byte offset into file

tokens#

The number of tokens in the code block.

children#

A tuple of child code blocks.

embedding_id#

id of embedding

affixes#

prefix and suffix text for node

complete#

Rolls up self and children’s complete status, incomplete means a child is missing.

property prefix: str#
Return type:

str

property suffix: str#
Return type:

str

property complete_text: str#
Return type:

str

property n_descendents: int#

The total number of descendents of this block

Returns:

The total number of descendents of this block

Return type:

int

property height: int#

The number of edges between this node and a leaf

Returns:

The number of edges between this node and a leaf

Return type:

int

property max_tokens: int#

The maximum number of tokens in this block or any of its descendents

Returns:

The maximum number of tokens in this block or any of its descendents

Return type:

int

property total_tokens: int#

The total tokens represented by this block and all its descendents

Returns:

The total number of tokens represented by this block and all its descendents

Return type:

int

pop_prefix()#

Get this block’s prefix and remove it from the block. This may be used to transfer the prefix from the first child of a node to its parent.

Return type:

str

pop_suffix()#

Get this block’s suffix and remove it from the block. This may be used to transfer the suffix from the first child of a node to its parent.

Return type:

str

rebuild_text_from_children()#
tree_str(depth=0)#

A string representation of the tree with this block as the root

Returns:

A string representation of the tree with this block as the root

Parameters:

depth (int) –

Return type:

str

class janus.language.block.TranslatedCodeBlock(original, language)#

Bases: CodeBlock

A class that represents the translated functional block of code.

original#

The original code block.

cost#

The total cost to translate the original code block.

retries#

The number of times translation had to be retried for this code

translated#

Whether this block has been successfully translated

Create an “empty” TranslatedCodeBlock from the given original

Parameters:
  • original (CodeBlock) – The original code block

  • language (str) – The language to translate to

Returns:

A TranslatedCodeBlock with the same attributes as the original, except for text, path, complete, language, tokens, and children

property total_cost: float#

The total cost spent translating this block and all its descendents

Returns:

The total cost spent translating this block and all its descendents

Return type:

float

property total_retries: int#

The total number of retries that were required to translate this block and all its descendents

Returns:

The total number of retries that were required to translate this block and

Return type:

int

property total_input_tokens: int#

The total number of input tokens represented by this block and all its successfully-translated descendents

Returns:

The total number of input tokens represented by this block and all its

Return type:

int

property translation_completeness: float#

The share of the input that was successfully translated

Returns:

The share of the input that was successfully translated

Return type:

float