Writing code

This section has been purposely kept short and only showcases three powerful features that many PyCharm users are not completely aware of. The first two features are very simple and require us to simply use a keyboard shortcut, while the other feature requires a little more work.

Refactoring

Refactoring is one of PyCharm's most powerful features and its capabilities go beyond a single file. One of the simplest ways to see this feature at work is renaming a variable or a function:

def add_one(n):
    return n + 1


def foo(func, n):
    return func(n)


foo(add_one, 2)

In the preceding example, we want to change the function name foo to apply (because it makes more sense). This is of course a simple example, but helps prove a point.

Refactoring

This brings up another window that gives us a few options:

Refactoring

Here, [1] is very useful if we have put docstrings into the functions that describe variables. [2] will search for text documents such as .rst and .md. This already shows you how far PyCharm can go in its analysis. In this simple example, we don't need to turn on [1] or [2], so let's see what happens when we just go ahead with what we have.

Refactoring

This panel might seem complicated, and we don't have to understand what all the buttons do to take advantage of what this panel has to offer. We can see the function that we are going to name, in this case foo, and the places where it is referenced. By default, things are grouped by directories and then by files, and it's best to keep it this way (although we can change this as well). In this case, we have only one use case of foo, and that is being shown under the aptly named file, refactoring.py. However, one of the buttons that I do find very useful is the one indicated by the arrow that will show the exact code in a side panel (although we cannot edit in the panel).

Once we hit the Do Refactor button, we can see that foo is now renamed apply. As I've mentioned before, this is a simple example because this can be used to refactor everything from module names to setting variables in totally different packages with usages all over the place. As long as PyCharm knows that a file exists, that is, it is in the PYTHONPATH, it will search the file for relevant usages.

Multiple cursors

Multiple cursors has been a long-awaited feature in PyCharm and its addition has everything to do with sublime text championing this much-loved feature. Placing cursors is easy; all we need to do is add or remove a caret (Shift + Alt + click). Clicking on the same place a second time after a cursor has been placed will remove the cursor. This is rather difficult to show in a book, but it looks similar to this:

Multiple cursors

We can also get multiple cursors by invoking Add Selection for Next Occurrence. This allows us to place a cursor on the same occurrence of a symbol.

Multiple cursors

We can also use Alt + drag to get a straight perpendicular line of cursors.

Multiple cursors

This can be really useful when trying to add trailing commas at the end of each item in a list or dictionary, for example.

doc_mode

This is something I coined myself. It is not an official feature, but rather something that you can set up in PyCharm. This mode is terribly useful when you want to explore a new library. Here we have a Quick Documentation popup, but what if we wanted to see the documentation in a panel and that panel updated as we invoked methods or functions?

doc_mode

What we first need to do is change Quick Documentation from Floating Mode to Pinned Mode:

doc_mode

We also need to disable Floating Mode. This part is confusing since we need to click on the gear icon again and then disable Floating Mode, just like we enabled Pinned Mode. After it is pinned, we need to invoke Quick Documentation again. Click on the gear icon once more and select Docked Mode. Make sure that Auto-update from Source is also selected.

doc_mode

Once this is done, we can now get documentation as we type.

doc_mode
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.31.159