Docstring Handler
- class docstring_handler.DocstringHandler[source]
Bases:
objectGenerates docstrings for all methods in a given class.
This function takes the file path and the class name as input, and generates docstrings for all the methods within the class. It uses the ast module to parse the file content, find the class definition, and then iterate through the methods within the class.
For each method, it generates a docstring using the generate_docstring function, which takes the function code and any context from child functions as input. The generated docstring is then inserted into the original file using the insert_docstring function.
- Args:
filepath (str): The file path containing the class definition. class_name (str): The name of the class for which to generate docstrings.
- Raises:
FileNotFoundError: If the specified file is not found. IOError: If there is an error reading or writing the file.
- generate_docstring(function_code, child_context='')[source]
Generate a docstring for a function based on the provided code and context.
- Args:
function_code (str): The code of the function to generate the docstring for. child_context (str, optional): Any context from child functions that should be included in the docstring. Defaults to an empty string.
- Returns:
str: The generated docstring for the function.
This function takes the code of a function and any relevant context from child functions, and generates a Google-style Python docstring for the function. The docstring is generated using the Anthropic language model, and is formatted to be directly used in the code. The docstring includes a description of the function’s logic and purpose, as well as the function’s arguments and return value.
- generate_docstrings_for_all_methods_in_class(filepath, class_name)[source]
Generate docstrings for all methods in a class.
This function reads the content of a Python file, parses the AST, and generates docstrings for all methods defined within a specified class. It then inserts the generated docstrings into the original file.
- Args:
filepath (str): The path to the Python file containing the class. class_name (str): The name of the class for which to generate docstrings.
- Raises:
None
- Returns:
None
- get_node_in_file(node, name, class_name=None)[source]
Retrieve a node in the given AST (Abstract Syntax Tree) by its name and optional class name.
- Args:
node (ast.AST): The root node of the AST to search. name (str): The name of the node to find. class_name (str, optional): The name of the class containing the node, if applicable.
- Returns:
ast.AST or None: The node with the specified name, if found, otherwise None.
- Raises:
None
This function searches the given AST recursively to find a node with the specified name. If a class name is provided, it will first search for the class definition and then look for the node within that class. If the node is found, it is returned, otherwise None is returned.
- insert_docstring(file_path, func_name, docstring, function_code, is_class_func=False)[source]
Inserts a docstring into a Python file at the specified function or class.
- Args:
file_path (str): The path to the Python file. func_name (str): The name of the function or class to insert the docstring for. docstring (str): The docstring to be inserted. function_code (str): The code of the function or class. is_class_func (bool, optional): Indicates whether the function is a class method. Defaults to False.
- Returns:
None
- Raises:
FileNotFoundError: If the specified file is not found. IOError: If there is an error reading or writing the file.
This function first reads the contents of the specified Python file. It then searches for the definition of the function or class with the given name. If the function or class is found, the function inserts the provided docstring at the appropriate location in the file. If an existing docstring is found, it is replaced with the new docstring.
The function handles both single-line and multi-line function definitions. It also correctly handles the case where the function is a class method, adjusting the indentation accordingly.
If the function or class is not found in the file, a message is printed indicating that the function or class was not found.