fromfml_doc_gen.func_dtoimportFunctionDTODOCSTRING_TEMPLATE="""{function_name}: ### INSERT FUNCTION DEFINITION HERE ###{function_parameters}{function_output} Examples: -------- ### INSERT FUNCTION EXAMPLE USAGES HERE ###"""DOCSTRING_PARAMETERS_TEMPLATE=""" Parameters: ----------{function_params_str}"""DOCSTRING_PARAMETER_TEMPLATE="""{param_name}: {param_type} ### INSERT PARAMETER DEFINITION HERE ###"""DOCSTRING_OUTPUT_TEMPLATE=""" Returns: -------{function_output_type} ### INSERT ADDITIONAL FUNCTION OUTPUT INFORMATION HERE ###"""DOCSTRING_PARAMETER_TYPE_PLACEHOLDER="..."
[docs]defgenerate_template(function_signature:FunctionDTO)->str:""" Generates a formatted docstring template for a function based on its name, inputs, and output type. Args: function_signature (FunctionDTO): An object containing metadata about the function, including its name, input parameters, and output type. The `inputs` attribute should be a list of tuples where each tuple contains the name and type of a parameter. Returns: str: A string representing the generated docstring template for the function, formatted with placeholders for detailed parameter and return value descriptions. Raises: ValueError: If the `name` attribute of the `function_signature` is empty. Example: >>> function_signature = FunctionDTO( ... name="add_numbers", ... output="int", ... inputs=[("a", "int"), ("b", "int")] ... ) >>> generate_template(function_signature) """ifnotisinstance(function_signature,FunctionDTO):raiseTypeError(f"Expected input to be FunctionDTO, got {type(function_signature)}")# Name logicifnotfunction_signature.name:raiseValueError("The name of the function cannot be empty!")function_name=function_signature.name# Parameters logicfunction_params=[]forinput_name,input_typeinfunction_signature.inputs:function_param=DOCSTRING_PARAMETER_TEMPLATE.format(param_name=input_name,param_type=input_typeifinput_typeelseDOCSTRING_PARAMETER_TYPE_PLACEHOLDER)function_params.append(function_param)function_params_str="".join(function_params)function_parameters=DOCSTRING_PARAMETERS_TEMPLATE.format(function_params_str=function_params_str)iffunction_params_strelse""# Output typefunction_output_type=function_signature.output_typefunction_output=DOCSTRING_OUTPUT_TEMPLATE.format(function_output_type=function_output_type)iffunction_output_typeelse""# Final templatereturnDOCSTRING_TEMPLATE.format(function_name=function_name,function_parameters=function_parameters,function_output=function_output)