Mastering PyCharm Transcripts
Chapter: The Editor
Lecture: Live templates in action
0:01 One of the cool little tricks we saw earlier
0:04 was that _ _name_ _=_ _main_ _
0:06 sort of run convention that we saw
0:09 so if you import a module its name is just the name of the module
0:12 but if you execute a module its name is _ _main_ _, that's a convention
0:16 and we saw that we could type main and get it just like that.
0:19 And notice even where the caret is
0:22 it's right where you should be typing to sort of do the stuff that happens.
0:26 We have a bunch of these things
0:28 so if I want to say enumerate over this small set of fibonacci numbers
0:33 and I want to like print out the first number is this, the second number is that
0:36 it's super easy I could say iter, and that would be basic iter,
0:39 I could say itere for the enumerate.
0:42 And boom, I num, and it proposes num, it knows this is iterable,
0:47 and all those kind of stuff.
0:49 I'll say it's a fib and this would be index
0:51 but you could just stick with whatever it proposes.
0:54 I could just do a little cool format thing.
1:05 And if we run this, notice it says run configuration, right click run—
1:10 boom super, super quick,
1:13 we have the first fibonacci number is 1, second is 1, third is 2, fourth is 3,
1:22 it's hard to say that.
1:25 These live temples are awesome
1:27 how do we know which ones there are?
1:29 Can we create others? I'll show you and yes.
1:31 If we go down here to the editor, to live templates
1:35 at the top, notice when in opens it should be like this probably
1:38 there's a whole bunch and it tells you to expand them with tab
1:41 and there's Python, there's Django, there's Flask
1:46 You can come down here and you can see we've got
1:49 actually not as many as I would hope
1:51 but here is a super call, here are some properties
1:53 with getter, setter, deleter, here is the main thing,
1:56 here's that iter notice for index variable in iterable,
2:01 and it automatically proposed nums
2:03 because that was an iterable in scope, really, really cool.
2:07 Now notice this is applicable to Python and Python classes, so this is important.
2:12 You can also tell it to reformat the code there are a bunch of things.
2:16 So let's add one, let's say hey, I want to be able to quickly have a wizard battle
2:21 so I'm going to create a live template and this is going to be
2:24 wiz is what I'm going to tie and this will be create a wizard battle
2:30 and I've got some text copied here, can I indent it, I can
2:37 so what this will do is quickly generate this wizard and some creatures
2:41 so hey, they can battle, and let's say even format the code
2:44 depending on where do we paste it.
2:47 Now if I try to run this, it's not going to work, it will never show up
2:50 and the reason is a little like warning down here
2:52 it says you have to say I could just say it runs everywhere, probably not
2:57 so I could come down here and just say
2:59 it runs in Python class other that's all good.
3:02 So now if we come down here and you want a Wizard battle,
3:04 I type wi— wow and look, create a wizard battle, bam.
3:08 Now of course, we got to import these things,
3:11 import the small creature but notice a little lightning bulb
3:14 hey, your code is broken but not for long.
3:17 Now we could just come down here and say something like
3:21 print hero battles whatever
3:26 and we'll do a little format, maybe hero does an attack
3:32 and remember we have beautiful help on what goes in there
3:37 it that takes a creature,
3:39 so let's just say it's going to have a creature of zero, the Toad
3:42 so now if we run it, boom
3:45 Gandolf attacks the Toad, the Wizard rolls 75, the toad one half boom,
3:51 that is a pretty handily beating that the Wizard gave to that Toad,
3:55 but what would you expect right, it's a Toad.
3:58 So we can create these live templates
4:00 this Wizard example that I told you about is silly
4:02 but it shows you how to create them, set their contacts and so on.
4:08 Definitely think about the patterns that you do over and over and over
4:11 and if you could make those in a couple keystrokes
4:14 and it just happens reliably,
4:16 and you can even maybe share them across your team, that'd be awesome.