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 Noneoutfile (
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.
CoderGPT
- 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.
The CoderGPT
class is designed to integrate various functionalities for working with code files, including inspecting, explaining, commenting, optimizing, testing, and documenting the code. It leverages a language model from OpenAI, specifically GPT-4 Turbo, to perform these tasks.
Constructor
- CoderGPT.__init__(model='gpt-4-turbo-preview')
Initialize the CoderGPT class.
The constructor initializes the CoderGPT
class with a specified model. By default, it uses the GPT-4 Turbo model. It sets up the language model with an API key from the environment variables and constructs a chain of operations starting with a prompt template indicating the role of a world-class software developer.
Inspect Package
This method inspects a given package or directory path, identifying the programming language of each file based on its file extension. It displays a table mapping files to their detected languages and returns this mapping as a dictionary.
Get Code
Extracts and returns the source code of a specified function or class within a file. If neither a function nor a class is specified, it returns the entire source code of the file.
Explainer
Takes a path to a code file and optionally a function or class name, then explains the content or functionality of the specified code. It utilizes a CodeExplainer
instance to perform this explanation.
Commenter
Adds comments to a code file at the specified path. It can optionally overwrite existing comments. This is facilitated by a CodeCommenter
instance.
Optimizer
Optimizes the code within a file for performance or readability. The method allows specifying a function or class to focus the optimization on. It leverages a CodeOptimizer
to perform these optimizations.
Test Writer
Generates tests for a specified function or class within a code file. This functionality is provided through a CodeTester
instance.
Documenter
Documents the code in a given file, producing comprehensive documentation that can include descriptions of functionality, parameters, return values, and more. This is accomplished using a CodeDocumenter
instance.
Main
The script checks if it is the main module and, if so, creates an instance of CoderGPT
and inspects the “src” directory. This serves as an example of how to use the CoderGPT
class to perform an inspection of a codebase.