Effective PyCharm Transcripts
Chapter: The Editor
Lecture: Light-bulb moments

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


Talk Python's Mastodon Michael Kennedy's Mastodon