An automatic commenter for Python code, with optional added translations.
Project description
Commentaire
Commentaire 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.
Usage
To use Commentaire, 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 commentaire
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
$ commentaire <file> <api-key>
Commentaire takes a path to a Python file and an optional language parameter. If language is specified, Commentaire translates each docstring and comment in the code to the specified language and includes the translated text in the output. If language is not specified, Commentaire does not include any translations in the output.
Installation
To install Commentaire, you can use pip:
$ pip install commentaire
Functionality
Commentaire has a single function, process
, which takes in a string
of Python code and an optional language parameter. If language is
specified, the function translates each docstring and comment in the
code to the specified language and includes the translated text in the
output. If language is not specified, the function does not include
any translations in the output. The output text includes the original
code, high-level explanatory comments, and any translated text (if
language is specified).
The commentaire
command uses process
to add comments and
translations to Python code in a file. It processes each file in a
list of files passed as arguments to the commentaire
command.
Example
Suppose you have a file called example.py
with the following code:
def foo(x):
y = x + 1
return y
You can run Commentaire on this file to add comments (and optionally, translations to another language):
$ commentaire example.py --language Spanish
The resulting code will be:
"""
This function takes in a value x and returns its incremented value.
Esta función toma un valor x y devuelve su valor incrementado.
"""
def foo(x):
y = x + 1
return y
Note that Commentaire has added high-level explanatory comments and translated the existing docstring and comment to Spanish.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for commentaire-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a53f892078bd345564a8cfd236c5d482a3c22527720de52787a40d09bac1e2e8 |
|
MD5 | 4dbde3d0268c2fa07e7b698d710a3c8e |
|
BLAKE2b-256 | 8fcafe94e40b21b3be967b6588e96f4369b120c778ea469950bcc60c15547a6d |