Skip to main content

An automatic commenter for Python code.

Project description

Commentator

Commentator is a Python program that generates comments and optional translations for Python code. It uses OpenAI's GPT-3 language model to add high-level explanatory comments and docstrings to Python code.

PyPI Latest ReleaseDownloads Downloads Python versions

Usage

To use Commentator, you must first set up an OpenAI API key. If you already have an API key, you can set it as an environment variable called OPENAI_API_KEY. Otherwise, you can pass your API key as an argument to the commentator command. (If you do not have one yet, you can get a key here: https://openai.com/api/.)

$ export OPENAI_API_KEY=<your-api-key>

or

$ commentator --api-key <your-api-key> FILE1 ...

Commentator takes a path to a Python file and an optional language parameter. If language is specified, Commentator translates each docstring and comment in the code to the specified language. The default language is English.

Installation

To install Commentator, you can use pip:

$ pip install python-commentator

Example

Suppose you have a file called example.py with the following code:

def absolutely(n):
    if n < 0:
        return -n
    else:
        return n

Run Commentator on this file to add comments and type annotations:

$ commentator example.py

The resulting code will be:

def absolutely(n: int):
    """
    Return the absolute value of an integer.
    
    Args:
    n(int): Integer number.
    
    Returns:
    An absolute integer value of the input.
    """
    if n < 0:
        return -n
    else:
        return n

Note that Commentator has added a docstring and type annotations.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-commentator-0.2.0.tar.gz (13.5 kB view hashes)

Uploaded Source

Built Distribution

python_commentator-0.2.0-py3-none-any.whl (14.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page