Hands-On Enterprise Automation with Python.
上QQ阅读APP看书,第一时间看更新

Visualizing Python code

Ever wondered how a Python custom module or class is manufactured? How does the developer write the Python code and glue it together to create this nice and amazing x module? What's going on under the hood?

Documentation is a good start, of course, but we all know that it's not usually updated with every new step or detail that the developer added.

For example, we all know the powerful netmiko library created and maintained by Kirk Byers (https://github.com/ktbyers/netmiko) that leverages another popular SSH library called Paramiko (http://www.paramiko.org/). But we don't understand the details and how the classes are related to each other. If you need to understand the magic behind netmiko (or any other library) in order to process the request and return the result, please follow the next steps (requires PyCharm professional edition).

Code visualization and inspection in PyCharm is not supported in PyCharm community edition and is only supported in the professional version.

Following are the steps you need to follow:

  1. Go to the netmiko module source code inside the Python library location folder (usually C:\Python27\Lib\site-packages on Windows or /usr/local/lib/pyhon2.7/dist-packages on Linux) and open the file from PyCharm.
  2. Right-click on the module name that appears in the address bar and choose Diagrams | Show Diagram. Select Python class diagram from the pop-up window:
  1. PyCharm will start to build the dependency tree between all classes and files in the netmiko module and then will show it in the same window. Note this process may require some time depending on your computer memory. Also, it's better to save the graph as an external image to view it:

Based on the resulting graph, you can see that Netmiko is supporting a lot of vendors such as HP Comware, entrasys, Cisco ASA, Force10, Arista, Avaya, and so on, and all of these classes are are inheriting from the                          netmiko.cisco_base_connection.CicsoSSHConnection parent class (I think this is because they use the same SSH style as Cisco). This in turn inherits from another big parent class called netmiko.cisco_base_connection.BaseConnection.

Also, you can see that Juniper has its own class (netmiko.juniper.juniper_ssh.JuniperSSH) that connects directly to the big parent. Finally, we connect to the parent of all parents in python: the Object class (remember everything in Python is an object in the end).

You can find a lot of interesting things such as an SCP transfer class and SNMP class, and with each one you will find the methods and parameters used to initialize the class.

So the ConnectHandler method is primarily used to check the device_type availability in the vendor classes and, based on returned data, it will use the corresponding SSH class:

Another way to visualize your code is to see exactly which modules and functions are being hit during code execution. This is called profiling and it allows you to examine the functions during runtime.

First, you need to write your code as usual and then right-click on an empty space and select profile instead of running the code as normal:

Wait for the code to be executed. This time PyCharm will inspect each file that is called from your code and generate the call graph for the execution so you can easily know which files and functions are used and count the execution time for each one:

As you can see in the previous graph, our code in profile_code.py (bottom of the graph) will call the ConnectHandler()  function which in turn will execute __init__.py, and execution will continue. On the graph's left side, you can see all files that it touched during your code execution.