Package: gpt4docstrings
gpt4docstringsGenerating Python docstrings with OpenAI ChatGPT!!What isgpt4docstrings?gpt4docstringsis a library that helps you to write docstrings
for your Python code. Select a path / paths where you wantgpt4docstringsto be applied and wait for the results!!Requirementsgpt4docstringssupports Python 3.9 and above.InstallationWarning:At the moment, this library is under heavy development, so it is recommended to always install
the latest version.You can installgpt4docstringsviapipfromPyPI:$pipinstall-Ugpt4docstringsUsageTo rungpt4docstringson a specific file, run this command.gpt4docstringsmy_file.pyRemember that, if you don't have your OpenAI API Key defined as an Environment Variable (OPENAI_API_KEY),gpt4docstringscan accept the API Key as an option.gpt4docstrings--api_keysk-xxxxxxxxxxxxmy_file.pyBe aware that, as a safety measure ,gpt4docstringswon't overwrite the file in place.
Instead, it will generate apatchcalledgpt4docstring_docstring_generator_patch.diffthat contains the information about all the changes.If you want to apply thepatchto the file, simply run:patch-p1<gpt4docstring_docstring_generator_patch.diffIn case you don't want to generate apatchfile and modify the files in place, you'll
need to add the--overwrite, -woption:gpt4docstrings-wmy_file.pyYou can also applygpt4docstringsto folders recursively.gpt4docstringssrc/Another quite common situation is that you may want to exclude thetests/folder, for example,
from the generation of docstrings. Doing this is very simple.gpt4docstrings--excludetests/src/By default,gpt4docstringsgeneratesgooglestyle docstrings. However,
you can choose between:google,numpy,epytext,reStructuredText.You can specify the docstring style using the-stoption. For example:gpt4docstrings-stepytextmy_file.pyFor more information about all the available options, you can check
thehelpinfo:Usage: gpt4docstrings [OPTIONS] [PATHS]...

Options:
  -h, --help                      Show this message and exit.
  -S, --ignore-setters            Ignore methods with property setter
                                  decorators.
  -P, --ignore-property-decorators
                                  Ignore methods with property setter/getter
                                  decorators.
  -n, --ignore-nested-functions   Ignore nested functions and methods.
  -C, --ignore-nested-classes     Ignore nested classes.
  -i, --ignore-init-method        Ignore `__init__` method of classes.
  -s, --ignore-semiprivate        Ignore semiprivate classes, methods, and
                                  functions starting with a single underscore.
  -p, --ignore-private            Ignore private classes, methods, and
                                  functions starting with two underscores.
                                  [default: False]
  -w, --overwrite                 If `True`, it will directly write the
                                  docstrings into the files (it will not
                                  generate git patches)
  -v, --verbose INTEGER           Verbosity parameter. Defaults to 0.
  -e, --exclude PATH              Exclude PATHs of files and/or directories.
                                  Multiple `-e/--exclude` invocations
                                  supported.
  -k, --api_key TEXT              OpenAI's API key. If not provided,
                                  `gpt4docstrings` will try to access
                                  `OPENAI_API_KEY` environment variable.
  -st, --style TEXT               Docstring style, which must be one of
                                  'google', 'reStructuredText', 'epytext',
                                  'numpy'
  -m, --model TEXT                The model to be used by `gpt4docstrings`. By
                                  default, `gpt-3.5-turbo`.I also encourage you to see theCommand-line Referencefor more details!!ExampleHere is a full example usinggpt4docstringto generate docstrings
for the python code insideexample/example.py.importasyncioasyncdefasync_example():awaitasyncio.sleep(2)classMyClass:def__init__(self,value):self.value=value@staticmethoddefnested_method():definner_function():print("Nested method inner function")print("Nested method start")inner_function()print("Nested method completed")We'll createnumpydocstrings in this case and will generate a patch file (default) instead of directly overwriting
the file directly. We'll also increase the level of verbosity to see some additional information.Warning:We are assuming you already have the OpenAI API Key set as an Environment Variable. Otherwise, this
example won't work.gpt4docstrings example/example.py -v 1 -st numpyAfter it finishes documenting, we should see a new patch file on our directory calledgpt4docstring_docstring_generator_patch.diff.To apply the patch, simply run:patch-p1<gpt4docstring_docstring_generator_patch.diffThe result should be similar to the following (gpt-3.5-turbotemperature
is set to 1, so you should expect different results every time you run the command)importasyncioasyncdefasync_example():"""An asynchronous example function.This function asynchronously sleeps for 2 seconds.Returns-------NoneThis function does not return any value."""awaitasyncio.sleep(2)classMyClass:"""A class representing MyClass.Parameters----------value : anyThe initial value for MyClass.Methods-------nested_method : static methodA nested static method within MyClass."""def__init__(self,value):"""Initialize a new instance of the class.Parameters----------value : anyThe initial value for the instance.Returns-------NoneRaises------None"""self.value=value@staticmethoddefnested_method():"""This static method demonstrates a nested method.Raises------NoneReturns-------None"""definner_function():"""Inner function.This function performs a nested method inner function.Parameters----------NoneReturns-------None"""print("Nested method inner function")print("Nested method start")inner_function()print("Nested method completed")Suppose now that we want to modify the docstring type. For example,
suppose we want to useepytextstyle. That's very easy withgpt4docstrings, we just need to run the same command changing the docstring style.Be aware that, by default,gpt4docstringswill allways generate
docstrings for undocumented functions / classes and also translate the existing
ones to follow the provided style. If you just want to generate docstrings (no translation),
simply set the-tflag toFalse.gpt4docstringsexample/example.py-stepytextIf we apply the patch, we'll get the previous code with the
docstrings translated:importasyncioasyncdefasync_example():"""An asynchronous example function.This function asynchronously sleeps for 2 seconds.@rtype: None@return: This function does not return any value."""awaitasyncio.sleep(2)classMyClass:"""A class representing MyClass.@type value: any@ivar value: The initial value for MyClass.@type nested_method: static method@ivar nested_method: A nested static method within MyClass."""def__init__(self,value):"""Initialize a new instance of the class.@param value: The initial value for the instance.@type value: any@return: None@raise None"""self.value=value@staticmethoddefnested_method():"""This static method demonstrates a nested method.@rtype: None@return: None@raise: None"""definner_function():"""Inner function.This function performs a nested method inner function.@rtype: None"""print("Nested method inner function")print("Nested method start")inner_function()print("Nested method completed")ContributingContributions are very welcome.
To learn more, see theContributor Guide.LicenseDistributed under the terms of theMIT license,gpt4docstringsis free and open source software.IssuesIf you encounter any problems,
pleasefile an issuealong with a detailed description.CreditsThis project was generated from@cjolowicz'sHypermodern Python Cookiecuttertemplate.