Chapter 4. Editing

Editing is what we are going to be doing the most in PyCharm, and hence, this is one of the most important chapters. This chapter does not try to cover a wide range of feature sets; it instead tries to provide a comprehensive look at some of the best tools available for editing. The most important part of this chapter is enhancing code completion in PyCharm. This IDE has a remarkably powerful engine that can understand your code, and it allows for things such as better code completion (even when we haven't specified the types), method hierarchies, and a lot more.

This chapter has several parts:

  • Improving code completion: This section will take a comprehensive look at the tools at our disposal that enhance PyCharm's code completion, giving us more completion options and smarter completions.
  • Writing code: This section will cover some of the best tools that PyCharm offers to write code quickly and effectively.
  • Setting up IPython Notebook: IPython Notebook has become the de facto document format for Python's scientific community, and for good reason. This section will take a look at how to set up IPython Notebook in PyCharm so that we can get the benefits of code completion inside the notebook.
  • Editor plugins: This section will cover a few select plugins to help you get used to PyCharm if you come from other editors such as Emacs and Vim. This section will also showcase support markdown.
  • Reading code: This section looks at the different tools at your disposal to better read code such as diagrams and method hierarchies.

Improving code completion

Code completion in PyCharm is really quite something, and there are many things that you can do to enhance it even further. PyCharm normally gives you code completion options as you type:

Improving code completion

However, if you press Ctrl + spacebar while this popup is on the screen, you will get even more code completion options:

Improving code completion

Note that islice is not even imported, yet PyCharm can smartly tell you that you can use it, and if you chose to do so, it will be automatically imported. PyCharm also supports Cyclic Word Completion also known as Hippe Complete (Alt + /), which can prove to be very useful when you want completion in strings:

Improving code completion

Furthermore, you can fine-tune your completion options:

Improving code completion

Here, [1] is, by default, set to first letter, and I feel it's best to demonstrate this with an example.

With first letter, you will need to type in a capital K in order to get KeyboardInterrrupt; otherwise, it will never show up in your completion options:

Improving code completion

But, with the setting set to None, you don't need to type in a capital K:

Improving code completion

I set my case sensitivity to None because I'm too lazy to press Shift.

What [2] and [3] do is pretty self-explanatory, however, you might be confused at what Smart Type Completion does, and you're right to be. It has nothing to do with Python, but it's a JavaScript feature in PyCharm that gives you better code completion options when you're writing JavaScript.

This would be a good time to tell you that PyCharm has what I like to call Fuzzy Completion, which means that in the preceding example, if you were to type in ki, it would take you to KeboardInterrupt, but this is not limited to camel case alone, it also extends to underscores:

Improving code completion

Again, [4] is self-explanatory. It will simply arrange your options in a certain way (basically, alphabetical, but numbers appear before letters). Toggling [5] will allow completion options to display commas, semicolons, and a whole host of other characters. This can slow down PyCharm quite a lot on slow systems. Option [6] will automatically open documentation when you press Ctrl + spacebar on a completion option:

Improving code completion

The Parameter Info popup, [7], shows you the parameter information that looks similar to a little callout:

Improving code completion

This will automatically pop up, and if it goes away for some reason, you can always bring it back with Ctrl + P (Parameter Info).

Understanding what intentions can do for you

If you've ever wondered about what that yellow light bulb means, it's simply an indication that there are possible intention actions at your disposal. Intentions in PyCharm are context-specific actions that you can invoke with Alt + Enter (Show Intention Actions). One very useful example of intentions at work is when using language injection in strings.

Understanding what intentions can do for you

And with that, we can insert the many different languages that PyCharm supports:

Understanding what intentions can do for you

The best part is that you get syntax highlighting and snippet completion within the string as well:

Understanding what intentions can do for you

But, injections are not limited to languages alone, for example, you can have things such as file paths:

Understanding what intentions can do for you

You can have a look at all the intentions at your disposal here:

Understanding what intentions can do for you

All the intentions are context-specific; sometimes, they're tuned specifically for languages and others are often more general purpose. For example, in Python, you have the intention to convert from a single-quoted string into a double-quoted string, however, language injections work across different languages. You can get a good idea of what these intentions do just by looking at its individual page:

Understanding what intentions can do for you

It gives you everything that you wanted to know about intentions, but notice what it is powered by. In this case, this is one of PyCharm's core functionalities, but some intentions are powered by plugins, so if one does not work, you can figure out what is causing the problem.

Collecting runtime types

Code completion is probably the reason why most of us use PyCharm. The PyCharm team spends a lot of their time improving and refining completion options not only in PyCharm but also in other IDEs. One of the easiest ways to improve code completion is to gather runtime data.

Collecting runtime types

Collecting runtime types allows PyCharm to provide better code completion. This runtime data is collected every time one debugs a program, not when one simply runs it. If by any chance, PyCharm shows you the wrong types, then clearing the caches is the best way to solve the problem.

Adding docstrings and type information

Docstrings are parsed and used by PyCharm to give you better code completion as well as better quick documentation information. Here is an example, using Intentions.

Adding docstrings and type information

Say we wanted to make a Person class so that we can declare the class and add a few parameters to its __init__ function:

Adding docstrings and type information

We can also add parameter info through docstrings or annotations (in Python 3) and since docstrings work for both Python 2 and 3, we're going to stick with them.

Adding docstrings and type information

This causes PyCharm to generate a lot of documentation for me (for free!):

Adding docstrings and type information

Let me take a step back to say that this docstring is in a particular format, that is, it's in rst or reStructuredText. This means that PyCharm can parse the string and get information regarding the parameters to the init function and class it is initializing. Note that you have the choice between Epytext and reStructuredText. I suggest using reStructuredText because that is what PyCharm uses to generate documentation skeletons for a variety of stdlib classes. Here is where all of these options are set:

Adding docstrings and type information

Now that we've put in some information regarding our Person object, let's see what the Quick Documentation (Ctrl + Q) brings up:

Adding docstrings and type information

This is great, but we can get even more information out of docstrings. So now using another intention option, we can generate the parameter types for each of the arguments for Person:

Adding docstrings and type information

And with this, we can say that the name will be of type str:

Adding docstrings and type information

With this, you get even more information from Quick Documentation:

Please note that you need to be careful when adding docstrings because they require special formatting. So, in this case, we have the following:

Adding docstrings and type information
        """
        :type name: str
        :param name: The name of the human

        :type age: int
        :param age: How old the human is


        :type gender: str 
        :param gender: Male or Female

        :type country_of_origin: str 
        :param country_of_origin: Somewhere on the planet, hopefully

See how we have the type of the parameter first, then its description, and finally a blank line between parameters. This is best practice, so even without PyCharm someone can quickly read about and understand the different parameters. This is how you need to arrange the information in your docstring in order for PyCharm to give you useful quick documentation. Docstrings can also be used by PyCharm to help you write more accurate code:

Adding docstrings and type information

PyCharm correctly points out that you've put in the wrong parameters, and this kind of static analysis can be very helpful. Also, note that you can choose to tell PyCharm to ignore the problem (by pressing the right arrow after we've selected Inspect 'Type checker' options):

Adding docstrings and type information

This will add a new command above the line, telling PyCharm to ignore the type checking for this statement:

Adding docstrings and type information

Docstrings can provide a lot of code completion options for functions, however, they often fall short when it comes to providing code completion options for metaclasses. This is to be expected since docstrings provide static analysis.

The skeletons in PyCharm's closet

No, it's not that PyCharm has something to hide; it's just that it has a lot of skeletons generated for different modules, and you can take a look at them too:

The skeletons in PyCharm's closet

Python skeletons provide a lot of the code completion that you see in PyCharm, and all are hosted on an open source GitHub repository at https://github.com/JetBrains/python-skeletons.

And when I said skeletons, I meant skeletons! These are not implementations; they are merely something that PyCharm can statically analyze. Let's look at the decimal.py file:

The skeletons in PyCharm's closet

There's no implementation here, just the bare information that PyCharm needs to give you the code completion options you need. The skeletons are still a work in progress but are getting better every day.

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

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