Mastering PyCharm Transcripts
Chapter: The Editor
Lecture: Light-bulb moments
0:02 One of the more significant things that PyCharm does to help us
0:05 is what I call light bulb moments.
0:09 So when you are in some error situation,
0:12 PyCharm will say, hey it looks like something's wrong, but I can fix it,
0:16 if it's yellowish like this one, I am pretty sure it's an error
0:21 other times it'll be red and they are like,
0:24 no, there is no chance this is going to work, I'm sure of it.
0:27 It depends a little bit on the severity of the situation
0:30 because it can do many different things
0:32 so the color of the light bulb tells you a little bit
0:35 but what PyCharm is saying is your code is not right
0:37 but I can fix it for you and I can do that most of the time automatically
0:42 if you just agree to it, so what's happening here?
0:45 Well, we have this data access library
0:47 but it doesn't have a change_address function
0:51 but we're creating this, we're writing this bit of code
0:56 this is the first time we're ever going to need to change_address function
0:59 and we're just about to switch over to data and write it.
1:03 But we want to kind of sketch out our use case of it here
1:05 and then we are going to go write it.
1:07 The thing is, you don't have to switch over to data and write it,
1:10 PyCharm will do that for you.
1:12 So if we hit alt enter, you'll see it will pull up a list and it says
1:16 hey how bout I create a function called change_address in the modular data
1:21 and it would actually have first parameter called user
1:25 and the second parameter, like parameter 2 or something
1:28 because it has no idea what that string means
1:30 but it would be a place holder for the email.
1:33 So we hit this and it will actually create the skeleton of the function
1:36 in the right place for us and then we can go just sketch out the implementation.
1:39 That's really awesome, so we hit enter and it goes boom
1:43 down here just like that.
1:46 And down here we may be want to type new email and implement the thing.
1:49 It'll go create this inside a data.py
1:54 when we are trying to access it from program.py.
1:56 Really nice. So you'll see these little what I call light bulb moments
2:00 or code helpers, all over the place.
2:02 Now they don't just mean hey a function is missing
2:05 they can do all kinds of stuff so if we look at those
2:08 we can create a function or a class from a particular usage
2:13 that's what you just saw;
2:15 we can also say import a module, so if I type xml.etree.ElementTree
2:22 but I don't have the import at the top, PyCharm would propose
2:25 one of these little light bulb moments to say
2:28 you know, you should import that at the top and then it's going to work.
2:31 It doesn't do that for external packages all the time
2:33 if it finds something else that could have been a candidate
2:36 and that's kind of what happened in my request example
2:38 but it will basically import the module
2:41 we also saw that we can install packages from that as well.
2:44 So we can do micro refactoring,
2:48 this code, it has parentheses around if statement
2:53 there's no need for that, we can take them away,
2:55 and other little things like that.
2:58 It can find dead code and sort of clean that up for you.
3:01 I showed you the example of that unused show_id right,
3:06 there's some little code helpers around that.
3:09 It can find pep 8 violations and fix those
3:12 it can even fix performance issues
3:15 if it knows you're using some pattern that is sub optimal
3:18 but it can automatically rearrange it to a better one
3:20 it will propose that it automatically fixes that for you.
3:23 It could be aware of Python 2 and Python 3 differences or mismatches
3:30 and fix those four you as well, it doesn't always turn these on,
3:33 but if it's pretty sure you're mixing Python code from 2 and 3
3:37 it will give you a little pop up that says hey, should we turn on
3:41 this Python 2/ 3 compatibility checking for you?
3:44 And if you say yes then it will propose to fix that.
3:47 It has all sorts of things around package maintenance,
3:50 one example is if I have a package that it's using a dependent package
3:55 like one package is using say Requests or SQLAlchemy,
3:58 it will propose to put those inside of the requirements.txt
4:04 if it doesn't find them in there, stuff like that.
4:06 Tons and tons more, you'll see these pop up all over the place
4:11 and really it's tough to find all of them and list them here
4:14 because it just does so many little things
4:17 so keep your eye out for any time you see a little light bulb
4:19 hit all enter, see what it's proposing
4:22 sometimes that is 2 or 3 different things it's suggesting for you.
4:25 You'll get used to what it can do and this is really helpful when you are new
4:29 but it's also really helpful when you are an expert
4:32 so when you're new, it's like I don't know what to do
4:35 oh it says it can fix this in this way great I guess that'll fix it.
4:38 Whereas when you're an expert, like
4:41 yes you could go track down the requirements.txt file
4:43 and write SQLALchemy in there
4:47 or you could just hit alt enter and keep coding
4:49 and not have to worry about that and stay in flow.
4:52 So I think this is a great feature for all levels
4:55 but it offers different things to different levels.