python dict使用唯一键覆盖其他值
我正在制作一个图形模型来存储来自nmap扫描的扫描。有一个GraphModel、Vertex和Edge类,NmapDataModel类充当GraphModel的包装器。当我将多个ip地址摄入其中时,它会获取最新的ip和顶点属性并覆盖这些属性,但保留正确的键值(这是使顶点唯一的值的散列)。任何帮助都将不胜感激…拔出我的头发。。。 主程序。第52行的打印语句显示了问题 编辑:我遇到的问题是每个键都是唯一的,但共享图模型中的所有值都采用使用NmapDataModel.add_()函数编写的最后一个值。密钥是唯一的,它们是通过标签和ipv4地址创建的哈希python dict使用唯一键覆盖其他值,python,dictionary,overwrite,Python,Dictionary,Overwrite,我正在制作一个图形模型来存储来自nmap扫描的扫描。有一个GraphModel、Vertex和Edge类,NmapDataModel类充当GraphModel的包装器。当我将多个ip地址摄入其中时,它会获取最新的ip和顶点属性并覆盖这些属性,但保留正确的键值(这是使顶点唯一的值的散列)。任何帮助都将不胜感激…拔出我的头发。。。 主程序。第52行的打印语句显示了问题 编辑:我遇到的问题是每个键都是唯一的,但共享图模型中的所有值都采用使用NmapDataModel.add_()函数编写的最后一个值。
class NmapDataModel(object):
def __init__(self):
self.graphmodel = graph.GraphModel()
def add_ipv4(self, ipAddress, properties={}):
properties['ipv4'] = ipAddress
v = graph.Vertex('IP', properties=properties, unique_by=['ipv4'])
return self.graphmodel.add_vertex(v)
class NmapInterface(object):
def build_model(self, filename=None):
dm = NmapDataModel()
tree = ET.parse(filename)
root = tree.getroot()
for host in root.findall('host'):
ipAddressFromHostStuff = some process that gets the IP there
dm.add_ipv4(ipAddressFromHostStuff)
for item in dm.graphmodel.vertices:
print(item, dm.graphmodel.vertices[item])
return dm.graphmodel
class GraphModel(object):
def __init__(self, graphDict=None):
self.vertices = dict()
self.edges = dict()
def add_vertex(self, vertex):
if(not vertex.id in self.vertices):
self.vertices.update({vertex.id:vertex})
else:
self.vertices[vertex.id].properties.update(vertex.properties)
return self.vertices[vertex.id]
class Vertex(object):
def __init__(self, label, properties={}, unique_by=[]):
self.label = label
id = label
if(unique_by):
for item in unique_by:
id += ':' + str(properties.get(item,'None'))
else:
for item in properties.keys():
id += ':' + str(properties.get(item,'None'))
id = hashlib.md5(id.encode('utf-8')).hexdigest()[0:10]
properties['hashcode'] = id
self.id = id
self.properties = properties
示例输出
============
192c6e67b6 IP:192c6e67b6:{'ipv4': '10.11.1.5', 'hashcode': '192c6e67b6'}
============
192c6e67b6 IP:192c6e67b6:{'ipv4': '10.11.1.7', 'hashcode': '9a4b1c6c9f'}
9a4b1c6c9f IP:9a4b1c6c9f:{'ipv4': '10.11.1.7', 'hashcode': '9a4b1c6c9f'}
============
192c6e67b6 IP:192c6e67b6:{'ipv4': '10.11.1.8', 'hashcode': 'fdb4251f16'}
9a4b1c6c9f IP:9a4b1c6c9f:{'ipv4': '10.11.1.8', 'hashcode': 'fdb4251f16'}
fdb4251f16 IP:fdb4251f16:{'ipv4': '10.11.1.8', 'hashcode': 'fdb4251f16'}
============
192c6e67b6 IP:192c6e67b6:{'ipv4': '10.11.1.10', 'hashcode': '6c5d6cc9ec'}
9a4b1c6c9f IP:9a4b1c6c9f:{'ipv4': '10.11.1.10', 'hashcode': '6c5d6cc9ec'}
fdb4251f16 IP:fdb4251f16:{'ipv4': '10.11.1.10', 'hashcode': '6c5d6cc9ec'}
6c5d6cc9ec IP:6c5d6cc9ec:{'ipv4': '10.11.1.10', 'hashcode': '6c5d6cc9ec'}
============
192c6e67b6 IP:192c6e67b6:{'ipv4': '10.11.1.13', 'hashcode': '12284ae326'}
9a4b1c6c9f IP:9a4b1c6c9f:{'ipv4': '10.11.1.13', 'hashcode': '12284ae326'}
fdb4251f16 IP:fdb4251f16:{'ipv4': '10.11.1.13', 'hashcode': '12284ae326'}
6c5d6cc9ec IP:6c5d6cc9ec:{'ipv4': '10.11.1.13', 'hashcode': '12284ae326'}
12284ae326 IP:12284ae326:{'ipv4': '10.11.1.13', 'hashcode': '12284ae326'}
============
192c6e67b6 IP:192c6e67b6:{'ipv4': '10.11.1.14', 'hashcode': 'b8e17aacb6'}
9a4b1c6c9f IP:9a4b1c6c9f:{'ipv4': '10.11.1.14', 'hashcode': 'b8e17aacb6'}
fdb4251f16 IP:fdb4251f16:{'ipv4': '10.11.1.14', 'hashcode': 'b8e17aacb6'}
6c5d6cc9ec IP:6c5d6cc9ec:{'ipv4': '10.11.1.14', 'hashcode': 'b8e17aacb6'}
12284ae326 IP:12284ae326:{'ipv4': '10.11.1.14', 'hashcode': 'b8e17aacb6'}
b8e17aacb6 IP:b8e17aacb6:{'ipv4': '10.11.1.14', 'hashcode': 'b8e17aacb6'}
============
192c6e67b6 IP:192c6e67b6:{'ipv4': '10.11.1.22', 'hashcode': '8ec5f874b7'}
9a4b1c6c9f IP:9a4b1c6c9f:{'ipv4': '10.11.1.22', 'hashcode': '8ec5f874b7'}
fdb4251f16 IP:fdb4251f16:{'ipv4': '10.11.1.22', 'hashcode': '8ec5f874b7'}
6c5d6cc9ec IP:6c5d6cc9ec:{'ipv4': '10.11.1.22', 'hashcode': '8ec5f874b7'}
12284ae326 IP:12284ae326:{'ipv4': '10.11.1.22', 'hashcode': '8ec5f874b7'}
b8e17aacb6 IP:b8e17aacb6:{'ipv4': '10.11.1.22', 'hashcode': '8ec5f874b7'}
8ec5f874b7 IP:8ec5f874b7:{'ipv4': '10.11.1.22', 'hashcode': '8ec5f874b7'}
============
192c6e67b6 IP:192c6e67b6:{'ipv4': '10.11.1.24', 'hashcode': '635dcc2038'}
9a4b1c6c9f IP:9a4b1c6c9f:{'ipv4': '10.11.1.24', 'hashcode': '635dcc2038'}
fdb4251f16 IP:fdb4251f16:{'ipv4': '10.11.1.24', 'hashcode': '635dcc2038'}
6c5d6cc9ec IP:6c5d6cc9ec:{'ipv4': '10.11.1.24', 'hashcode': '635dcc2038'}
12284ae326 IP:12284ae326:{'ipv4': '10.11.1.24', 'hashcode': '635dcc2038'}
b8e17aacb6 IP:b8e17aacb6:{'ipv4': '10.11.1.24', 'hashcode': '635dcc2038'}
8ec5f874b7 IP:8ec5f874b7:{'ipv4': '10.11.1.24', 'hashcode': '635dcc2038'}
635dcc2038 IP:635dcc2038:{'ipv4': '10.11.1.24', 'hashcode': '635dcc2038'}
============
192c6e67b6 IP:192c6e67b6:{'ipv4': '10.11.1.31', 'hashcode': 'c7daaefc8d'}
9a4b1c6c9f IP:9a4b1c6c9f:{'ipv4': '10.11.1.31', 'hashcode': 'c7daaefc8d'}
fdb4251f16 IP:fdb4251f16:{'ipv4': '10.11.1.31', 'hashcode': 'c7daaefc8d'}
6c5d6cc9ec IP:6c5d6cc9ec:{'ipv4': '10.11.1.31', 'hashcode': 'c7daaefc8d'}
12284ae326 IP:12284ae326:{'ipv4': '10.11.1.31', 'hashcode': 'c7daaefc8d'}
b8e17aacb6 IP:b8e17aacb6:{'ipv4': '10.11.1.31', 'hashcode': 'c7daaefc8d'}
8ec5f874b7 IP:8ec5f874b7:{'ipv4': '10.11.1.31', 'hashcode': 'c7daaefc8d'}
635dcc2038 IP:635dcc2038:{'ipv4': '10.11.1.31', 'hashcode': 'c7daaefc8d'}
c7daaefc8d IP:c7daaefc8d:{'ipv4': '10.11.1.31', 'hashcode': 'c7daaefc8d'}
============
192c6e67b6 IP:192c6e67b6:{'ipv4': '10.11.1.35', 'hashcode': '53273bad7d'}
9a4b1c6c9f IP:9a4b1c6c9f:{'ipv4': '10.11.1.35', 'hashcode': '53273bad7d'}
fdb4251f16 IP:fdb4251f16:{'ipv4': '10.11.1.35', 'hashcode': '53273bad7d'}
6c5d6cc9ec IP:6c5d6cc9ec:{'ipv4': '10.11.1.35', 'hashcode': '53273bad7d'}
12284ae326 IP:12284ae326:{'ipv4': '10.11.1.35', 'hashcode': '53273bad7d'}
b8e17aacb6 IP:b8e17aacb6:{'ipv4': '10.11.1.35', 'hashcode': '53273bad7d'}
8ec5f874b7 IP:8ec5f874b7:{'ipv4': '10.11.1.35', 'hashcode': '53273bad7d'}
635dcc2038 IP:635dcc2038:{'ipv4': '10.11.1.35', 'hashcode': '53273bad7d'}
c7daaefc8d IP:c7daaefc8d:{'ipv4': '10.11.1.35', 'hashcode': '53273bad7d'}
53273bad7d IP:53273bad7d:{'ipv4': '10.11.1.35', 'hashcode': '53273bad7d'}
============
192c6e67b6 IP:192c6e67b6:{'ipv4': '10.11.1.39', 'hashcode': '021890997d'}
9a4b1c6c9f IP:9a4b1c6c9f:{'ipv4': '10.11.1.39', 'hashcode': '021890997d'}
fdb4251f16 IP:fdb4251f16:{'ipv4': '10.11.1.39', 'hashcode': '021890997d'}
6c5d6cc9ec IP:6c5d6cc9ec:{'ipv4': '10.11.1.39', 'hashcode': '021890997d'}
12284ae326 IP:12284ae326:{'ipv4': '10.11.1.39', 'hashcode': '021890997d'}
b8e17aacb6 IP:b8e17aacb6:{'ipv4': '10.11.1.39', 'hashcode': '021890997d'}
8ec5f874b7 IP:8ec5f874b7:{'ipv4': '10.11.1.39', 'hashcode': '021890997d'}
635dcc2038 IP:635dcc2038:{'ipv4': '10.11.1.39', 'hashcode': '021890997d'}
c7daaefc8d IP:c7daaefc8d:{'ipv4': '10.11.1.39', 'hashcode': '021890997d'}
53273bad7d IP:53273bad7d:{'ipv4': '10.11.1.39', 'hashcode': '021890997d'}
021890997d IP:021890997d:{'ipv4': '10.11.1.39', 'hashcode': '021890997d'}
这里的代码太多了。你能不能把它归结为一个问题?这个问题是针对neo4j模块和图形的,还是你能在没有第三方库的情况下演示这个问题?代码太多,几乎没有上下文。但我得到的印象是,你还不明白,在字典中,键必须是唯一的。