Chapter 2. Python Tools in Visual Studio

Now that we have our tools up and running, we can start to take a deeper look into one of the most important features of PTVS: the intelligent code completion feature or IntelliSense of Visual Studio.

In this chapter, we will dig deeper into the automatic syntax and hierarchic analysis tools of Visual Studio that we can use with Python. Essentially, these are IntelliSense and navigation tools, which are really helpful for a Python developer during the coding process.

Furthermore, we will see how to maximize the capabilities of Visual Studio in conjunction with the inner REPL tool for quick and useful code testing and debugging.

Mastering IntelliSense with Python

As shown in the Python Environments tab, Visual Studio analyzes the Python code that is available in the current solution and the installed libraries to populate the internal database. By doing so, we are able to gain a better understanding of the available classes, methods, and field descriptions. This is done in a way that can help the developer speed up the coding process.

The IntelliSense context-aware code completion feature can be recalled in line using the Ctrl + Space bar or Ctrl + J shortcuts. The Ctrl + J shortcut displays the list even when there's only one possibility. This is what happens when you call it in the middle of a command:

Mastering IntelliSense with Python

IntelliSense shows all the available methods, classes, and fields available in the current code, ordered in alphabetical order. Besides showing them from the list of available commands, it also provides you with a quick documentation. In our example, for the range method, IntelliSense shows the list of its overloaded methods and its signature.

If you are aware of the method that you are searching for but want a little help with the list of parameters available, just recall the IntelliSense window either through the parameter parenthesis when you open it or with the Ctrl + Shift + Space bar shortcut.

Mastering IntelliSense with Python

In this case, IntelliSense will show possible combinations of the parameters and a quick documentation for each one, given by the position of the parameter in the list as shown in preceding screenshot.

The automatic importing of modules is another interesting functionality that helps in speeding up development. It also provides the functionality of automatic inclusion when you use a class from another Python file.

As shown in the preceding screenshot, if you have a Paging class in one of your Python files in your project, IntelliSense can recognize it as a class that is reachable from your project; also, it will suggest further operations by showing a smart tag under the first character of the classname:

Mastering IntelliSense with Python

IntelliSense in action on a classname for importing. The little blue rectangle under the classname means there are functions available from Visual Studio

When it's clicked, an action pop up is shown. You can choose the operation to execute along with its ability to reference the source/originated file into the current code:

Mastering IntelliSense with Python

If you click on one of the two options, an inclusion statement will be added as the header of your code file:

Mastering IntelliSense with Python

After selecting the "from Paging import Paging" option, the inclusion statement is generated at the top of the code file

Once the class is visible in your code, Visual Studio is able to inspect the referenced class. You will start seeing the class in the IntelliSense window when it's called, as shown in the following screenshot:

Mastering IntelliSense with Python

IntelliSense showing the methods available in the Paging class

IntelliSense can be extended even further. In the following example, when the foo class is defined with the bar method, IntelliSense will fetch the class structure to display the helper to be used in the code:

Mastering IntelliSense with Python

IntelliSense is able to provide us with an insight into the structure of the class and the available elements of it, but without any documentation. To have the documentation shown in the code, we can simply add it to the code of the class as follows:

Mastering IntelliSense with Python

IntelliSense showing the class or method documentation

It's really straightforward and simple. Any element in the Python project is automatically analyzed and fed into the reference database of Visual Studio without having to rework the code or execute external tools during the coding session.

IntelliSense can also help when the code is referenced, giving us an overview of all the modules that are available, all the PythonPath-referenced modules, the modules you are going to reference in your solution, and the modules that are part of your project:

Mastering IntelliSense with Python

Furthermore, IntelliSense is useful not only to select the module, but also to select the import part:

Mastering IntelliSense with Python
..................Content has been hidden....................

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