官术网_书友最值得收藏!

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 commandexec-timeout 15 0
  • Cisco NXOS commandexec-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.

主站蜘蛛池模板: 江安县| 沁阳市| 巫山县| 汕尾市| 敦煌市| 长乐市| 浏阳市| 资源县| 德江县| 科技| 辽阳县| 新田县| 二连浩特市| 正镶白旗| 凤山县| 开封县| 平泉县| 辽宁省| 茶陵县| 宁明县| 江北区| 乌鲁木齐市| 兴山县| 灵山县| 万源市| 奉节县| 临汾市| 长治县| 娄底市| 安庆市| 皋兰县| 岳池县| 留坝县| 邵阳县| 辽阳市| 汶上县| 吉木萨尔县| 新化县| 昌江| 武宁县| 五指山市|