Lens mode is a feature in PyCharm that allows you to quickly and easily view issues in your code by merely hovering over markers on the right side scrollbar. You can see problems, and you can click and move directly to the part of the code that's marked. Once you're there, you can use intent actions by using alt-enter which displays a list of suggestions. If you've ever used JetBrains' ReSharper in Visual Studio, or any other JetBrains product like IntelliJ Idea, you'll recognize this feature.
- [Narrator] One of the guiding principles in Python development is that we should always create code that's easy to read and easy to understand. The language itself is geared around this with it's reliance on white space and it's body of standards called PEP 8. All of these styling rules make Python very different than most other languages. And so it can be a little off-putting to developers coming in from another language like maybe C# or Java where everything is straight forwardly delineated by semi-colons.
In Python of course that's not how it works. We have to structure our code in a certain way in order for it to be valid code. Keeping up with all those stylistic concerns can be a bit daunting. And this is probably the best reason to use an IDE like PyCharm because the IDE understands all of those rules and all of those requirements. I'm going to open up the page_spider.py file that I created earlier. And I can see in this file visually that there are some problems with it. The obvious indicator is that on line four I can see that there is a squiggly underline, and that usually indicates that something needs to be fixed.
I can also visually inspect a file by looking over here to the right side where there's a scroll bar which you would expect but above that scroll bar is a yellow square. The yellow square indicates that there are warnings within this file. And you'll also notice that there are several markers along the file here. These are visual indicators that show me where the problems lie in the file. So since this problem happens to be right here, I can just roll over this squiggly line here and it'll tell me what's wrong. So this one happens to be a PEP 8 problem, and PEP 8 says that we need to have two blank lines between our function definitions.
And I've only got one here. So I can fix this very easily by just adding another space here and it'll go away, but I want to show you a different way of looking at this a different way of fixing this so I'll put it back in. I can select anywhere on this squiggly line and I can hit Alt+Enter and this allows me to use a feature in PyCharm called intents or intent actions. If you've ever used ReSharper in Visual Studio then you're going to be right at home with this feature because of course JetBrains makes that product as well.
And so you have all of the features that you would be used to in a Visual Studio ReSharper environment right here in PyCharm. For those of you that have never used ReSharper before, it's one of the most popular add-ons to Visual Studio and I think you're about to see why. So from here I can use the intent action to actually reformat the entire file. I'm going to go ahead and press Enter here and as you can see it not only reformatted the first one but it also went ahead and spaced out the rest of the file, and took care of any other lingering problems that were in there that were formatting related.
I'll scroll back up to the top and I can see that I still have a yellow indicator here over on the right side indicating that things are not quite perfect. If I hover over this it'll tell me at the top that there's still one warning in the file and I can see it's right below here on the first line, and it's telling me that I have an unused import statement, which is absolutely correct. I do have an unused import statement but it's not going to be unused forever and so I'm just going to choose to ignore that one for now. So armed with our new techniques let's go back over to url_utilities.py and let's see if we can fix these problems as well.
I can hover over the square at the top which is a very very dim yellow and it's going to tell me that I have some weak warnings. These are things that are really minor and probably aren't going to affect much. So here let's see we have parameter 'file_path' is not used, absolutely true because we haven't written that code yet. And you can guess that the rest of them are going to be pretty much the same thing but we're going to have these blank line issues here. So I can go ahead and fix that, I'll just select one of them and I'll hit Alt+Enter, and select Reformat file.
And it'll go ahead and reformat that file for me, putting in all of the required spaces. There's another way to do this and I'll go ahead and point it out. You can go up to Code and you can select Reformat Code here, and that way you don't have to use it as an intent action. I've also committed to memory the keyboard shortcut for this and I do this often. So that will keep your code formatted at all times and allow you to keep things neat and tidy. So the rest of these are things that we're just not going to fix just yet.
Now you might notice that there are some blue areas here and those are actually going to be pointing to your TODO items so you can see a representation of your file and where they are and of course you can click on them and go right to them. One thing worth pointing out is that the space the size of this side bar here is not necessarily directly proportional to the size of the entire file. We can see by scrolling up that we have scroll space in here but if I scroll to the top and select this one on the bottom it's not going to correspond directly.
This warning here is not pointing to this line right here, it's pointing to the one that's further down. So you're going to have a representation in space of your whole file here on the side even though we're going to need to scroll up and down. And this is really similar to the window in Sublime Text if you've ever used that tool before it shows things a little bit differently but it's the same idea. Let's go ahead and fix database_utilities and it's going to be something along the same lines.
This time I'll just come in and select Code, and Reformat Code or I'll press Alt+F8 if you prefer. Remember too that these options are going to be different depending on what operating system you're using. So whatever you see here in the window is the keyboard shortcut that you should use for your environment. I'll go ahead and select Reformat Code and that should take care of all of the problems that I care to fix at this point. I'll go ahead and check the remainder but they should just be value not used and then we've got our two TODO items.
So everything now looks pretty clean here but you might also notice that everything has turned blue. So the issue is that we have changed these files and we need to check them into our revision control system we need to update things. So I'll just come down here to my revision control panel I'll select Default, I'll select Commit Changes, and I'll just say for my Commit Message fixed formatting issues. And I'll Commit this change.
- Installing the PyCharm, Git, and Pypy packages
- Adding functions
- Refactoring code
- Calling packages
- Debugging code
- Creating databases
- Working with in-line SQL
- Creating web projects with PyCharm