codergpt package

Subpackages

Submodules

codergpt.cli module

Command line interface for CoderGPT.

Author: Harshad Hegde

This module provides a command line interface (CLI) for CoderGPT, a powerful code generation tool.

Usage:

codergpt [OPTIONS] COMMAND [ARGS]…

Options:
-v, --verbose INTEGER

Verbosity level (0, 1 or 2).

-q, --quiet

Run in quiet mode.

--version

Show the version and exit.

Commands:

inspect Inspect package to show file-language-map. explain Inspect package to show file-language-map. comment Inspect package to show file-language-map.

codergpt.constants module

All constants used in the package.

codergpt.constants.PYTHON_CODE_COMMENT_TEMPLATE = PosixPath('/home/runner/work/CoderGPT/CoderGPT/src/codergpt/templates/python_code_comments.md')

Templates for different languages and commands.

Follows format:
{
“language”: {

“command”: “path/to/comment/template”,

}

}

codergpt.main module

Main python file.

class codergpt.main.CoderGPT(model='gpt-4-turbo-preview')

Bases: object

CoderGPT class.

bug_finder(path, function=None, classname=None)

Find bugs in the code file.

Parameters:
  • path (Union[str, Path]) – The path to the code file.

  • function (Optional[str]) – The name of the function to find bugs in. Default is None.

  • classname (Optional[str]) – The name of the class to find bugs in. Default is None.

bug_fixer(path, function=None, classname=None, outfile=None)

Fix bugs in the code file.

Parameters:
  • path (Union[str, Path]) – The path to the code file.

  • function (Optional[str]) – The name of the function to fix bugs in. Default is None.

  • classname (Optional[str]) – The name of the class to fix bugs in. Default is None.

  • outfile (Optional[str]) – The path to the output file. Default is None.

commenter(path, overwrite=False)

Add comments to the code file.

Parameters:
  • path (Union[str, Path]) – The path to the code file.

  • overwrite (bool) – Whether to overwrite the existing comments. Default is False.

documenter(path, outfile=None)

Document the code file.

Parameters:

path (Union[str, Path]) – The path to the code file.

explainer(path, function=None, classname=None)

Explains contents of the code file.

Parameters:
  • path (Union[str, Path]) – The path to the code file.

  • function (Optional[str]) – The name of the function to explain. Default is None.

  • classname – The name of the class to explain. Default is None.

get_code(filename, function_name=None, class_name=None)

Extract and return the source code of the specified function or class from a file.

Parameters:
  • filename (str) – The path to the file containing the code.

  • function_name (Optional[str]) – The name of the function to extract code for. Default is None.

  • class_name (Optional[str]) – The name of the class to extract code for. Default is None.

Return type:

Optional[str]

Returns:

The extracted source code of the specified function or class, if found.

inspect_package(path)

Inspecting the code and displaying a mapping of files to their languages in a table.

Parameters:

path (Union[str, Path]) – The path to the package or directory.

optimizer(path, function=None, classname=None, overwrite=False)

Optimize the code file.

Parameters:

path (Union[str, Path]) – The path to the code file.

test_writer(path, function=None, classname=None, outfile=None)

Test the code file.

Parameters:

path (Union[str, Path]) – The path to the code file.

codergpt.utils module

Utility functions for the codergpt package.

codergpt.utils.extract_code_from_response(language, response, filename, outfile=None)

Generate code files based on LLM responses.

Parameters:
  • language (str) – Code language.

  • response (str) – LLM response.

  • filename (Union[str, Path]) – Source code file.

  • outfile (Optional[str]) – Destination filepath, defaults to None

Return type:

str

codergpt.utils.get_language_from_extension(filename)

Get the language of a file from its extension.

Parameters:

filename (Union[str, Path]) – The filename to get the language for.

Return type:

Optional[str]

Returns:

The language of the file, if found.

Module contents

CoderGPT package.

class codergpt.CodeCommenter(chain)

Bases: object

Code Explainer class that extracts and explains code from a given file.

comment(code, filename, overwrite=False, language=None)

Comment the contents of the code string by invoking the runnable chain and write it to a new file.

Parameters:
  • code (str) – The string containing the code to be commented.

  • filename (str) – The original filename of the code file.

  • overwrite (bool) – A boolean indicating whether to overwrite the original file. Default is False.

  • language (Optional[str]) – Coding language of the file, defaults to None

class codergpt.CodeDocumenter(chain)

Bases: object

Code Explainer class that extracts and explains code from a given file.

document(filename, code, language=None, outfile=None)

Document the contents of the code file by invoking the runnable chain.

Parameters:
  • filename (str) – filename of the code file.

  • code (str) – The string containing the code to be documented.

  • language (Optional[str]) – Coding language of the file, defaults to None

  • outfile (Optional[str]) – Destination filepath, defaults to None

class codergpt.CodeExplainer(chain)

Bases: object

Code Explainer class that extracts and explains code from a given file.

explain(code, function=None, classname=None, language=None)

Explain the contents of the code file by invoking the runnable chain.

Parameters:
  • code (str) – The code to be explained.

  • function (Optional[str]) – The name of the function to explain. Default is None.

  • classname (Optional[str]) – The name of the class to explain. Default is None.

class codergpt.CodeOptimizer(chain)

Bases: object

Code Explainer class that extracts and explains code from a given file.

optimize(filename, function=None, classname=None, overwrite=False)

Optimize the the code by invoking the runnable chain.

Parameters:
  • path – The path to the code file to be explained.

  • function (Optional[str]) – The name of the function to explain. Default is None.

  • classname (Optional[str]) – The name of the class to explain. Default is None.

class codergpt.CoderGPT(model='gpt-4-turbo-preview')

Bases: object

CoderGPT class.

bug_finder(path, function=None, classname=None)

Find bugs in the code file.

Parameters:
  • path (Union[str, Path]) – The path to the code file.

  • function (Optional[str]) – The name of the function to find bugs in. Default is None.

  • classname (Optional[str]) – The name of the class to find bugs in. Default is None.

bug_fixer(path, function=None, classname=None, outfile=None)

Fix bugs in the code file.

Parameters:
  • path (Union[str, Path]) – The path to the code file.

  • function (Optional[str]) – The name of the function to fix bugs in. Default is None.

  • classname (Optional[str]) – The name of the class to fix bugs in. Default is None.

  • outfile (Optional[str]) – The path to the output file. Default is None.

commenter(path, overwrite=False)

Add comments to the code file.

Parameters:
  • path (Union[str, Path]) – The path to the code file.

  • overwrite (bool) – Whether to overwrite the existing comments. Default is False.

documenter(path, outfile=None)

Document the code file.

Parameters:

path (Union[str, Path]) – The path to the code file.

explainer(path, function=None, classname=None)

Explains contents of the code file.

Parameters:
  • path (Union[str, Path]) – The path to the code file.

  • function (Optional[str]) – The name of the function to explain. Default is None.

  • classname – The name of the class to explain. Default is None.

get_code(filename, function_name=None, class_name=None)

Extract and return the source code of the specified function or class from a file.

Parameters:
  • filename (str) – The path to the file containing the code.

  • function_name (Optional[str]) – The name of the function to extract code for. Default is None.

  • class_name (Optional[str]) – The name of the class to extract code for. Default is None.

Return type:

Optional[str]

Returns:

The extracted source code of the specified function or class, if found.

inspect_package(path)

Inspecting the code and displaying a mapping of files to their languages in a table.

Parameters:

path (Union[str, Path]) – The path to the package or directory.

optimizer(path, function=None, classname=None, overwrite=False)

Optimize the code file.

Parameters:

path (Union[str, Path]) – The path to the code file.

test_writer(path, function=None, classname=None, outfile=None)

Test the code file.

Parameters:

path (Union[str, Path]) – The path to the code file.