Modern Python Projects Transcripts
Chapter: Your code editor
0:00 As you start typing in VSCode, you will sometimes notice that there is a pop up suggesting you some different auto completion
0:06 So, if we try to create a new function, you can see that there are some strange long auto completion options.
0:13 Those are snippets, and they come usually from different plug ins. For example, Python snippets come from the Python pluggin.
0:20 They can save you some typing. For example, this one for the function will pre generate the scaffolding of your function
0:26 So let's create a name of a function, you can hit TAB to go to the next location in the snippet. So now we need to provide some parameters.
0:35 Let's put the name the Docstrings and then finally, the code. Let's print something and let's run this one.
0:51 The nice thing about snippets is that you can easily create your own, so let's try to make a new snippet for Python. First, using the command palette,
0:59 select configure user snippets. That's such if we have Python snippets, yes, we do. So, let's select this one.
1:07 And now we have a file where we can write our snippet inside this file. We already have some example Snippet. Unfortunately, it's not a Python snippet.
1:27 Let's say I want to create a snippet that will measure the execution time of a given piece of code. So I want to start the timer on the beginning,
1:35 execute some code and then display how long it took. So let's call it time measurement.
1:44 So first parameter is prefix. This is the text that will trigger the suggestion for
1:48 the snippet. Let's say we want to trigger the snippet auto completion when we type time. Next, we have a body. Body is a list of lines that will
1:59 be inserted as our snippet. So, don't worry about the indentation. VSCode will figure it out. So here is the code of our simple snippet.
2:07 First we import time module, then we save What time is it, then? We have a placeholder called tab stop, when you insert a snippet and you
2:16 add this $1, $2 and so on and so on. This is the location where the cursor will move when you press stop,
2:24 as you saw when we used the def snippet each time I pressed stop, I was moving first from the function name,
2:31 then to the parameters list, then to the Docstring and finally to this pass statement. So those are four different tab stops. Here
2:39 We only have one, because this is where we want to type some code. And finally, we have a line that will subtract the initial time from the current
2:47 time and print out the results. Final parameter that we can specify is the description.
2:58 This is the description that will be displayed in this auto completion. Pop up, for your snippet. So, once we have the snippet,
3:04 let's give it a try. Let's remove this and let's type time and you can see this is our snippet. Let's add some numbers so it will take some time
3:14 Let's let's actually execute only this part in the terminal. So I select the code and pressed shift enter and,
3:30 as you can see, first I have the sum of the first one million number and then I have a statement saying that it took 0.4 seconds,
3:40 so that's how we can create a simple snippet. If you want to learn more, go to the VSCode documentation. There's a whole section on snippets here,
3:47 but I suggest you take a look at create your own snippets, after explaining the basics The important thing that you should take a look are variables,
3:56 so you can use special variables in your snippets and that will, for example, insert the currently selected text, the content of the current line or,
4:05 for example, the content of your clipboard. And so on, dates and stuff like that, you can even perform some regular expression transformations,
4:19 and you can even assign key bindings to snippets. So to create more advanced snippets, I suggest you take a look at this documentation.