lookout.style.format.code_generator

Code generator that able to generate new source code from format model style suggestions.

Module Contents

exception lookout.style.format.code_generator.CodeGenerationBaseError

Bases:Exception

Base class for CodeGenerator exceptions.

exception lookout.style.format.code_generator.CodeGenerationError

Bases:lookout.style.format.code_generator.CodeGenerationBaseError

CodeGenerator.generate() can raise this exception if it fails to generate new file.

To avoid this you can use skip_errors=True while creating CodeGenerator instance.

exception lookout.style.format.code_generator.InapplicableIndentation

Bases:lookout.style.format.code_generator.CodeGenerationBaseError

InapplicableIndentation raises if you try to decrease tab indentation and have space indentation. And vice versa.

class lookout.style.format.code_generator.CodeGenerator(feature_extractor:FeatureExtractor, skip_errors:bool=False, verbose:bool=False, url:str='<N/A>', commit:str='<N/A>')

Generate new source code from format model suggestions.

Use generate() function to get a result.

_log
INDENTATIONS_DEC
INDENTATIONS_INC
DEC_TO_INC
INDENTATIONS
NEWLINE_INDEX
generate_new_line(self, line_vnodes:List[VirtualNode])

Generate new code line for giving vnodes.

Parameters:line_vnodes – corresponding virtual nodes sequence. It should be started from token with indentation change or newline token from the prevoius line if exists. The final line break of the line should not be included.
Returns:Code line.
apply_predicted_y(self, vnodes:Sequence[VirtualNode], vnodes_y:Sequence[VirtualNode], rule_winners:Sequence[int], rules:Rules)

Update labels for the sequence of VirtualNode-s. We also discard NOOPs.

Parameters:
  • vnodes – Sequence of all the VirtualNode-s corresponding to the input code file. Should be ordered by position.
  • vnodes_y – Sequence of the labeled VirtualNode-s corresponding to labeled samples. Should be ordered by start position value.
  • rule_winners – Sequence of applied rules to generate y_pred.
  • rules – Rules that were used for prediction.
Returns:

The list of VirtualNode-s with adjusted labels. “y_old” attribute of each node contains the previous label.

generate_one_change(self, vnodes:Sequence[VirtualNode], changed_vnode_index:int, new_label:Tuple[int, ...])

Generate new source code for single provided change in vnode Sequence.

Parameters:
  • vnodes – Sequence of the VirtualNode-s for the code snippet.
  • changed_vnode_index – index in vnodes sequence where new label should be applied.
  • new_label – new label for single change in vnodes[changed_vnode_index].
Returns:

New source code.

generate(self, vnodes:Sequence[VirtualNode])

Generate new source code from format model suggestions.

Parameters:vnodes – Sequence of all the VirtualNode-s corresponding to the input code file. Should be ordered by position.
Returns:New source code file content.
_can_set_label(self, vnode:VirtualNode, last_indentation:str)

Check if a new label is applicable to a VirtualNode.

Return False or raises FileGenerationError if it is not applicable. True means nothing because applicability depends also on the context.

_iterate_vnodes(self, vnodes:Sequence[VirtualNode], vnodes_y:Sequence[VirtualNode], rule_winners:ndarray, rules:Rules)
_handle_error(self, msg:str)
classmethod apply_new_indentation(cls, vnode:VirtualNode, last_indentation:str)

Apply new indentation token vnode.y to the vnode.value.

Old value for vnode.y must be saved in vnode.y_old. If it is not possible to apply new indentation InapplicableIndentation() is raised.

Parameters:
  • vnode – VirtualNode with new indentation label y and old label y_old.
  • last_indentation – Last indentation in code. Usually indentation from the previous line. If you do not expect additional line breaks in comparision with vnode.y_old you can set to empty line.
Returns:

string with values before indentation and string with a new indentation.

classmethod revert_indentation_change(cls, vnode:VirtualNode)

Reverts original change for provided VirtualNode.

Example: for vnode ``` VirtualNode(value=’⏎␣␣␣␣’, y=⏎␣⁺␣⁺, start=(852, 26, 20), end=(857, 27, 5), node=None,

path=”lib/find-chrome.js”)

``` revert_indentation_change() removes two spaces that were added. The result is ⏎␣␣.

Parameters:vnode – VirtualNode with indentation label y.
Returns:new value for VirtualNode.value.
static _split_by_set(to_split:tuple, s:set)