Commentator
by Emery Berger
Commentator leverages large language models to add high-level explanatory comments, docstrings, and types to Python code.
Usage
Commentator needs to be connected to an OpenAI account or an Amazon Web Services account.
OpenAI
Your account will need to have a positive balance for this to work (check your OpenAI balance). Get an OpenAI key here.
Commentator currently defaults to GPT-4, and falls back to GPT-3.5-turbo if a request error occurs. For the newest and best model (GPT-4) to work, you need to have purchased at least $1 in credits (if your API account was created before August 13, 2023) or $0.50 (if you have a newer API account).
Once you have an API key, set it as an environment variable called
OPENAI_API_KEY.# On Linux/MacOS: export OPENAI_API_KEY=<your-api-key> # On Windows: $env:OPENAI_API_KEY=<your-api-key>Amazon Bedrock
Commentator now supports Amazon Bedrock, using the Claude model. To use Bedrock, you need to set three environment variables.
# On Linux/MacOS: export AWS_ACCESS_KEY_ID=<your-access-key> export AWS_SECRET_ACCESS_KEY=<your-secret-key> export AWS_REGION_NAME=<your-region>If you do not already have access keys, you should be able create them by modifying this link with your own username and region:
https://us-east-1.console.aws.amazon.com/iam/home?region=us-east-1#/users/details/YOUR_USER_NAME?section=security_credentialsYou also need to request access to Claude (change region as needed):
https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/modelaccessCommentator will automatically select which AI service to use (OpenAI or AWS Bedrock) when it detects that the appropriate environment variables have been set.
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 might be:
def absolutely(n: int) -> int:
"""
Return the absolute value of a number.
Args:
- n (int): the number whose absolute value we want to find
Returns:
- int: the absolute value of n
"""
if n < 0:
# If n is negative
# Return the negative version of n (i.e. its absolute value)
return -n
else:
# Otherwise (if n is non-negative)
# Return n as-is (it already is its absolute value)
return n
Note that Commentator has added a docstring and type annotations.
Metadata
Release files for python-commentator 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| python-commentator-0.3.0.tar.gz | 19.5 kB | Without provenance | Details for python-commentator-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| python_commentator-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for python_commentator-0.3.0-py3-none-any.whl |
Total release size: 39.6 kB
Release files / python-commentator-0.3.0.tar.gz
| Download URL | python-commentator-0.3.0.tar.gz |
|---|---|
| Size | 19.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
66f4e3fd03f01b5d3a8190d701c8dd86f5f9d92e962cedec16029d3c64e1b437
|
|
BLAKE2b-256 checksum How to use checksums |
877dec8dfe365bbac483256bf5cd26ac3d606d72708a381601dc019776dbc4a6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.8
|
Release files / python_commentator-0.3.0-py3-none-any.whl
| Download URL | python_commentator-0.3.0-py3-none-any.whl |
|---|---|
| Size | 20.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
08ed579c625993ce8f8956008cf44c11453774c59692663f5c281b739b187d25
|
|
BLAKE2b-256 checksum How to use checksums |
69cd0d5c8c017f22522328c2e0a63667f61045580de4986575407e3a8aef560c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.8
|
Security for python-commentator 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.