- Practical Network Automation
- Abhishek Ratan
- 176字
- 2021-07-02 14:53:08
Making the right choice
In this example, we will use a switch case to identify the right set of configurations based upon certain input given by the user.
As a prerequisite understanding, the syntax of the exec-timeout command based upon OS is as follows:
- Cisco IOS command: exec-timeout 15 0
- Cisco NXOS command: exec-timeout 15
#create a dictionary:
config={
"IOS":"exec-timeout 15 0",
"NXOS":"exec-timeout 15"
}
getchoice=input("Enter IOS type (IOS/NXOS) : ")
if (getchoice == "IOS"):
print (config.get("IOS"))
if (getchoice == "NXOS"):
print (config.get("NXOS"))
The sample output is as follows:
>
Enter IOS type (IOS/NXOS) : IOS
exec-timeout 15 0
>>>
Enter IOS type (IOS/NXOS) : NXOS
exec-timeout 15
>>>
In the preceding example, we have tackled a common challenge of using a switch case in Python. Unlike some other languages, Python does not provide a switch case statement, hence we need to use a dictionary to overcome this. Using this approach, we can remove the usage of multiple if statements and directly call the dictionary values based upon the mappings done in the dictionary.
推薦閱讀
- Project 2007項目管理實用詳解
- 自動化控制工程設計
- 計算機網(wǎng)絡技術實訓
- 控制系統(tǒng)計算機仿真
- 完全掌握AutoCAD 2008中文版:機械篇
- 空間站多臂機器人運動控制研究
- Mastering Game Development with Unreal Engine 4(Second Edition)
- 在實戰(zhàn)中成長:C++開發(fā)之路
- 會聲會影X4中文版從入門到精通
- 重估:人工智能與賦能社會
- 常用傳感器技術及應用(第2版)
- Effective Business Intelligence with QuickSight
- 網(wǎng)絡安全概論
- MySQL Management and Administration with Navicat
- Learning OpenShift