Skip to main content

Pythoness

Pythoness

Pythoness: The priestess of the oracle of Apollo at Delphi.

by Emery Berger

PyPI Latest ReleaseDownloads Downloads Python versions

Pythoness automatically generates Python code from natural language descriptions and tests.

Note

Pythoness needs to be connected to an OpenAI account. Your account will need to have a positive balance for this to work (check your balance). If you have never purchased credits, you will need to purchase 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) in order to have access to GPT-4, which Pythoness uses. Get a key here.

Once you have an API key, set it as an environment variable called OPENAI_API_KEY.

export OPENAI_API_KEY=<your-api-key>

Installation

The easiest way to install Pythoness is through pip:

python3 -m pip install pythoness

Usage

To use Pythoness, you just import the pythoness module and then use the @pythoness.spec decorator to specify the desired functionality:

import pythoness

@pythoness.spec("Compute the nth number in the Fibonacci series.")
def myfib(n: int) -> int:
    ""

This code will internally generate a Python function named myfib that computes the nth number in the Fibonacci series. To actually execute the function, you can call it as you would any other Python function:

for i in range(20):
    print(myfib(i))

Pythoness caches the results of translating natural language to Python, so subsequent executions in the same directory will run much faster (Pythoness creates a database called pythoness-cache.db that saves these translations).

Incorporating tests

You can guide Pythoness by providing some tests. Pythoness will use tests both to generate the Python code and to validate it. Tests are just a list of strings containing Python code which should all evaluate to True.

@pythoness.spec("Compute the nth number in the Fibonacci series.",
                tests=["myfib(1) == 1", "myfib(2) == 1"])
def myfib(n: int) -> int:
    ""

You can also guide Pythoness with property-based tests. To do this, describe the properties that you want your program to exhibit. Pythoness will run a property-based tester (Hypothesis), which will perform tests many times to ensure that the generated function meets the specified properties. This approach is much more powerful than the unit tests described above.

@pythoness.spec("Compute the nth number in the Fibonacci series.",tests = [({'n':'integers(1,20)'}, "myfib(n+2) == myfib(n+1)+myfib(n)")])
def myfib(n: int) -> int:
    ""

Replacing Pythoness functions with Python

You can have Pythoness to replace the spec directly in your file with the generated function: just add replace=True:

@pythoness.spec("Compute the nth number in the Fibonacci series.",
                tests=["myfib(1) == 1", "myfib(2) == 1"],
		replace=True)
def myfib(n: int) -> int:
    ""

For example, Pythoness produced this code:

def myfib(n: int) -> int:
    """
    Compute the nth number in the Fibonacci series.

    :param n: The position of the desired number in the Fibonacci series
    :type n: int
    :return: The nth number in the Fibonacci series
    :rtype: int
    """
    if n <= 0:
        raise ValueError("n must be a positive integer")
    elif n == 1 or n == 2:
        return 1
    else:
        fib1, fib2 = 1, 1
        for _ in range(3, n + 1):
            fib1, fib2 = fib2, fib1 + fib2
        return fib2

Other ways to control Pythoness

Pythoness offers a few other ways to control its behavior. These are all arguments to spec. The provided values indicate the the default value.

  • max_retries=3: controls the maximum number of retries due to failures (e.g., a function that fails one of the provided tests).

  • min_confidence=0.7: sets the minimum level of confidence that the AI system reports regarding the correctness of the generated function (a number between 0 and 1, corresponding to 0% and 100%). Increasing the confidence level may require increasing max_retries.

  • verbose=False: set this to True to have Pythoness to output details as it generates and validates code. Mostly useful for developers and for keeping tabs on progress.

  • output=False: set this to True to have Pythoness output the generated code the first time the function is called.

Metadata

Release files for pythoness 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pythoness 0.1.0
File Size Uploaded Provenance Actions
pythoness-0.1.0.tar.gz 13.1 kB Without provenance Details for pythoness-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pythoness 0.1.0
File Interpreter ABI Platform Actions
pythoness-0.1.0-py3-none-any.whl Python 3 none any Details for pythoness-0.1.0-py3-none-any.whl

Total release size: 24.4 kB

Release files / pythoness-0.1.0.tar.gz

Download URL pythoness-0.1.0.tar.gz
Size 13.1 kB
Tags Source
SHA-256 checksum
How to use checksums
f3c5c8a582308820bc7aa5b45a361033980cefa55204bc93a5f92ec6c99a3123
BLAKE2b-256 checksum
How to use checksums
8308ae6fdee53370f3f0ad90a086163b16c24fdb81ed7b7267fb850a238ce756
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.11

Release files / pythoness-0.1.0-py3-none-any.whl

Download URL pythoness-0.1.0-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
16147a737de9e105822539bced6e2a7294b694048ef762bc626edfbf3757c63a
BLAKE2b-256 checksum
How to use checksums
f272d5fca600a6b4096936d5afc5774a77e42e22c5e189355e81f219640a9d8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.11

Security for pythoness 0.1.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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page