Another GitHub Copilot Blog Post

Jacob Russell
5 min readNov 21, 2021

--

I was on Twitter the other day and I saw someone tweet about GitHub copilot. I can’t remember what they said, who said it, or really when I read it, but I remember thinking to myself: “I need to go sign up for that!” So off I went to https://copilot.github.com. And what was I greeted with?:

I completely forgot I signed up for the technical preview four months ago after reading someones Slack message when the early access list had just opened. So off I skipped to VS Code and installed the extension.

After I installed the extension I felt a little like a dog who finally caught the car he was chasing — I didn’t really know what to do with it. I started off by trying something I saw in the documentation (I’ll be using mostly python through this post by the way):

# I typed the function header without parameters def calculate_days_between_dates

and it very quickly prompted me to review this function:

# Copilot filled out the rest 
def calculate_days_between_dates(year1, month1, day1, year2, month2, day2):
"""
Calculates the number of days between two dates.
"""
assert year1 > 0, "Year 1 must be a positive number."
assert year2 > 0, "Year 2 must be a positive number."
assert month1 > 0 and month1 <= 12, "Month 1 must be between 1 and 12."
assert month2 > 0 and month2 <= 12, "Month 2 must be between 1 and 12."
assert day1 > 0 and day1 <= 31, "Day 1 must be between 1 and 31." assert day2 > 0 and day2 <= 31, "Day 2 must be between 1 and 31."
date1 = datetime.date(year1, month1, day1)
date2 = datetime.date(year2, month2, day2)
return abs(date2 - date1).days

I did find my mouth somewhat agape. I’ve seen the demos, read the articles, and sorted through all the various takes, but something about seeing it in my own editor was just different, as cheesy as that sounds. I felt like VS Code was looking at me through my webcam a little bit.

My next thought was “maybe this will help my finally stop Googling literally anything about regex,” so I decided to make my dreams a reality and also try the comment functionality:

# I typed out a doc string 
"""
Use regex to find a five digit number within a string.
"""
# and Copilot filled out the function
def get_store_number(string):
import re
return re.findall(r'\\d{5}', string)

I might be easily pleased, but I love this. Another way I found copilot helpful is generating some sample data. I was working on something where I needed to test out some logic before I connected to an external API, and I needed an array of dictionaries representing some information about books:

# I typed out the following doc string 
"""
An array of dictionaries with the following keys: title, author, number_of_highlights.
"""
# Copilot created the following list
books = [
{
"title":"The Great Gatsby",
"author":"F. Scott Fitzgerald",
"number_of_highlights":10
},
{
"title":"I Capture The Castle",
"author":"James Patterson",
"number_of_highlights":2
},
{
"title":"The Shining",
"author":"Stephen King",
"number_of_highlights":15
},
{
"title":"The Da Vinci Code",
"author":"Dan Brown",
"number_of_highlights":1
},
{
"title":"Angels & Demons",
"author":"Dan Brown",
"number_of_highlights":9
},
{
"title":"The Lost Symbol",
"author":"Dan Brown",
"number_of_highlights":2
},
{
"title":"The Alchemist",
"author":"Paulo Coelho",
"number_of_highlights":0
},
{
"title":"The Way of Kings",
"author":"Brandon Sanderson",
"number_of_highlights":11
},
{
"title":"The Name of the Wind",
"author":"Patrick Rothfuss",
"number_of_highlights":7
},
{
"title":"Lord of the Flies",
"author":"William Golding",
"number_of_highlights":4
},
{
"title":"The Hobbit",
"author":"J. R. R. Tolkien",
"number_of_highlights":6
},
{
"title":"The Hunger Games",
"author":"Suzanne Collins",
"number_of_highlights":14
}
]

I’ve shown Copilot to a few other engineers and whenever I demo this particular use case I’m usually always met with a “wow that would have actually been pretty helpful when I did X the other day.” I frequently find myself context switching to think of some examples to test a function I just wrote, so it’s nice when Copilot can stub out a quick test for me or create some sample data.

I’ve also found Copilot helpful when interacting with popular libraries like boto3. I wanted to retrieve a text file from an S3 bucket, and before I knew it I had tabbed my way to a working example:

# I started off with a log statement 
logger.info(f"Getting file from s3 by key")
# and Copilot sugested the following
s3 = boto3.client("s3", config=Config(signature_version="s3v4")) response = s3.get_object(Bucket="my-bucket-name", Key=s3_key) file_content = response["Body"].read()
logger.info(f"Decoding file content")
file_content = file_content.decode("utf-8")

All I typed was the first logger. I thought to myself, “oh yeah… I forgot how you had to .read() the ["Body"] and totally forgot that I had to .decode("utf-8") after loading the file in memory." For me, that's where GitHub Copilot shines - those little "oh yeah!" moments, or for quickly generating some boilerplate that I forgot how to do, or for creating some sample objects when I don't want to think of 10 book names and their metadata. For a guy who pretty regularly forgets how to use the functions in the libraries I use almost everyday I'm pretty pumped about this.

A great example of this was when I was just doing some debugging and investigation in a project a few days ago. I needed to translate an epoch timestamp to a more human readable format. I was about to head over to google when I thought I would try this out:

# I wrote the following doc string 
"""
Returns datetime from a given epoch time.
"""
# Copilot suggested the following function
def epoch_to_datetime(epoch):
return datetime.datetime.fromtimestamp(epoch).strftime('%Y-%m-%d %H:%M:%S')
if __name__ == "__main__":
# using the function above last_seen = epoch_to_datetime(my_obj["lastSeen"]) print(last_seen)

The code above didn’t make it to production, it didn’t even last long in my editor, but it let me keep moving and stay in the flow without having to context switch. That’s the real power of Copilot for me right now. I wouldn’t suggest using Copilot to create a function for storing your user password or even using the generated code without treating it like you would if you found it on StackOverflow, but I’m still really excited to see how Copilot continues to get better and how it improves my productivity in the future.

Originally published at https://jacobhrussell.com.

--

--

Jacob Russell
Jacob Russell

Written by Jacob Russell

Software Engineer in the Metro Atlanta Area

No responses yet