- So, now that we explored the basic auto-completion and suggestions functionality with this basic
- example, let's dive into another slightly more complex example.
- Let's say we want to write a script that helps us back up our SQLite database.
- So, let's imagine we have some SQLite database, an app.db file, which could contain our SQLite
- database.
- Here it's of course just an empty file, but we could populate and initialize it with some
- other Python code.
- And let's say we have such a database file and now we want to write a little utility
- program which we can run to back up that database.
- And that does not just mean that we want to have a program that copies the database,
- but instead here I want to build a program that actually asks me what I want to do.
- And one thing I could do besides backing up the database could be that I want to restore
- it.
- So, my idea essentially is that it always keeps like one backup and one active database
- and we can roll back and restore the backed up database.
- And of course we can use GitHub Copilot for that.
- Now we could use the chat functionality, but we'll explore that later, so let's get there
- with just suggestions, because as you will see, that's also really simple to do and these
- suggestions can be really powerful.
- Now I'll start here by adding some global variables and I'll start by adding one variable
- which I'll name active DB file and there I'll store a string where I have app.db.
- And you see here for me it automatically suggests backup DB file.
- Now you might be getting different suggestions.
- That's important.
- You will very likely not always see what I'm seeing here.
- That's how AI works, but chances are high that you're getting a suggestion like this
- as well.
- And if you don't, well, you can always just start typing and at some point you should
- see a similar suggestion.
- And here I'll actually rename this to just backup.db because of course you can and should
- always evaluate the code that is generated by GitHub Copilot and you can change it to
- make sure that it is the code you want because this is a course for developers using AI.
- So use your developer skills, your knowledge and get the best of both worlds.
- Get more efficient by using AI.
- Don't rely on just the AI.
- That's really important.
- And if you do the latter, as you will also see a little bit later, you can actually get
- less efficient.
- So always think about the code that was generated by AI.
- Like here where I renamed this to be in line with the naming convention I want to use.
- So now I got these two variables here, but actually I now already got more than that.
- I'm giving GitHub Copilot some extra context already because the entire code base it sees
- in this file is the context it evaluates behind the scenes.
- So it will take these variables into account for its next suggestions and it probably already
- has a first idea regarding what I'm building here, what my program will be about.
- So now I'll add a function and guess what?
- It suggests this function name and also this code block here.
- So not just one line, but multiple lines.
- And if I hover over this after a while, I'm getting this little info box here and I got
- different suggestions here, as you can tell.
- Now here I actually want to take the suggestion, which includes a little log here.
- So I'll hit tab and I got this code here.
- Now as you can tell, got some yellow squiggly lines here that this thing here is not defined
- because I'm using some identifier here, which doesn't exist in this file.
- Now that's because I need to import something.
- So let's go up there.
- And if I type IM, GitHub Copilot, again, because it takes the entire file content into account,
- it understands that this shutil package is missing and it suggests this import.
- So that's how it can speed up your work, because now I'm, of course, taking a lot of time to
- explain everything, but you can go through the
- entire code really fast and just hit tab all the time in the different places, you just need to
- know where to place your cursor and which characters to type to get the right suggestions.
- That's the part where your developer skills still are important, leave alone more complex programs
- where of course GitHub Copilot might not be smart enough. But for simple programs like this,
- it's really just about placing the cursor in the right place,
- typing the right characters and suggestions should then get you all the rest.
- Now here it actually already suggests a restore database function for me here,
- though I'll actually change the indentation and then I'll accept this.
- And as soon as I do, in my case here, it suggests the next lines of code where it wants to copy the
- backup db file and replace the active db file with it, so to say. So that's all looking pretty
- good and with that I already got my very basic backup restore script but of course I got no way
- of invoking these different functions. So therefore I'll do that as a next step and
- now I'm getting a suggestion here which is not helpful to me. This looks better,
- some code that would run when this script is executed, so let's see what I get as the next
- suggestions. But this of course now doesn't make sense. Now you again might be getting different
- suggestions but at least for me the suggestions I'm getting here don't make sense. I don't want to
- backup the database and restore the database immediately thereafter whenever I run this
- Python script. That's not what I want to do here. So here you can see the AI isn't really
- understanding what I want to do with this overall script. Now let's take a look at the completions
- panel here. Maybe we got more useful suggestions here. After all it is taking more time here
- but no this is also not what I want. So instead again it's my developer skills that are needed
- here and I'll actually add another function here and I'll name it main and in that function I also
- don't want to backup and restore the database. Instead I want to evaluate the user input
- and for that first of all I want to tell the user what their options are. Now just by typing print
- GitHub Copilot kind of catches up and understands that I maybe want to print some options and the
- first option could be to backup the database. I'll accept this. Then restore the database
- and then here I want to get the the user choice with the input function. Makes sense. So I'll
- hit tab again and then thereafter it also correctly estimates and suggests that I want to
- check which choice the user made. One or two and then invoke the appropriate function up there
- or else I want to tell the user that the choice is invalid.
- Now actually I also want to allow the user to exit the program so I'll add another print here
- and smartly it suggests this option and I'll add another else if block in here
- all with GitHub Copilot and then I want to print exiting and thereafter it will exit the program
- since I'm not invoking any other code. Now inside of my code down here I now want to call this main
- function. It did not suggest it here for me so I'll just type it manually. It's not too much work
- and that's also important. You shouldn't wait for suggestions if it takes some time or if they
- don't show up for some reason. If you know the code you need just type it. Maybe you'll get
- suggestions along the way at some point then but even if you don't if it's not a lot of code like
- this just type it. Don't sit there and wait for the suggestions because that is a trap you can
- fall into. When using these AI tools you can feel inclined to use them for everything but
- don't forget that you can type code as well. You have more than just the tab key on your keyboard
- so use it. And with that I get this program here. Let's give it a try I'd say. I'll run it
- like this. I get my choices. I can exit of course. Yeah that works but now let's run it again and
- let's backup the database and you see this backup db file is created. If I now run it again and
- choose restore probably also worked. Didn't get an error. Looks like the database was restored.
- We can test this by adding some text in there which is of course not valid sql light database
- content but it's good for testing this. So I got the t and the appdb and the abc in the backup
- and now if I again restore this you see I get the abc here in appdb as well.
- So the script is working the way it should. Of course still a simple script but you saw how we
- can use github copilot to generate it and you already learned about some potential pitfalls
- and how to use github copilot efficiently.