Lab 2 of 5
Β·
Semi-Guided
Create Your Own device_utils Module
Objective: Write a Python module with two reusable functions for processing device data.
Instructions
Create a file called device_utils.py with two functions:
format_hostname(name)β strips whitespace and lowercases the nameget_device_type(hostname)β returns"switch"if the name starts withsw,"router"if it starts withrtr,"firewall"if it starts withfw, otherwise"unknown"
Then create a file called lab2.py that imports device_utils and runs these calls:
import device_utils
print(device_utils.format_hostname(" SW-CORE-01 "))
print(device_utils.get_device_type("rtr-edge-01"))
print(device_utils.get_device_type("sw-access-01"))
Run python3 lab2.py. Paste your output below and submit.
Starter Code
# device_utils.py β paste this into device_utils.py (not lab2.py)
def format_hostname(name):
return name.strip().lower()
def get_device_type(hostname):
if hostname.startswith("sw"):
return "switch"
elif hostname.startswith("rtr"):
return "router"
elif hostname.startswith("fw"):
return "firewall"
else:
return "unknown"
π‘ Show Hint
Create two separate files: device_utils.py (the module) and lab2.py (the script that imports it). Both must be in the same directory.
Paste Your Output