Package: docstring-gen
docstring-genInstantly improve the documentation of your Python code with
Codex.docstring-genis an easy-to-use Python library that usesOpenAI’s
Codex modelto automatically generateGoogle-style
docstringsfor Python codebase. The library is capable of reading
both Jupyter notebooks and Python files, and seamlessly adds meaningful
docstrings to classes and functions that lack documentation. By usingdocstring-gen, developers can automatically generate docstrings for
their codebase, resulting in time savings and improved documentation
quality.Installdocstring-gencan be installed by running the command below. This
package requires Python 3.7 or higher to work.pipinstalldocstring-genIf the installation was successful, you should now havedocstring-geninstalled on your system. To see a full list of
settings, rundocstring_gen --helpIf you’re excited to try the latest version, you can install it directly
from GitHub by using the command:pip install git+https://github.com/airtai/docstring-genHow to useThedocstring-genlibrary uses OpenAI’s Codex model to generate
docstrings for your Python classes and functions. In order to use the
library, you’ll need tocreate
an API key for OpenAI.Once you have your API key, store it in theOPENAI_API_KEYenvironment variable. This is a necessary step for the library to work.To get started right away with sensible defaults, run:docstring_gen{source_file_or_directory}This will automatically add meaningful, Google-style docstrings to the
Python classes and functions in the {source_file_or_directory} that do
not already have one.For example, a function like below without the docstring:defconcatenate_strings(s1:str,s2:str)->str:ifnotisinstance(s1,str)ornotisinstance(s2,str):raiseTypeError("Both arguments should be strings.")returns1+s2will become similar to:defconcatenate_strings(s1:str,s2:str)->str:"""Concatenate two strings.Args:s1: First strings2: Second stringReturns:The concatenated stringRaises:TypeError: If s1 or s2 is not a string!!! noteThe above docstring is autogenerated by docstring-gen library (https://docstring-gen.airt.ai)"""ifnotisinstance(s1,str)ornotisinstance(s2,str):raiseTypeError("Both arguments should be strings.")returns1+s2If you wish to regenerate the docstrings, you can re-run the command
with the-fflag, which will remove the previous auto-generated
docstrings and replace them with new ones.docstring_gen{source_file_or_directory}-fNote: The default behavior of the library is to add docstrings only
to functions and classes that are missing them. So, if you do not
provide the-fflag when re-running the command, the library will not
replace previously auto-generated docstrings, assuming that the
functions already have them.If you prefer not to include the text“autogenerated by docstring-gen
library”in the generated docstrings, you can use the--no-include-auto-gen-txtflag when running the command.docstring_gen{source_file_or_directory}-f--no-include-auto-gen-txtNow the docstring for the above function will look similar to:defconcatenate_strings(s1:str,s2:str)->str:"""Concatenate two strings.Args:s1: First strings2: Second stringReturns:The concatenated stringRaises:TypeError: If s1 or s2 is not a string"""ifnotisinstance(s1,str)ornotisinstance(s2,str):raiseTypeError("Both arguments should be strings.")returns1+s2Important: The library uses the text“autogenerated by
docstring-gen library”to identify which docstrings were generated by
the library. When the--no-include-auto-gen-txtflag is used, this
text will not be included in the generated docstrings. As a result, when
re-running the command with the-fflag, these docstrings will not be
replaced.”Alternatively, you can manually delete the“autogenerated by
docstring-gen library”(starting from the !!! note until the end) text
from the classes and functions for which you think the auto-generated
docstring is appropriate, and then re-run the command using the-fflag to update the remaining auto-generated docstrings.In addition to the-fand--no-include-auto-gen-txtflags, you can
also customize the behavior by adjusting other parameters such as--model,--temperature, etc., For more information on these options
and how to use them, please refer to theOpenAI’s
documentation.Jupyter notebook extensionWe have created a user-friendly notebook extension for the docstring-gen
library. This extension provides a convenient way to document your code
cell-by-cell, rather than having to document the entire notebook all at
once. To install the extension, simply run the following commands in
your terminal:Note: Please ensurejupyter-contrib-nbextensions.
is installed before installing the docstring-gen library extensionjupyternbextensioninstallhttps://github.com/airtai/jupyter-docstring-gen/archive/main.zip--user
jupyternbextensionenablejupyter-docstring-gen-main/jupyter-docstring-genAfter successful installation, you will see a new button on your jupyter
notebook toolbar. This button allows you to easily generate docstrings
for your Python code and improve your documentation.For more detailed information, please refer to thislink.CopyrightCopyright © 2023 onwards airt technologies ltd, Inc.LicenseThis project is licensed under the terms of theApache
License 2.0