Task 2: Do Examples from Book
Objective
To learn the basics of Prompt Engineering using Copilot.
My focus here is on:
- The prompts, not Python specifics
- Gotchas, tips, and tricks using VS Code and Copilot
For clear, complete, and concise specifics on how to do the examples, buy this book.
The Book
Learn AI-Assisted Python Programming with GitHub Copilot and Chat GPT Leo Porter & Daniel Zingaro, Manning Publications (2024)
the Coding Activity below is taken from the book's Aaron Rogers football example. See pp. 22. the Testing Activity below continues the Aaron Rogers football example. See pp. 116.
Activity 1. Coding
Prompt 1.1 Read the file
I did this in a new Python file, football01.py
.
The prompt I entered was:
open the csv file called “nfl_offensive_stats.csv” and read in the csv data from that file
Copilot suggested 10 solutions.I picked:
"""
open the csv file named "nfl_offensive_stats.csv" and read in the csv data from that file
"""
import csv
with open("nfl_offensive_stats.csv", "r") as nfl_offensive_stats:
nfl_offensive_stats_reader = csv.reader(nfl_offensive_stats)
for row in nfl_offensive_stats_reader:
print(row)
This code ran correctly in VS Code without any editing. Note: Copilot gratuitously added the print statement. Note: Copilot failed to include a close() statement.
Prompt 1.2 Filter on name and passing yards
I did this in a new Python file, football02.py
.
I did this after I ran the previous Python code.
The prompt I entered was:
In the data we just read in, the fourth column is the player and the 8th column is the passing yards.
Get the sum of yards from column 8 where the 4th column value is "Aaron Rodgers"
Copilot suggested 10 solutions.I picked:
"""
In the data we just read in, the fourth column is the player and the 8th column is the passing yards.
Get the sum of yards from column 8 where the 4th column value is "Aaron Rodgers"
"""
import csv
sum = 0
with open("nfl_offensive_stats.csv") as file:
reader = csv.reader(file)
for row in reader:
if row[3] == "Aaron Rodgers":
sum += int(row[7])
print("Aaron Rodgers passing yards: ", sum)
This code ran correctly in VS Code without any other editing.
Prompt 1.3 Filter on Aaron Rogers
I did this in a new Python file, football03.py
.
I did this after I ran the previous Python code.
The prompt I entered was:
XXX
Copilot suggested 10 solutions.I picked:
YYY
ZZZ
Prompt 1.4 Filter on just Quarterbacks
Lorem ipsum
Prompt 1.5 Filter out Brady
Lorem ipsum
Prompt 1.6 Print sums sorted descending
Lorem ipsum
Prompt 1.7 Plot sums sorted ascending
Lorem ipsum
Tips:
- In Visual Studio Code, always Open the workspace folder first. Else your Python code can't easily find the CSV file.
-
Toggling-on
List Suggestions
(Ctl+Enter) is faster than stepping through inline Suggestions. -
When toggling-on
List Suggestions
, place your cursor outside of the docstring, into VS Code's Enter Code Here field. Else Copilot's Suggestions will be comments and other wonkiness. -
Repeatedly toggling
List Suggestions
on and off gives you alternate sets of suggested solutions. But they're each in a new Tab, which can get numerous. - Inline Suggestions can sometimes be bothersome. To toggle them on and off, enter Ctl+Alt+\.
Conclusions:
The above is Iterative Development using Prompt Engineering.
Activity 2. Testing
Prompt 1. YYY
Lorem ipsum
Activity XXX
Prompt 1. YYY
Lorem ipsum