Working with Python Modules
A central goal of OOP is to allow you to build modular software that breaks code up into smaller, easier-to-understand pieces. One big file with thousands of lines of code would be extremely difficult to maintain and work with. If you are going to break up your code into functions and classes, you can also separate that code into smaller chunks that hold key structures and classes and allow them to be physically moved into other files, called modules, that can be included in your main Python code with the import statement. Creating modular code provides the following benefits:
Easier readability/maintainability: Code written in a modular fashion is inherently easier to read and follow. It’s like chapters in a book providing groupings of similar concepts and topics. Even the best programmers struggle to understand line after line of code, and modularity makes maintaining and modifying code much easier.
Low coupling/high cohesion: Modular code should be written in such a way that modules do not have interdependencies. Each module should be self-contained so that changes to one module do not affect other modules or code. In addition, a module should only include functions and capabilities related to what the module is supposed to do. When you spread your code around multiple modules, bouncing back and forth, it is really difficult to follow. This paradigm is called low coupling/high cohesion modular design.
Code reusability: Modules allow for easy reusability of your code, which saves you time and makes it possible to share useful code.
Collaboration: You often need to work with others as you build functional code for an organization. Being able to split up the work and have different people work on different modules speeds up the code-production process.
There are a few different ways you can use modules in Python. The first and easiest way is to use one of the many modules that are included in the Python standard library or install one of thousands of third-party modules by using pip. Much of the functionality you might need or think of has probably already been written, and using modules that are already available can save you a lot of time. Another way to use modules is to build them in the Python language by simply writing some code in your editor, giving the file a name, and appending a .py extension. Using your own custom modules does add a bit of processing overhead to your application, as Python is an interpreted language and has to convert your text into machine-readable instructions on the fly. Finally, you can program a module in the C language, compile it, and then add its capabilities to your Python program. Compared to writing your own modules in Python, this method results in faster runtime for your code, but it is a lot more work. Many of the third-party modules and those included as part of the standard library in Python are built this way.
Importing a Module
All modules are accessed the same way in Python: by using the import command. Within a program—by convention at the very beginning of the code—you type import followed by the module name you want to use. The following example uses the math module from the standard library:
>>> import math >>> dir(math) ['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'ldexp', 'lgam- ma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
After you import a module, you can use the dir() function to get a list of all the methods available as part of the module. The ones in the beginning with the __ are internal to Python and are not generally useful in your programs. All the others, however, are functions that are now available for your program to access. As shown in Example 4-4, you can use the help() function to get more details and read the documentation on the math module.
Example 4-4 math Module Help
>>> help(math) Help on module math: NAME math MODULE REFERENCE https://docs.python.org/3.8/library/math The following documentation is automatically generated from the Python source files. It may be incomplete, incorrect or include features that are considered implementation detail and may vary between Python implementations. When in doubt, consult the module reference at the location listed above. DESCRIPTION This module provides access to the mathematical functions defined by the C standard. FUNCTIONS acos(x, /) Return the arc cosine (measured in radians) of x. acosh(x, /) Return the inverse hyperbolic cosine of x. asin(x, /) Return the arc sine (measured in radians) of x. -Snip for brevity-
You can also use help() to look at the documentation on a specific function, as in this example:
>>> help(math.sqrt) Help on built-in function sqrt in module math: sqrt(x, /) Return the square root of x.
If you want to get a square root of a number, you can use the sqrt() method by calling math.sqrt and passing a value to it, as shown here:
>>> math.sqrt(15) 3.872983346207417
You have to type a module’s name each time you want to use one of its capabilities. This isn’t too painful if you’re using a module with a short name, such as math, but if you use a module with a longer name, such as the calendar module, you might wish you could shorten the module name. Python lets you do this by adding as and a short version of the module name to the end of the import command. For example, you can use this command to shorten the name of the calendar module to cal.
>>> import calendar as cal
Now you can use cal as an alias for calendar in your code, as shown in this example:
>>> print(cal.month(2020, 2, 2, 1)) February 2020 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
Importing a whole module when you need only a specific method or function adds unneeded overhead. To help with this, Python allows you to import specific methods by using the from syntax. Here is an example of importing the sqrt() and tan() methods:
>>> from math import sqrt,tan >>> sqrt(15) 3.872983346207417
As you can see here, you can import more than one method by separating the methods you want with commas.
Notice that you no longer have to use math.sqrt and can just call sqrt() as a function, since you imported only the module functions you needed. Less typing is always a nice side benefit.
The Python Standard Library
The Python standard library, which is automatically installed when you load Python, has an extensive range of prebuilt modules you can use in your applications. Many are built in C and can make life easier for programmers looking to solve common problems quickly. Throughout this book, you will see many of these modules used to interact programmatically with Cisco infrastructure. To get a complete list of the modules in the standard library, go to at https://docs.python.org/3/library/. This documentation lists the modules you can use and also describes how to use them.
Importing Your Own Modules
As discussed in this chapter, modules are Python files that save you time and make your code readable. To save the class example from earlier in this chapter as a module, you just need to save all of the code for defining the class and the attributes and functions as a separate file with the .py extension. You can import your own modules by using the same methods shown previously with standard library modules. By default, Python looks for a module in the same directory as the Python program you are importing into. If it doesn’t find the file there, it looks through your operating system’s path statements. To print out the paths your OS will search through, consider this example of importing the sys module and using the sys.path method:
>>> import sys >>> sys.path ['', '/Users/chrijack/Documents', '/Library/Frameworks/Python. framework/Versions/3.8/lib/python38.zip', '/Library/Frameworks/ Python.framework/Versions/3.8/lib/python3.8', '/Library/Frameworks/ Python.framework/Versions/3.8/lib/python3.8/lib-dynload', '/ Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ site-packages']
Depending on your OS (this output is from a Mac), the previous code might look different from what you see here, but it should still show you what Python sees, so it is useful if you are having trouble importing a module.
If you remove the class from the code shown in Example 4-2 and store it in a separate file named device.py, you can import the classes from your new module and end up with the following program, which is a lot more readable while still operating exactly the same:
from device import Router, Switch rtr1 = Router('iosV', '15.6.7', '10.10.10.1') rtr2 = Router('isr4221', '16.9.5', '10.10.10.5') sw1 = Switch('Cat9300', '16.9.5', '10.10.10.8') print('Rtr1\n', rtr1.getdesc(), '\n', sep='') print('Rtr2\n', rtr2.getdesc(), '\n', sep='') print('Sw1\n', sw1.getdesc(), '\n', sep='')
When you execute this program, you get the output shown in Example 4-5. If you compare these results with the results shown in Example 4-3, you see that they are exactly the same. Therefore, the device module is just Python code that is stored in another file but used in your program.
Example 4-5 Code Results of device.py Import as a Module
Rtr1 Router Model :iosV Software Version :15.6.7 Router Management Address:10.10.10.1 Rtr2 Router Model :isr4221 Software Version :16.9.5 Router Management Address:10.10.10.5 Sw1 Switch Model :Cat9300 Software Version :16.9.5 Router Management Address:10.10.10.8
Useful Python Modules for Cisco Infrastructure
This chapter cannot cover every single module that you might find valuable when writing Python code to interact with Cisco infrastructure. As you become more familiar with Python, you will come to love and trust a wide range of standard library and third-party modules. The following list includes many that are widely used to automate network infrastructure. Many of these modules are used throughout this book, so you will be able to see them in action. The following list provides a description of each one, how to install it (if it is not part of the standard library), and the syntax to use in your Python import statement:
General-purpose standard library modules:
pprint: The pretty print module is a more intelligent print function that makes it much easier to display text and data by, for example, aligning data for better readability. Use the following command to import this module:
from pprint import pprint
sys: This module allows you to interact with the Python interpreter and manipulate and view values. Use the following command to import this module:
import sys
os: This module gives you access to the underlying operating system environment and file system. It allows you to open files and interact with OS variables. Use the following command to import this module:
import os
datetime: This module allows you to create, format, and work with calendar dates and time. It also enables timestamps and other useful additions to logging and data. Use the following command to import this module:
import datetime
time: This module allows you to add time-based delays and clock capabilities to your Python apps. Use the following command to import this module:
import time
Modules for working with data:
xmltodict: This module translates XML-formatted files into native Python dictionaries (key/value pairs) and back to XML, if needed. Use the following command to install this module:
pip install xmltodict
Use the following command to import this module:
import xmltodict
csv: This is a standard library module for understanding CSV files. It is useful for exporting Excel spreadsheets into a format that you can then import into Python as a data source. It can, for example, read in a CSV file and use it as a Python list data type. Use the following command to import this module:
import csv
json: This is a standard library module for reading JSON-formatted data sources and easily converting them to dictionaries. Use the following command to import this module:
import json
PyYAML: This module converts YAML files to Python objects that can be converted to Python dictionaries or lists. Use the following command to install this module:
pip install PyYAML
Use the following command to import this module:
import yaml
pyang: This isn’t a typical module you import into a Python program. It’s a utility written in Python that you can use to verify your YANG models, create YANG code, and transform YANG models into other data structures, such as XSD (XML Schema Definition). Use the following command to install this module:
pip install pyang
Tools for API interaction:
requests: This is a full library to interact with HTTP services and used extensively to interact with REST APIs. Use the following command to install this module:
pip install requests
Use the following command to import this module:
import requests
ncclient: This Python library helps with client-side scripting and application integration for the NETCONF protocol. Use the following command to install this module:
pip install ncclient
Use the following command to import this module:
from ncclient import manager
netmiko: This connection-handling library makes it easier to initiate SSH connections to network devices. This module is intended to help bridge the programmability gap between devices with APIs and those without APIs that still rely on command-line interfaces and commands. It relies on the paramiko module and works with multiple vendor platforms. Use the following command to install this module:
pip install netmiko
Use the following command to import this module:
from netmiko import ConnectHandler
pysnmp: This is a Python implementation of an SNMP engine for network management. It allows you to interact with older infrastructure components without APIs but that do support SNMP for management. Use the following command to install this module:
pip install pysnmp
Use the following command to import this module:
import pysnmp
Automation tools:
napalm: napalm (Network Automation and Programmability Abstraction Layer with Multivendor Support) is a Python module that provides functionality that works in a multivendor fashion. Use the following command to install this module:
pip install napalm
Use the following command to import this module:
import napalm
nornir: This is an extendable, multithreaded framework with inventory management to work with large numbers of network devices. Use the following command to install this module:
pip install nornir
Use the following command to import this module:
from nornir.core import InitNornir
Testing tools:
unittest: This standard library testing module is used to test the functionality of Python code. It is often used for automated code testing and as part of test-driven development methodologies. Use the following command to import this module:
import unittest
pyats: This module was a gift from Cisco to the development community. Originally named Genie, it was an internal testing framework used by Cisco developers to validate their code for Cisco products. pyats is an incredible framework for constructing automated testing for infrastructure as code. Use the following command to install this module:
pip install pyats (just installs the core framework, check documentation for more options)
Many parts of the pyats framework can be imported. Check the documentation on how to use it.
Chapter 5, “Working with Data in Python,” places more focus on techniques and tools used to interact with data in Python. This will round out the key Python knowledge needed to follow along with the examples in the rest of the book.