Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample code for the article on documenting code with ChatGPT #446

Merged
merged 6 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions document-python-code-with-chatgpt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Document Your Python Code and Projects With ChatGPT

This folder provides the code examples for the Real Python tutorial [Document Your Python Code and Projects With ChatGPT](https://realpython.com/document-python-code-with-chatgpt/).
56 changes: 56 additions & 0 deletions document-python-code-with-chatgpt/circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""Prompt
Write Numpy-style docstrings for the following class and its methods:

class Circle:
def __init__(self, radius):
self.radius = radius

def calculate_area(self):
return round(math.pi * self.radius ** 2, 2)
"""

import math


# Output:
class Circle:
"""
A Circle class to perform some circle-related operations.

Parameters
----------
radius : float
The radius of the circle.

Attributes
----------
radius : float
The radius of the circle.

Methods
-------
calculate_area():
Calculates the area of the circle.
"""

def __init__(self, radius):
"""
Constructs all the necessary attributes for the circle object.

Parameters
----------
radius : float
The radius of the circle.
"""
self.radius = radius

def calculate_area(self):
"""
Calculate the area of the circle.

Returns
-------
float
The area of the circle rounded to 2 decimal places.
"""
return round(math.pi * self.radius**2, 2)
9 changes: 9 additions & 0 deletions document-python-code-with-chatgpt/code_explanation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Prompt
Explain how the following function works:

def find_extremes(iterable):
data = tuple(iterable)
if len(data) == 0:
raise ValueError("input iterable must not be empty")
return min(data), max(data)
"""
12 changes: 12 additions & 0 deletions document-python-code-with-chatgpt/docstring_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
""""Prompt
Write a single-line docstring for the following function:

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


# Output:
def add(a, b):
"""Return the sum of a and b."""
return a + b
71 changes: 71 additions & 0 deletions document-python-code-with-chatgpt/docstring_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""Propmt
Write a Google-style docstring for the following function:

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


# Output:
def add(a, b):
"""Sum two numbers.

Args:
a (int or float): The first number.
b (int or float): The second number.

Returns:
int or float: The sum of the two numbers.
"""
return a + b


"""Prompt
Write a Numpy-style docstring for the following function:

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


# Output:
def add_(a, b):
"""
Sum two numbers.

Parameters
----------
a : int or float
The first number to be added.
b : int or float
The second number to be added.

Returns
-------
int or float
The sum of a and b.
"""
return a + b


"""Prompt
Write a Sphinx-style docstring for the following function:

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


# Output:
def add__(a, b):
"""
Calculate the sum of two numbers.

:param a: The first number.
:type a: int or float
:param b: The second number.
:type b: int or float
:return: The sum of the two numbers.
:rtype: int or float
"""
return a + b
39 changes: 39 additions & 0 deletions document-python-code-with-chatgpt/doctest_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Prompt
Write a Numpy-style docstring for the following function.
Add a section with usage examples as doctest tests:

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


# Output:
def add(a, b):
"""
Add two numbers together.

This function takes two numbers (either integers or floats)
as arguments and returns their sum.

Parameters
----------
a : int or float
The first number to add.
b : int or float
The second number to add.

Returns
-------
int or float
The sum of the two input numbers.

Examples
--------
>>> add(1, 2)
3
>>> add(5.5, 2.5)
8.0
>>> add(100, -50)
50
"""
return a + b
30 changes: 30 additions & 0 deletions document-python-code-with-chatgpt/interval.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Prompt
Write a Google-style docstring for a function with the following signature:

def in_interval(number, start, end):
"""


# Output:
def in_interval(number, start, end):
"""
Checks if a given number is within a specified interval.

This function takes three arguments: a number, and the start
and end points of an interval.
It checks whether the given number falls within the specified
interval or not.

Args:
number (int or float): The number to check.
start (int or float): The starting point of the interval.
end (int or float): The ending point of the interval.

Returns:
bool: True if the given number falls within the interval,
False otherwise.

Raises:
ValueError: If the start is larger than the end, or if
the number, start, or end are not numbers.
"""
27 changes: 27 additions & 0 deletions document-python-code-with-chatgpt/prime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Prompt
Write a Google-style docstring for a function that takes an
integer as an argument and determines whether it’s a primary
number.
"""


# Output:
def is_primary_number(n):
"""
Determines if an input integer is a primary number.

This function takes an integer as an argument and checks
whether it is a primary number. A primary number is a
positive natural number greater than 1 that has no positive
integer divisors other than 1 and itself.

Args:
n (int): The integer to check.

Returns:
bool: True if the input integer is a primary number,
False otherwise.

Raises:
ValueError: If n is less than or equal to 1.
"""
9 changes: 9 additions & 0 deletions document-python-code-with-chatgpt/usage_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Prompt
Write three usage examples for the following function.
Add explanations to each example:

def is_divisible(a, b):
if not a % b:
return True
return False
"""
Loading