我正在尝试创建字典以镜像类中的网络硬件。我已经嵌套了几个对象属性,以便于管理代码:
self.hw_profile = defaultdict(dict)
self.a1k2x_dict = defaultdict(dict)
self.a1k4_dict = defaultdict(dict)
self.a1k6_dict = defaultdict(dict)
self.spa_none = {}
int_map = {'local_int':"", 'local_pc':"", 'opp_dev_desc':"", 'opp_dev_match':"", 'opp_int':""}
self.spa_6x1g = {'spa_name':"6XGE-BUILT-IN",
'int_type' : 'GigabitEthernet',
'int_quant' : 6,
'int_map' : int_map
}
self.spa_1x10g = {'spa_name' : 'SPA-1X10GE-L-V2',
'int_type' : 'TenGigabitEthernet',
'int_quant' : 1,
'int_map' : int_map
}
self.spa_5x1g = {'spa_name' : "SPA-5X1GE-V2",
'int_type' : 'GigabitEthernet',
'int_quant' : 5,
'int_map' : int_map
}
self.a1k6_dict = {'slot0':
{'spa0': self.spa_1x10g,
'spa1': self.spa_none,
'spa2': self.spa_none,
'spa3': self.spa_5x1g},
'slot1':
{'spa0': self.spa_1x10g,
'spa1': self.spa_1x10g,
'spa2': self.spa_none,
'spa3': self.spa_none},
'slot2':
{'spa0': self.spa_1x10g,
'spa1': self.spa_1x10g,
'spa2': self.spa_none,
'spa3': self.spa_none}
}
self.a1k2x_dict = {'slot0':
{'spa0': self.spa_6x1g,
'spa1': self.spa_5x1g,
'spa2': self.spa_none,
'spa3': self.spa_none},
}
现在我想采用这些抽象词典并用特定值填充它们。我尝试使用.get
/ .update
或仅使用self.hw_profile['slot0']['spa1'] = x
等设置新值。
elif self.hw_name == "cisco-asr-1006":
self.hw_profile = self.a1k6_dict
temp_dict1 = {'local_int' : "0/0/0" , 'local_pc':"", 'opp_dev_desc' : "blah1", 'opp_dev_match' :
self.re_core1_match, 'opp_int':"" }
self.hw_profile.get('slot0', {}).get('spa0', {}).get('int_map', {}).update(temp_dict1)
temp_dict2 = {'local_int' : "1/0/0" , 'local_pc':"", 'opp_dev_desc' : "blah2", 'opp_dev_match' :
self.re_core2_match, 'opp_int':"" }
self.hw_profile.get('slot1', {}).get('spa0', {}).get('int_map', {}).update(temp_dict2)
temp_dict3 = {'local_int' : "2/0/0" , 'local_pc':"", 'opp_dev_desc' : "blah", 'opp_dev_match' :
self.re_alg_match, 'opp_int':"" }
self.hw_profile.get('slot2', {}).get('spa0', {}).get('int_map', {}).update(temp_dict3)
我遇到的问题是,无论我做什么,字典中的第一个键似乎都会被忽略,树下的所有分支都会被写入LAST值,如此打印输出所示。词典:
[slot1]
[spa2]
[spa3]
[spa0]
spa_name = SPA-1X10GE-L-V2
[int_map]
opp_dev_desc = opposing ALG
local_int = 2/0/0
local_pc =
opp_dev_match = <_sre.SRE_Pattern object at 0x7f647551b870>
opp_int =
int_quant = 1
int_type = TenGigabitEthernet
[spa1]
spa_name = SPA-1X10GE-L-V2
[int_map]
opp_dev_desc = blah
local_int = 2/0/0
local_pc =
opp_dev_match = <_sre.SRE_Pattern object at 0x7f647551b870>
opp_int =
int_quant = 1
int_type = TenGigabitEthernet
[slot0]
[spa2]
[spa3]
spa_name = SPA-5X1GE-V2
[int_map]
opp_dev_desc = blah
local_int = 2/0/0
local_pc =
opp_dev_match = <_sre.SRE_Pattern object at 0x7f647551b870>
opp_int =
int_quant = 5
int_type = GigabitEthernet
[spa0]
spa_name = SPA-1X10GE-L-V2
[int_map]
opp_dev_desc = opposing ALG
local_int = 2/0/0
local_pc =
opp_dev_match = <_sre.SRE_Pattern object at 0x7f647551b870>
opp_int =
int_quant = 1
int_type = TenGigabitEthernet
[spa1]
[slot2]
[spa2]
[spa3]
[spa0]
spa_name = SPA-1X10GE-L-V2
[int_map]
opp_dev_desc = blah
local_int = 2/0/0
local_pc =
opp_dev_match = <_sre.SRE_Pattern object at 0x7f647551b870>
opp_int =
int_quant = 1
int_type = TenGigabitEthernet
[spa1]
spa_name = SPA-1X10GE-L-V2
[int_map]
opp_dev_desc = opposing ALG
local_int = 2/0/0
local_pc =
opp_dev_match = <_sre.SRE_Pattern object at 0x7f647551b870>
opp_int =
int_quant = 1
int_type = TenGigabitEthernet
我已经尝试了我能想到的一切。我遇到了一个错误,还是我错过了一些基本的东西?