Skip to main content

generate docstrings

Project description

Markata

Lockhart

PyPI - Version PyPI - Python Version


Table of Contents

Installation

pip install lockhart
## or with pipx
pipx install lockhart

Instructions

You will need an openai key, you can obtain one from their website. Once you have it then set your OPENAI_API_KEY environment variable.

export OPENAI_API_KEY='sk-***'

Lockhart it currently very crude, it works out of the clipboard. Copy a function to your clipboard, run lockhart docstring then paste the docstring in.

# copy a function
lockhart docstring
# paste in your docstring

yes it just uses the clipboard for now.

Docstring Examples

def add(a, b):
    """

    Add two numbers together.

    Args:
        a (int): The first number to add.
        b (int): The second number to add.

    Returns:
        int: The sum of the two numbers.

    Example:
        add(2, 3) -> 5

    """
    return a + b

Without even type hinting the arguments lockhart can see that this function is using pandas.

def get_summary_data(df):
    """
    This function takes a dataframe as an argument and returns a summary of the data grouped by column A.

    Args:
        df (DataFrame): The dataframe to be summarized.

    Returns:
        DataFrame: A summary of the data grouped by column A, containing the sum of columns C and D.

    Example:
        df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9], 'D': [10, 11, 12]})
        get_summary_data(df)
    """
    df.groupby("A")[["C", "D"]].sum()

Here is an example from the fastapi docs. It writes a correct docstring to describe the function, but misses out that its a fastapi route without more context than just the function.

@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
    """
    Update an item in the database.

    This function takes an item_id and an item object as parameters and updates the item in the database.

    Args:
        item_id (int): The id of the item to be updated.
        item (Item): The item object to be updated.

    Returns:
        dict: A dictionary containing the item_id and the updated item.

    Example:
        results = update_item(1, {'name': 'Foo', 'price': 10.99})
    """
    results = {"item_id": item_id, "item": item}
    return results

Examples Refactor

def add(a, b):
    return a + b

# refactor the following code to compute the ratio of the two numbers

def ratio(a, b):
    return a / b
# Before
def get_summary_data(df):
    df.groupby("A")[["C", "D"]].sum()

# refactor the following code to also return the averages

# After
def get_summary_data(df):
    df.groupby("A")[["C", "D"]].agg(["sum", "mean"])

Run lockhart refactor, then set the prompt to refactor the following code to accept a parameter item_name. It did a great job at adding it to the arguments list, type hinting it to a string, but again missed that this is a fastapi route and we wanted to also update the decorator.

``` python
# before
@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results

# refactor the following code to accept a parameter item_name

# after
@app.put("/items/{item_id}")
def update_item(item_id: int, item_name: str, item: Item):
    results = {"item_id": item_id, "item_name": item_name, "item": item}
    return results

License

lockhart is distributed under the terms of the MIT license.

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

lockhart-0.4.0.dev0.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lockhart-0.4.0.dev0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file lockhart-0.4.0.dev0.tar.gz.

File metadata

  • Download URL: lockhart-0.4.0.dev0.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.3

File hashes

Hashes for lockhart-0.4.0.dev0.tar.gz
Algorithm Hash digest
SHA256 baabc6ab84273ec92aff0df55a84972995bec3d8588a0e1fe49a4da01dabe898
MD5 78563c79faf94d6d42cf17e89b794189
BLAKE2b-256 da8cd90a61c345075d9887c28213ba9e74ddd49af3d681104a2e90b30a63d74d

See more details on using hashes here.

File details

Details for the file lockhart-0.4.0.dev0-py3-none-any.whl.

File metadata

File hashes

Hashes for lockhart-0.4.0.dev0-py3-none-any.whl
Algorithm Hash digest
SHA256 f91b6ba630a23922e82e3112de5290449fc30a8451fdc6cbccc92d7fa98095af
MD5 8fb3be62b455b495de617475a1de2dfe
BLAKE2b-256 2b9eeaea74bc2ff6c24d03902f32169950bcf7c684d075c2f9daa3eacf5a700f

See more details on using hashes here.

Supported by

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