diff --git a/document-python-code-with-chatgpt/README.md b/document-python-code-with-chatgpt/README.md new file mode 100644 index 0000000000..905694ee9b --- /dev/null +++ b/document-python-code-with-chatgpt/README.md @@ -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/). diff --git a/document-python-code-with-chatgpt/circle.py b/document-python-code-with-chatgpt/circle.py new file mode 100644 index 0000000000..d9940822e2 --- /dev/null +++ b/document-python-code-with-chatgpt/circle.py @@ -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) diff --git a/document-python-code-with-chatgpt/code_explanation.py b/document-python-code-with-chatgpt/code_explanation.py new file mode 100644 index 0000000000..279269ff15 --- /dev/null +++ b/document-python-code-with-chatgpt/code_explanation.py @@ -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) +""" diff --git a/document-python-code-with-chatgpt/docstring_1.py b/document-python-code-with-chatgpt/docstring_1.py new file mode 100644 index 0000000000..110858255a --- /dev/null +++ b/document-python-code-with-chatgpt/docstring_1.py @@ -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 diff --git a/document-python-code-with-chatgpt/docstring_2.py b/document-python-code-with-chatgpt/docstring_2.py new file mode 100644 index 0000000000..b576d82858 --- /dev/null +++ b/document-python-code-with-chatgpt/docstring_2.py @@ -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 diff --git a/document-python-code-with-chatgpt/doctest_tests.py b/document-python-code-with-chatgpt/doctest_tests.py new file mode 100644 index 0000000000..4430d9a6bf --- /dev/null +++ b/document-python-code-with-chatgpt/doctest_tests.py @@ -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 diff --git a/document-python-code-with-chatgpt/interval.py b/document-python-code-with-chatgpt/interval.py new file mode 100644 index 0000000000..168114869f --- /dev/null +++ b/document-python-code-with-chatgpt/interval.py @@ -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. + """ diff --git a/document-python-code-with-chatgpt/prime.py b/document-python-code-with-chatgpt/prime.py new file mode 100644 index 0000000000..f27ede4273 --- /dev/null +++ b/document-python-code-with-chatgpt/prime.py @@ -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. + """ diff --git a/document-python-code-with-chatgpt/usage_examples.py b/document-python-code-with-chatgpt/usage_examples.py new file mode 100644 index 0000000000..3db413ab8c --- /dev/null +++ b/document-python-code-with-chatgpt/usage_examples.py @@ -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 +"""