Effective PyCharm Transcripts
Chapter: The Editor
Lecture: Live templates in action

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


Talk Python's Mastodon Michael Kennedy's Mastodon