GitHub Copilot Quick Start

GitHub Copilot is an AI pair programmer. You can use GitHub Copilot to get suggestions for entire lines or entire functions in the editor.

1 Introduction

Let's first learn a little bit about GitHub Copilot. This is a collaboration between GitHub and OpenAI. Copilot is a language model trained on billions of lines of code written by human programmers. , Copilot is capable of generating computer code in multiple languages. For example, you can type "Write a function to reverse a binary search tree" and it will do it automatically. And it is compatible with the following IDEs:

2. Install the GitHub Copilot extension for Visual Studio Code

To use GitHub Copilot, you must first install the Visual Studio Code extension.

In the Visual Studio Code Marketplace, go to the GitHub Copilot extension page and click Install.

A pop-up window will appear asking to open Visual Studio Code. Click "Open Visual Studio Code".

In Visual Studio Code, in the Extensions: GitHub Copilot tab, click Install.

If you have not previously authorized Visual Studio Code in your GitHub account, you will be prompted to log in to GitHub in Visual Studio Code.

If you have previously authorized Visual Studio Code in your GitHub account, the system will automatically authorize GitHub Copilot.

In the browser, GitHub will request the permissions required by GitHub Copilot. To approve these permissions, click Authorize Visual Studio Code.

In Visual Studio Code, in the Visual Studio Code dialog box, to confirm authentication, click Open.

GitHub Copilot provides recommendations for multiple languages ​​and various frameworks, but works especially well with Python, JavaScript, TypeScript, Ruby, Go, C#, and C++. The following examples use JavaScript, but other languages ​​work similarly.

Open Visual Studio Code.

In Visual Studio Code, create a new JavaScript (*.js) file. 1. In the JavaScript file, type the following function header.

```javascript{:copy}
function calculateDaysBetweenDates(begin, end) {
``` GitHub Copilot 将自动以灰色文本建议整个函数正文,如下所示。 具体的建议可能会有所不同。

To accept a suggestion, press Tab.

3. Dataset Overview

The dataset we selected was downloaded from the UCL Machine Learning Repository. It includes data estimating obesity levels based on the eating habits and physical condition of individuals in the countries of Mexico, Peru, and Colombia (Fabio et al., 2019).

I found using copilot to be very easy. As you can see below, Copilot will start generating gray code every time you type something or go to the next line. Most of the time, just hit Tab or Enter and the code will autofill.

Below is an example of using Copilot to import a dataset and quickly view it. It helps us make beautiful charts. Enter a comment:

## nobesity is the dependent variable
## check distribution of nobesity

Hit tab and watch it generate the rest:

Copilot has more than just programming knowledge. It also has general knowledge about the world. Enter the following code:

## calculating body index (BMI)

The BMI formula is implemented correctly in the code! Not only that, but you can also draw distribution plots and heat maps using Copilot's suggestions. enter:

## plot all variables

and

## get corr

4. Assist non-native English speakers

GitHub Copilot understands not only English but other languages ​​as well, which is helpful for non-native English-speaking developers since programming languages ​​are based on American English. For example, the CSS property color is American English, so for native speakers of British English or Canadian English (where the word is written "colour"), "color" is an unfamiliar spelling. Spelling mistakes or grammatical errors can cause errors in the program and waste a lot of time.

In the image below, I wrote a note in Spanish that contains the word "importar" which translates to "import" in English. But GitHub Copilot quickly completed my annotation in Spanish and imported the necessary libraries as described in the annotation.

Additionally, GitHub Copilot can translate English words into other languages. MilMikDev posted a post on Twitter, using GitHub Copilot to translate an array containing a series of words "answer", "question" and "date" into various languages.

5. Create a dictionary to find data

Martin Woodward, Vice President of Developer Relations at GitHub, shared the following tip with us. GitHub Copilot is great at creating dictionaries for looking up data. You can try this by telling GitHub Copilot to create a two-letter ISO country code with a comment, and it will start completing the country name. You just need to write a comment and the first few lines of code, and GitHub Copilot will generate the desired results.

6. Test code

Writing tests is a crucial but sometimes tedious step in the entire software development lifecycle. GitHub Copilot has excellent pattern recognition and pattern completion functions, so it can speed up writing unit tests, regression tests, etc.

For more information on how to use GitHub Copilot for testing, see the following resources:

  • Use GitHub Copilot to automate testing: https://applitools.com/blog/using-github-copilot-to-automate-tests/

  • Simplify testing with GitHub: https://github.com/blackgirlbytes

  • Write better tests with AI and GitHub Copilot: https://about.codecov.io/blog/writing-better-tests-with-ai-and-github-copilot/

7. Use regular expressions to match patterns

GitHub Copilot helps you write regular expressions faster. You just need to write a comment or a function name, and GitHub Copilot will prompt you.

GitHub Copilot can help you remove spaces from strings.

8. Prepare for technical interviews

I've found that a lot of developers, myself included, use GitHub Copilot to prepare for interviews.

The specific method is as follows:

  • First, I would try to solve the problem myself without relying on GitHub Copilot's help.

  • If I get stuck or get frustrated while solving a problem, I activate GitHub Copilot and use it to find better ideas for solving the problem.

  • Next, I delete the code generated by GitHub Copilot, disable GitHub Copilot, and try to find a solution again based on the new information.

Whenever I encounter setbacks and want to give up, I will continue to learn through this method. Even if I don’t have a mentor or a peer to guide me, I won’t give up because I can always get new ideas. GitHub Copilot is my digital mentor. However, be careful not to activate GitHub Copilot during the interview (this is cheating).

9. Send a tweet

Of course, you can send tweets directly through the Twitter app, but I prefer sending tweets through the IDE. In a recent live broadcast, I needed to demonstrate using Python to call Twitter API v2 in GitHub Copilot, but I rarely use Python. But after I wrote a few comments, GitHub Copilot generated the code I needed and saved me!

10. Exit Vim

A question that developers new to Vim often encounter is: how to exit Vim. Now Visual Studio Code, JetBrains and Neovim all support GitHub Copilot, so you can exit Vim through GitHub Copilot.

11. Explore new code bases with Copilot Labs

GitHub Copilot Labs is a complementary extension to GitHub Copilot, developed by the GitHub Next team. It is an experimental sidebar that can help developers translate code from one programming language to another, and will also explain each piece of code step by step.

Understanding a new code base can be difficult, and these two features from GitHub Copilot Labs can help. Developers can better understand complex blocks of code by translating code into a language they are more familiar with and using the Explain feature to illustrate the code.

12. Further analysis

Let's try using lambda functions to encode some categorical variables. Copilot can generate almost accurate code for this. But some adjustments have to be made, like changing "Yes" to "yes". I'm not sure whether this is a problem with Copilot itself or a problem with the instructions I gave. Because it is due to training, it may be that many people write different codes, or it may be necessary to specify how the string should be formatted.

Copilot is particularly gender sensitive. Enter "gender" in any cell and there will be no more suggestions! This is definitely for the so-called PC, so when Copilot is working, you need to avoid using more sensitive words such as "gender".

Now, let’s analyze the relationship between obese people and high_cal and fam_his:

## sum of high_cal and fam_his across different obesity groups

Copilot provides the code we need. It also provides us with code to plot these results. This is similar to the code snippet below.

Our daily EDA work can be completed easily, so in the end I tried something less formal:

## best way to plot BMI and age and obesity group

A very colorful and interpretable scatter plot can be seen (see image above). This shows that writing a review that is not very informative can still give us accurate results.

13. Summary of use

Copilot can generate useful short code snippets. It can also generate several lines of high-quality code. It's not perfect but it does help me start somewhere. Using Copilot every day really saves time. Because we no longer need to spend too much time looking for answers to questions like "how to use .agg on multiple columns", we can just write the annotation, so do we need it to cost money?

references

GitHub Copilot Quick Start - GitHub Enterprise Cloud Docs

Baidu security verification 

Baidu security verification 

Guess you like

Origin blog.csdn.net/xhtchina/article/details/130179013