Skip to content

Lab 08 - Using Python Modules

In programming languages, you often need to "import" other libraries to get additional functionalities. In this lab, we'll explore using a few Python libraries that come with the standard installation of Python.

Note that a Python library may be a Python module (which is a file that has Python code) or a Python package (which is a collection of Python modules)

Task 1 - Use the json module

Step 1-1

Create a dictionary called facts:

>>> facts = {'platform': 'nexus', 'version': '7.3', 'vendor': 'cisco', 'device_type': 'switch', 'os': 'nxos'}
>>>

Step 1-2

Print the dictionary:

>>> print(facts)
{'platform': 'nexus', 'version': '7.3', 'vendor': 'cisco', 'device_type': 'switch', 'os': 'nxos'}
>>>

The larger the dictionary, the harder this will be to read.

Let's introduce a Python module that helps in "pretty" printing Python objects like dictionaries (i.e. printing data in a more human readable format).

Step 1-3

First, import the json module using the import keyword:

>>> import json
>>>

Step 1-4

Using the dumps method in the json module to pretty print the dictionary. We'll use an indent of 4.

>>> print(json.dumps(facts, indent=4))
{
    "platform": "nexus",
    "version": "7.3",
    "vendor": "cisco",
    "device_type": "switch",
    "os": "nxos"
}
>>>

Step 1-5

We'll use 4 spaces as a sane default for every example in the course, but feel free to try 10 and 20 and see what happens:

>>> print(json.dumps(facts, indent=10))
{
          "platform": "nexus",
          "version": "7.3",
          "vendor": "cisco",
          "device_type": "switch",
          "os": "nxos"
}
>>>
>>> print(json.dumps(facts, indent=20))
{
                    "platform": "nexus",
                    "version": "7.3",
                    "vendor": "cisco",
                    "device_type": "switch",
                    "os": "nxos"
}
>>>

Using json.dumps is going to help tremendously when trying to read larger dictionary objects in the next section.

Task 2 - Use the time module

When you're writing a Python script, you may need to insert a pause or delay. This may be needed if you're performing many operations on a device, or need to give a device time to perform a calculation.

You can do this with the time module. Specifically the sleep function. Let's see how.

Step 2-1

Import the time module:

>>> import time
>>>

Step 2-2

Insert a pause for 5 seconds using the sleep function:

>>> time.sleep(5)
>>>

Notice how the interpreter hangs for 5 seconds. This comes in handy when you're writing Python scripts (which we'll do soon).

Step 2-3

Explore printing the date and time with the time module.

First, do a dir(time). You'll see there are many objects within the time module.

Here is one example of how to print the local time:

>>> local_time = time.asctime()
>>>
>>> print(local_time)
Tue Nov  9 17:20:33 2021
>>>

Feel free to use the help function on the individual objects above.

Task 3 - Use the os module

This task shows how you can access the underlying operating system ("os") of a given device.

Step 3-1

Import the os module:

>>> import os
>>>

Step 3-2

You can check to see your current working directory with the getcwd() function:

>>> os.getcwd()
'/home/ntc'

Step 3-3

You can also change your working directory with chdir():

>>> os.chdir('/home/ntc/files')
>>>
>>> os.getcwd()
'/home/ntc/files'
>>>

Step 3-4

You can also access your OS ENVIRONMENT variables. Here we're accessing an environment variable called HOME:

>>> os.getenv('HOME')
'/home/ntc'
>>>

In a separate Linux terminal window, you can type env to see other environment variables you can check from Python.

Step 3-5

You can also list the contents of a given directory from Python:

>>> os.listdir('/home/ntc')
# output omitted
>>>

Step 3-6

You can even issue arbitrary Linux commands from Python:

>>> os.system('date')
Tue Nov  9 17:23:20 UTC 2021
0
>>>
>>> os.system('ifconfig')
# output omitted
>>>

Keep in mind, this lab is just showing how to get started with Python modules. It's not an exhaustive list of what's possible and it's not making recommendations to do things like executing Linux commands from Python.

However, you can view the standard Python docs for a module like os here:

https://docs.python.org/3.8/library/os.html

Note: We'll continue to use even more Python modules throughout the course.