Use the dir() function to list all function names in a Python module.
returns a list of names in the current local scope or a list of attributes of an object
when a module is passed as an argument,
returns a list of the module’s attributes, including its functions
Example
import mathfunctions = [name for name in dir(math) if callable(getattr(math, name))]print(functions)
dir(math) returns a list of all attributes of the math module
the list comprehension filters this list to include only callable attributes (i.e., functions)
the result is a list of all function names
this method can be applied to any module to list its functions
this will also include built-in functions and classes
to exclude, need to use additional filtering criteria
Import from a Module
Specific parts of a module can be imported using the from ... import ... statement.
allows for importing only the necessary functions or classes
from network_tools import pingip = '192.168.1.1'if ping(ip): print(f'Device {ip} is online.')else: print(f'Device {ip} is offline.')
only the ping function is imported from the module
The ipaddress Module
The ipaddress module is a powerful tool for manipulating and analyzing IP addresses and networks.
provides classes for handling IPv4 and IPv6 addresses, networks, and interfaces
support validation, comparison, sorting, and other operations
automatically handles intricacies of IP addressing, such as excluding the network and broadcast addresses
Example
import ipaddress# Define the networknetwork = ipaddress.ip_network('192.0.2.0/24')# Iterate over all hosts in the networkfor host in network.hosts(): print(host)
ipaddress.ip_network('192.0.2.0/24') creates an IPv4 object
network.hosts() method is used to iterate over all hosts in the network
each host is printed to the console
The help Module
The help() function in Python is a built-in function that can be used to access the built-in documentation for Python modules, functions, classes, keywords, etc.
most commonly used in the interpreter
e.g., help(print)
displays documentation for print function
can be used on Python modules
import oshelp(os)
Python Libraries
Install a Library
Python libraries can be installed using package managers like pip.
syntax: pip install library_name
recommended to use a virtual environment to avoid conflicts between libraries
upgrade a library: pip install --upgrade library_name
upgrade pip to latest version before install libraries
Standard Library
The Standard Library is a collection of modules and packages that come pre-installed with Python.
available in most Python installations
designed to enhance Python’s usability and standardize solutions for common programming tasks
Running a Library
first import it into a Python script
then call its functions or classes
Example
import socket# Create a socket objects = socket.socket(socket.AF_INET, socket.SOCK_STREAM)# Define the server and the portserver = 'hostname'port = 80# Connect to the servers.connect((server, port))
socket library is imported
socket function is used to create a socket object
connect method is used to establish a connection to the server
Python Packages
Listing a Package
Use pip list in CLI to return a list of all installed packages and their versions.
to check for a specific package: pip show package_name
Installing a Package
Install packages using pip with the pip install package_name command in the CLI.
recommended to use a virtual environment to avoid conflicts between packages
upgrade packages with pip install --upgrade package_name
ensure pip is upgraded before installing packages
Using a Package
import the package into a Python script
access functions and classes using dot notation
Example
from netmiko import ConnectHandlerdevice = { 'device_type': 'cisco_ios', 'ip': '10.0.0.1', 'username': 'admin', 'password': 'password',}connection = ConnectHandler(**device)output = connection.send_command('show ip int brief')print(output)
the ConnectHandler class from the netmiko package is used to establish an SSH connection to a device
send_command method is then used to send a command to the device and print the output
Removing a Package
Remove/uninstall a package with pip uninstall package_name in the CLI.