Feedback

Chat Icon

Building with GitHub Copilot

From Autocomplete to Autonomous Agents

Smart Inline Completions: Use Cases, Examples, and Exercises
29%

From Code to Documentation

We have seen how to generate code from comments and docstrings, but the reverse is also entirely possible. If you have code and you want to describe it, you can use the code as an input.

To understand how this works, use the following code snippet and copy it into a file:

import multiprocessing

def worker(x, y, output_queue):
    result = x * y
    output_queue.put((x, y, result))

def multiprocess(number_pairs):

    output_queue = multiprocessing.Queue()
    processes = []

    for x, y in number_pairs:
        process = multiprocessing.Process(target=worker, args=(x, y, output_queue))
        processes.append(process)
        process.start()

    for process in processes:
        process.join()

    results = []
    while not output_queue.empty():
        results.append(output_queue.get())

    return results

if __name__ == '__main__':
    number_pairs = [(1, 2), (2, 3), (3, 4)]
    results = multiprocess(number_pairs)
    print(results)

Let's say that we don't understand what this code does. Copilot can help us generate a docstring that provides more context about the code. All you need to do is place the cursor at different locations in the file, give Copilot a hint (for example, start typing """ to trigger the docstring suggestion or add a # before a line of code to trigger the comment suggestion), and wait for the suggestion.

"""
This application is a simple example of how to use the multiprocessing module in Python.
The application creates a worker function that receives two numbers and returns the product of them.
The main function creates a list of number pairs and uses the multiprocess function to calculate the product of each pair.
The multiprocess function creates a process for each number pair and uses a queue to store the results.
<-- [Add a new line after each generated line, wait for the suggestion, when the copilot is done, remove the empty line]
"""

import multiprocessing

# [press ENTER after the 'def worker(x,y,output_queue)' line. Add a `"""`, press ENTER and wait for the suggestion]
def worker(x, y, output_queue):
    """
    Worker function that receives two numbers and returns the product of them.
    The result is stored in a queue.

    [Add a new line and wait for the suggestion]
    :param x: The first number
    :param y: The second number
    :param output_queue: The queue to store the result
    :return: None
    """
    result = x * y
    output_queue.put((x, y, result))

# [Repeat the same actions for the rest of the code]
def multiprocess(number_pairs):
    """
    Spawns multiple processes to multiply pairs of numbers concurrently.

    :param number_pairs: A list of number pairs to multiply
    :return: A list of tuples containing the input numbers and their product
    """

    #[Add a "#" on top of each line and wait for the suggestion]
    # Create a multiprocessing Queue to collect results
    output_queue = multiprocessing.Queue()

    # List to keep track of processes
    processes = []

    # Create a process for each pair of numbers
    for x, y in number_pairs:
        process = multiprocessing.Process(target=worker, args=(x, y, output_queue))
        processes.append(process)
        process.start()

    # Wait for all processes to complete
    for process in processes:
        process.join()

    # Collect all results from the queue
    results = []
    while not output_queue.empty():
        results.append(output_queue.get

Building with GitHub Copilot

From Autocomplete to Autonomous Agents

Enroll now to unlock all content and receive all future updates for free.