gpt4 book ai didi

python - 灯泡:仅在尚不存在时创建顶点

转载 作者:太空宇宙 更新时间:2023-11-03 18:21:01 25 4
gpt4 key购买 nike

我正在使用灯泡来创建顶点。我编写了下面给出的 3 个函数来执行相同的操作。只是为了帮助您理解我在每个函数中的调试行输出下方列出的顶点属性。

DEBUG LINE:
self.logger.info('%s',self.switches)
OUTPUT:
00:00:00:00:00:00:02:01
00:00:00:00:00:00:02:02
00:00:00:00:00:00:02:03
00:00:00:00:00:00:02:04
00:00:00:00:00:00:02:05
00:00:00:00:00:00:02:06

DEBUG LINE:
self.logger.info('%s',self.ports)
OUTPUT:
{'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:011', 'desc': 's1-eth1'}
{'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:012', 'desc': 's1-eth2'}
{'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:021', 'desc': 's2-eth1'}
{'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:022', 'desc': 's2-eth2'}
{'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:031', 'desc': 's3-eth1'}
{'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:032', 'desc': 's3-eth2'}
{'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:041', 'desc': 's4-eth1'}
{'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:042', 'desc': 's4-eth2'}
{'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:051', 'desc': 's5-eth1'}
{'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:052', 'desc': 's5-eth2'}
{'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:061', 'desc': 's6-eth1'}
{'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:062', 'desc': 's6-eth2'}

DEBUG LINE:
self.logger.info('%s',self.devices)
OUTPUT:
{'dl_addr': '00:00:00:00:00:04', 'state': 'active', 'type': 'device'}
{'dl_addr': '00:00:00:00:00:01', 'state': 'active', 'type': 'device'}
{'dl_addr': '00:00:00:00:00:03', 'state': 'active', 'type': 'device'}
{'dl_addr': '00:00:00:00:00:05', 'state': 'active', 'type': 'device'}
{'dl_addr': '00:00:00:00:00:02', 'state': 'active', 'type': 'device'}
{'dl_addr': '00:00:00:00:00:06', 'state': 'active', 'type': 'device'}

def create_switch_vertices(self):
for index,switch in enumerate(self.switch_list):
dpid_str = dpid_to_str(switch.dp.id)
dpid_str = ':'.join([dpid_str[i:i + 2] for i in range(0, len(dpid_str), 2)])
self.switches = {'state':'active','dpid':dpid_str,'type':'switch'}
self.logger.info('%s',dpid_str)
self.sw_vertices.append(self.g.vertices.create({'state':'active','dpid':dpid_str,'type':'switch'}))


def create_port_vertices(self):
for index,switch in enumerate(self.switch_list):
for port in switch.ports:
desc = port.name
hw_addr = port.hw_addr
state = 'active'
port_state = 1
number = port.port_no
dpid_str = dpid_to_str(port.dpid)
dpid_s = ':'.join([dpid_str[i:i + 2] for i in range(0, len(dpid_str), 2)])
port_id = dpid_s + str(number)
self.ports = {'desc':desc,'port_id':port_id,'state':state,'port_state':port_state,'number':number,'type':'port'}
self.logger.info('%s',self.ports)
self.port_vertices.append(self.g.vertices.create({'desc':desc,'port_id':port_id,'state':state,'port_state':port_state,'number':number,'type':'port'}))

def create_host_vertices(self):
for value in switch_port_to_host.values():
self.devices = {'state':'active','dl_addr':value,'type':'device'}
self.logger.info('%s',self.devices)
self.host_vertices.append(self.g.vertices.create({'state':'active','dl_addr':value,'type':'device'}))

我的问题如下。

1) 我打开了运行此代码后打开的 rexster shell。如果我这样做

g = rexster.getGraph("graph")
==>titangraph[embeddedcassandra:null]

g.V('type', 'device').map
==>{state=active, type=device, dl_addr=00:00:00:00:00:05}
==>{state=active, type=device, dl_addr=00:00:00:00:00:06}
==>{state=active, type=device, dl_addr=00:00:00:00:00:04}
==>{state=active, type=device, dl_addr=00:00:00:00:00:03}
==>{state=active, type=device, dl_addr=00:00:00:00:00:02}
==>{state=active, type=device, dl_addr=00:00:00:00:00:05}
==>{state=active, type=device, dl_addr=00:00:00:00:00:06}
==>{state=active, type=device, dl_addr=00:00:00:00:00:04}
==>{state=active, type=device, dl_addr=00:00:00:00:00:01}
==>{state=active, type=device, dl_addr=00:00:00:00:00:03}
==>{state=active, type=device, dl_addr=00:00:00:00:00:01}
==>{state=active, type=device, dl_addr=00:00:00:00:00:02}

g.V('type', 'switch').map
==>{state=active, dpid=00:00:00:00:00:00:02:06, type=switch}
==>{state=active, dpid=00:00:00:00:00:00:02:05, type=switch}
==>{state=active, dpid=00:00:00:00:00:00:02:02, type=switch}
==>{state=active, dpid=00:00:00:00:00:00:02:04, type=switch}
==>{state=active, dpid=00:00:00:00:00:00:02:06, type=switch}
==>{state=active, dpid=00:00:00:00:00:00:02:03, type=switch}
==>{state=active, dpid=00:00:00:00:00:00:02:01, type=switch}
==>{state=active, dpid=00:00:00:00:00:00:02:04, type=switch}
==>{state=active, dpid=00:00:00:00:00:00:02:01, type=switch}
==>{state=active, dpid=00:00:00:00:00:00:02:05, type=switch}
==>{state=active, dpid=00:00:00:00:00:00:02:02, type=switch}
==>{state=active, dpid=00:00:00:00:00:00:02:03, type=switch}

g.V('type', 'port').map
==>{desc=s5-eth1, port_id=00:00:00:00:00:00:02:051, state=active, port_state=1, number=1, type=port}
==>{desc=s5-eth1, port_id=00:00:00:00:00:00:02:051, state=active, port_state=1, number=1, type=port}
==>{desc=s1-eth1, port_id=00:00:00:00:00:00:02:011, state=active, port_state=1, number=1, type=port}
==>{desc=s2-eth1, port_id=00:00:00:00:00:00:02:021, state=active, port_state=1, number=1, type=port}
==>{desc=s6-eth1, port_id=00:00:00:00:00:00:02:061, state=active, port_state=1, number=1, type=port}
==>{desc=s3-eth1, port_id=00:00:00:00:00:00:02:031, state=active, port_state=1, number=1, type=port}
==>{desc=s3-eth2, port_id=00:00:00:00:00:00:02:032, state=active, port_state=1, number=2, type=port}
==>{desc=s1-eth2, port_id=00:00:00:00:00:00:02:012, state=active, port_state=1, number=2, type=port}
==>{desc=s3-eth1, port_id=00:00:00:00:00:00:02:031, state=active, port_state=1, number=1, type=port}
==>{desc=s4-eth1, port_id=00:00:00:00:00:00:02:041, state=active, port_state=1, number=1, type=port}
==>{desc=s6-eth2, port_id=00:00:00:00:00:00:02:062, state=active, port_state=1, number=2, type=port}
==>{desc=s1-eth2, port_id=00:00:00:00:00:00:02:012, state=active, port_state=1, number=2, type=port}
==>{desc=s3-eth2, port_id=00:00:00:00:00:00:02:032, state=active, port_state=1, number=2, type=port}
==>{desc=s6-eth2, port_id=00:00:00:00:00:00:02:062, state=active, port_state=1, number=2, type=port}
==>{desc=s4-eth2, port_id=00:00:00:00:00:00:02:042, state=active, port_state=1, number=2, type=port}
==>{desc=s5-eth2, port_id=00:00:00:00:00:00:02:052, state=active, port_state=1, number=2, type=port}
==>{desc=s1-eth1, port_id=00:00:00:00:00:00:02:011, state=active, port_state=1, number=1, type=port}
==>{desc=s4-eth1, port_id=00:00:00:00:00:00:02:041, state=active, port_state=1, number=1, type=port}
==>{desc=s5-eth2, port_id=00:00:00:00:00:00:02:052, state=active, port_state=1, number=2, type=port}
==>{desc=s2-eth2, port_id=00:00:00:00:00:00:02:022, state=active, port_state=1, number=2, type=port}
==>{desc=s6-eth1, port_id=00:00:00:00:00:00:02:061, state=active, port_state=1, number=1, type=port}
==>{desc=s2-eth2, port_id=00:00:00:00:00:00:02:022, state=active, port_state=1, number=2, type=port}
==>{desc=s4-eth2, port_id=00:00:00:00:00:00:02:042, state=active, port_state=1, number=2, type=port}
==>{desc=s2-eth1, port_id=00:00:00:00:00:00:02:021, state=active, port_state=1, number=1, type=port}

您可以看到在多次运行代码时可能会创建相同顶点的多个副本。我想避免这种情况。我希望仅在顶点尚不存在时才创建该顶点。我该如何实现这一点。

最佳答案

这个问题已在 gremlin-users 邮件列表中得到解答:

https://groups.google.com/forum/#!topic/gremlin-users/5rVEiX744-c

从线程中,人们可以:

  1. 在 gremlin 中编写自己的 getOrCreate 方法
  2. 考虑使用灯泡 native function

关于python - 灯泡:仅在尚不存在时创建顶点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24136443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com