Cst
FunctionAndClassCollector
Bases: cst.CSTVisitor
A CSTVisitor that collects the names of functions and classes from a CST tree.
Source code in write_the/cst/function_and_class_collector.py
__init__(force, update=False)
Initializes the FunctionAndClassCollector.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
force |
bool
|
Whether to force the collection of functions and classes even if they have docstrings. |
required |
update |
bool
|
Whether to update the collection of functions and classes if they have docstrings. |
False
|
Source code in write_the/cst/function_and_class_collector.py
leave_ClassDef(node)
visit_ClassDef(node)
Visits a ClassDef node and adds its name to the list of classes if it does not have a docstring or if force
or update
is True
. Also sets the current class name for nested function collection.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node |
cst.ClassDef
|
The ClassDef node to visit. |
required |
Source code in write_the/cst/function_and_class_collector.py
visit_FunctionDef(node)
Visits a FunctionDef node and adds its name to the list of functions if it does not have a docstring or if force
or update
is True
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node |
cst.FunctionDef
|
The FunctionDef node to visit. |
required |
Source code in write_the/cst/function_and_class_collector.py
get_node_names(tree, force, update=False)
Gets the names of functions and classes from a CST tree.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tree |
cst.CSTNode
|
The CST tree to traverse. |
required |
force |
bool
|
Whether to force the collection of functions and classes even if they have docstrings. |
required |
update |
bool
|
Whether to update the collection of functions and classes if they have docstrings. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
list[str]: A list of function and class names. |
Source code in write_the/cst/function_and_class_collector.py
DocstringAdder
Bases: cst.CSTTransformer
Source code in write_the/cst/docstring_adder.py
add_docstring(node)
Adds a docstring to a CST node if it doesn't have one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node |
cst.CSTNode
|
The CST node to add a docstring to. |
required |
Returns:
Type | Description |
---|---|
cst.CSTNode: The updated CST node with a docstring added if it didn't have one. |
Note
If the node already has a docstring and the force flag is set, the existing docstring is removed before adding the new one.
Source code in write_the/cst/docstring_adder.py
leave_ClassDef(original_node, updated_node)
Adds a docstring to a class definition if it doesn't have one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
original_node |
cst.ClassDef
|
The original CST node representing the class definition. |
required |
updated_node |
cst.ClassDef
|
The updated CST node representing the class definition. |
required |
Returns:
Type | Description |
---|---|
cst.ClassDef
|
cst.ClassDef: The updated CST node with a docstring added if it didn't have one. |
Source code in write_the/cst/docstring_adder.py
leave_FunctionDef(original_node, updated_node)
Adds a docstring to a function definition if it doesn't have one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
original_node |
cst.FunctionDef
|
The original CST node representing the function definition. |
required |
updated_node |
cst.FunctionDef
|
The updated CST node representing the function definition. |
required |
Returns:
Type | Description |
---|---|
cst.FunctionDef
|
cst.FunctionDef: The updated CST node with a docstring added if it didn't have one. |
Source code in write_the/cst/docstring_adder.py
DocstringRemover
Bases: cst.CSTTransformer
Source code in write_the/cst/docstring_remover.py
__init__(nodes)
Initializes the DocstringRemover object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes |
list
|
A list of nodes to remove docstrings from. |
required |
leave_ClassDef(original_node, updated_node)
Removes the docstring from a ClassDef node if it is in the list of nodes and resets the current_class attribute to None.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
original_node |
cst.ClassDef
|
The original ClassDef node. |
required |
updated_node |
cst.ClassDef
|
The updated ClassDef node. |
required |
Returns:
Type | Description |
---|---|
cst.ClassDef
|
cst.ClassDef: The updated ClassDef node with the docstring removed if it is in the list of nodes. |
Source code in write_the/cst/docstring_remover.py
leave_FunctionDef(original_node, updated_node)
Removes the docstring from a FunctionDef node if it is in the list of nodes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
original_node |
cst.FunctionDef
|
The original FunctionDef node. |
required |
updated_node |
cst.FunctionDef
|
The updated FunctionDef node. |
required |
Returns:
Type | Description |
---|---|
cst.FunctionDef
|
cst.FunctionDef: The updated FunctionDef node with the docstring removed if it is in the list of nodes. |
Source code in write_the/cst/docstring_remover.py
remove_docstrings_from_tree(tree, nodes)
Removes the docstrings from a tree of nodes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tree |
cst.CSTNode
|
The tree of nodes to remove the docstrings from. |
required |
nodes |
list
|
A list of nodes to remove docstrings from. |
required |
Returns:
Type | Description |
---|---|
cst.CSTNode: The tree of nodes with the docstrings removed. |
Source code in write_the/cst/docstring_remover.py
get_docstring(node)
Retrieves the docstring of a CSTNode if it has one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node |
cst.CSTNode
|
The node to check. |
required |
Returns:
Type | Description |
---|---|
Optional[str]
|
Optional[str]: The docstring of the node if it exists, None otherwise. |
Notes
Only retrieves docstrings for FunctionDef and ClassDef nodes.
Source code in write_the/cst/utils.py
has_docstring(node)
Checks if a CSTNode has a docstring.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node |
cst.CSTNode
|
The node to check. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
Whether or not the node has a docstring. |
Notes
Only checks for docstrings on FunctionDef and ClassDef nodes.
Source code in write_the/cst/utils.py
nodes_to_tree(nodes)
Converts a list of CSTNodes into a CSTModule.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes |
list[cst.CSTNode]
|
The list of nodes to convert. |
required |
Returns:
Type | Description |
---|---|
cst.Module: The CSTModule containing the given nodes. |
Source code in write_the/cst/utils.py
remove_docstring(node)
Removes the docstring from a CSTNode.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node |
cst.CSTNode
|
The node to remove the docstring from. |
required |
Returns:
Type | Description |
---|---|
cst.CSTNode: The node with the docstring removed. |
Source code in write_the/cst/utils.py
Background
Bases: Node
A class representing a background in a CST tree.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
body |
cst.CSTNode
|
The CST node of the background. |
required |
Source code in write_the/cst/node_batcher.py
__init__(body)
Initializes a Background object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
body |
cst.CSTNode
|
The CST node of the background. |
required |
Source code in write_the/cst/node_batcher.py
Node
A class representing a node in a CST tree.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The name of the node. |
required |
node |
cst.CSTNode
|
The CST node. |
required |
code |
str
|
The code of the node. |
required |
tokens |
int
|
The number of tokens in the node. |
required |
Source code in write_the/cst/node_batcher.py
__init__(*, tree, node_name, response_size=80)
Initializes a Node object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tree |
cst.Module
|
The CST tree. |
required |
node_name |
str
|
The name of the node. |
required |
response_size |
int
|
The size of the response. |
80
|
Source code in write_the/cst/node_batcher.py
NodeBatch
dataclass
A class representing a batch of nodes in a CST tree.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tree |
cst.Module
|
The CST tree. |
required |
background |
Optional[Background]
|
The background of the tree. |
required |
max_tokens |
int
|
The maximum number of tokens in the batch. |
required |
prompt_size |
int
|
The size of the prompt. |
required |
nodes |
List[Node]
|
The list of nodes in the batch. |
field(default_factory=list)
|
max_batch_size |
Optional[int]
|
The maximum size of the batch. |
None
|
send_node_context |
bool
|
Whether to send the context of the nodes. |
False
|
Source code in write_the/cst/node_batcher.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|
code
property
Gets the code of the batch.
Returns:
Name | Type | Description |
---|---|---|
str |
The code of the batch. |
node_names: List[str]
property
Gets the names of the nodes in the batch.
Returns:
Type | Description |
---|---|
List[str]
|
List[str]: The names of the nodes in the batch. |
space_available: int
property
Gets the amount of space available in the batch.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The amount of space available in the batch. |
tokens: int
property
Gets the number of tokens in the batch.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The number of tokens in the batch. |
add(node)
Adds a node to the batch.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node |
Node
|
The node to add. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If there is no space available in the batch. |
Source code in write_the/cst/node_batcher.py
create_batches(tree, node_names, max_tokens, prompt_size, response_size_per_node, max_batch_size=None, send_background_context=True, send_node_context=True, remove_docstrings=True)
Creates batches of nodes from a tree.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tree |
cst.Module
|
The tree to create batches from. |
required |
node_names |
List[str]
|
The names of the nodes to create batches for. |
required |
max_tokens |
int
|
The maximum number of tokens per batch. |
required |
prompt_size |
int
|
The size of the prompt for each node. |
required |
response_size_per_node |
int
|
The size of the response for each node. |
required |
max_batch_size |
Optional[int]
|
The maximum number of nodes per batch. |
None
|
send_background_context |
bool
|
Whether to send background context. |
True
|
send_node_context |
bool
|
Whether to send node context. |
True
|
remove_docstrings |
bool
|
Whether to remove docstrings from the tree. |
True
|
Returns:
Type | Description |
---|---|
List[NodeBatch]
|
List[NodeBatch]: A list of batches of nodes. |
Source code in write_the/cst/node_batcher.py
extract_background(tree)
Extracts the background from a CST tree.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tree |
cst.Module
|
The CST tree. |
required |
Returns:
Name | Type | Description |
---|---|---|
Background |
The background of the tree. |
Source code in write_the/cst/node_batcher.py
NodeExtractor
Bases: cst.CSTVisitor
Source code in write_the/cst/node_extractor.py
visit_ClassDef(node)
Visits a ClassDef node and sets the current_class attribute. If the class name is in the nodes list, it also adds the node to the extracted_nodes list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node |
cst.ClassDef
|
The ClassDef node to visit. |
required |
Side Effects
Modifies the current_class attribute of the NodeExtractor instance, setting it to the name of the visited node. If the class name is in the nodes list, it also modifies the extracted_nodes list, adding the node.
Source code in write_the/cst/node_extractor.py
visit_FunctionDef(node)
Visits a FunctionDef node and adds it to the extracted_nodes list if its name is in the nodes list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node |
cst.FunctionDef
|
The FunctionDef node to visit. |
required |
Side Effects
Modifies the extracted_nodes list of the NodeExtractor instance, adding the node if its name is in the nodes list.
Source code in write_the/cst/node_extractor.py
extract_nodes_from_tree(tree, nodes)
Extracts specified nodes from a CST tree.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tree |
cst.CSTNode
|
The CST tree to extract nodes from. |
required |
nodes |
list of str
|
A list of node names to extract. |
required |
Returns:
Type | Description |
---|---|
list of cst.CSTNode: A list of extracted nodes. |
Examples:
Source code in write_the/cst/node_extractor.py
NodeRemover
Bases: cst.CSTTransformer
Source code in write_the/cst/node_remover.py
__init__(nodes)
Initializes a NodeRemover instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes |
list
|
A list of nodes to remove. |
required |
leave_ClassDef(original_node, updated_node)
Removes a ClassDef node from the tree if it is in the list of nodes to remove.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
original_node |
cst.ClassDef
|
The original ClassDef node. |
required |
updated_node |
cst.ClassDef
|
The updated ClassDef node. |
required |
Returns:
Type | Description |
---|---|
cst.RemovalSentinel
|
cst.RemovalSentinel: A sentinel indicating whether the node should be removed. |
Source code in write_the/cst/node_remover.py
leave_FunctionDef(original_node, updated_node)
Removes a FunctionDef node from the tree if it is in the list of nodes to remove. The node is identified by its fully qualified name, which includes the class name if the function is a method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
original_node |
cst.FunctionDef
|
The original FunctionDef node. |
required |
updated_node |
cst.FunctionDef
|
The updated FunctionDef node. |
required |
Returns:
Type | Description |
---|---|
cst.RemovalSentinel
|
cst.RemovalSentinel: A sentinel indicating whether the node should be removed. |
Source code in write_the/cst/node_remover.py
remove_nodes_from_tree(tree, nodes)
Removes specified nodes from a CST tree.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tree |
cst.CSTNode
|
The CST tree to remove nodes from. |
required |
nodes |
list
|
A list of nodes to remove. |
required |
Returns:
Type | Description |
---|---|
cst.CSTNode: The updated CST tree after removal of specified nodes. |