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

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.

主站蜘蛛池模板: 微山县| 剑阁县| 衡山县| 辽宁省| 寻甸| 洛川县| 九龙县| 武平县| 莲花县| 天水市| 绥阳县| 绩溪县| 明星| 忻城县| 涡阳县| 察隅县| 沛县| 普兰县| 锦州市| 济宁市| 福贡县| 阜阳市| 准格尔旗| 治县。| 金昌市| 巴林左旗| 平度市| 鹤山市| 邛崃市| 阿尔山市| 高安市| 大埔县| 航空| 左权县| 淮北市| 平和县| 桦川县| 辽阳县| 安宁市| 南漳县| 平阴县|