Linux Format

Your coding copilot

Where we’re going, we don’t need licences!

-

We are now turning our attention to Microsoft’s

GitHub Copilot, which Microsoft describes as “Your AI pair programmer”. The idea of Copilot is that can detect the context of the code you are writing, including imported libraries, language used and recently written code. Using all of this informatio­n, Copilot can suggest code samples that can be used to write the program you are working on. GitHub has an enormous repository of code and

Copilot is trained on a huge sample from this source.

Before writing some code, we need to sign up for a free trial and configure VS Code accordingl­y. To sign up for a free account, open your favourite browser and navigate to https://github.com/features/copilot.

Use the Start My Free Trial button and follow the steps on the website to sign up. Please note that during the signup process, you are required to enter a billing address and payment details. If you choose to sign up, ensure that you cancel before the end of the free period to ensure you won’t be charged. Your billing details and subscripti­ons can be managed at https:// github.com/settings/billing.

Take off with Copilot

Now that you’ve signed up for your free trial, open

VS Code and go to the extensions tab on the left-hand side. Search for GitHub Copilot and use the Install button to install the extension.

Once installed, a message shows in the bottomrigh­t of the screen asking you to sign in to your GitHub account. Follow the prompts to sign in and then create a new Python file called copilot.py.

As you begin typing code, Copilot works in the background to make suggestion­s. Suggested code is shown in grey and can be accepted by hitting Tab. This is similar to the way email suggestion­s are displayed on the screen in Gmail. For the first basic example, let’s write a simple piece of code that requests the user’s name and enables them to try to guess the random number that has been generated. Feedback is provided to help the user guess the correct number.

Looking at the code sample (below-left), what is happening here is that we are first of all importing the libraries that we wish to use. The second thing we are doing is setting some variables to store the name of the user and the number that is being generated for the game player to guess. The keepGuessi­ng variable is used to determine when we should break out from the loop. All of the code in grey is suggested by Copilot and can be accepted by pressing the Tab key. Analysing this code shows that there is an unnecessar­y while loop being suggested. The first three suggested lines should be outside of the first while loop and the other lines then do not need to be nested within the second while

loop. Make this change and then test the code again.

What this code also doesn’t do is provide for the loop being broken out of and a success message being printed when the answer has been guessed. To rectify this, add a numGuesses = 0 line in the variables section and then after the guess = int(input()) / start to enter numGuesses . Accept the suggestion of numGuesses += 1

and then move to the end of the code sample. In line with elif enter else: and suggest Copilot’s

suggestion of printing a congratula­tions message. If it is not included, also add a line to exit the

while loop once the guess has been successful: keepGuessi­ng = False .

What’s in a format?

In our second example, we are going to write a piece of code that can convert to and from hex values when referring to RGB numbers. On web pages, RGB numbers are referenced by name or, most often, sixdigit hexadecima­l values. These have three numbers, representi­ng red, green and blue values, with each in a range from 0 to 255. What we will do is allow the user to convert each way, so if a value entered begins with a #, it is a hex value that needs converting to a decimal. If the number starts with a number between 0 and 9, it needs converting to hex. To start, create a new file called colourConv­ert.py in VS Code.

As can be seen in this simple code sample (above),

we are allowing numbers to be entered multiple times and Copilot suggests how to convert the hex numbers to decimal. Note, this isn’t quite correct because we wish to return three separate integers, for red, green and blue. Tweak the code to state numToConve­rtR = int(numToConve­rt[1:3], 16) . Then copy this line to create separate variables for numToConve­rtG and numToConve­rtB. The list slicing needs to be changed to [3:5] and [5:7] for these two new variables.

At every step, Copilot suggested useful additions. The context was guessed pretty well and only minor changes needed to be made to our simple program. Please note that should other values be input to the program, it will crash. A useful addition would be to use a try: except: block to make sure the value selected follows the correct format. This is left for you to try.

These examples are intentiona­lly simple to allow for the functional­ity to be tested and learned about. When writing more complicate­d programs, imagine how much time could be saved accepting some code suggestion­s and testing them, instead of writing them, then testing them anyway. The key to using this technology is to realise where the AI is best used and where human knowledge is best used. The AI performs a best guess based on thousands of code samples the model has been trained on and the human is best suited to understand what has been suggested, ensure that the context is correct and perform the necessary testing.

 ?? ?? The code in grey is being suggested by Copilot and is using what was entered previously to understand the context of the program being written.
The code in grey is being suggested by Copilot and is using what was entered previously to understand the context of the program being written.
 ?? ??
 ?? ?? Install the GitHub Copilot extension from the marketplac­e to use all of its benefits.
Install the GitHub Copilot extension from the marketplac­e to use all of its benefits.
 ?? ?? The initial code sample for the number conversion code.
The initial code sample for the number conversion code.
 ?? ?? This is the final code sample, which converts from hex to decimal and vice versa.
This is the final code sample, which converts from hex to decimal and vice versa.

Newspapers in English

Newspapers from Australia