首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >灯泡: g.vertices.index.lookup(dpi=dpid_str)不能按预期工作

灯泡: g.vertices.index.lookup(dpi=dpid_str)不能按预期工作
EN

Stack Overflow用户
提问于 2014-06-16 08:22:45
回答 1查看 95关注 0票数 1

我正在使用TitanGraphDB + Cassandra.I,我将按照以下方式启动土卫六

代码语言:javascript
运行
复制
cd titan-cassandra-0.3.1
bin/titan.sh config/titan-server-rexster.xml config/titan-server-cassandra.properties

我有一个Rexster外壳,我可以使用它与上面的Titan+Cassandra通信。

代码语言:javascript
运行
复制
cd rexster-console-2.3.0
bin/rexster-console.sh

我想从我的python程序中编写土卫六图形数据库,我正在使用灯泡包。

我从python中创建了3种类型的顶点,如下所示。三种类型的顶点是

代码语言:javascript
运行
复制
- switch
- port
- device

from bulbs.titan import Graph
 vswitch = self.g.vertices.get_or_create('dpid',dpid_str,{'state':'active','dpid':dpid_str,'type':'switch'})
 vport   = self.g.vertices.get_or_create('port_id',port_id,{'desc':desc,'port_id':port_id,'state':state,'port_state':port_state,'number':number,'type':'port'})

如果我试图打印出变量vswitch、vport和vdevice,就会得到以下结果。

代码语言:javascript
运行
复制
vswitch     <Vertex: http://localhost:8182/graphs/graph/vertices/4>
vport       <Vertex: http://localhost:8182/graphs/graph/vertices/28>

但是,如果我尝试使用下面的键检索上面的顶点。

代码语言:javascript
运行
复制
vswitch = self.g.vertices.index.lookup(dpid=dpid_str)
vport   = self.g.vertices.index.lookup(port_id=port_id_str)

并尝试打印出vswitch和vport变量,我得到以下值

代码语言:javascript
运行
复制
<generator object <genexpr> at 0x26d6370>)
<generator object <genexpr> at 0x26d63c0>

我是否在尝试使用g.vertices.index.lookup(dpid=dpid_str)检索上面的顶点时做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-17 19:27:54

g.vertices.index.lookup()方法返回一个Python生成器 (这是迭代器的一种类型)。

使用next()获取生成器中的下一个值:

代码语言:javascript
运行
复制
>>> # lookup() returns an generator (can return more than 1 value)

>>> switches = self.g.vertices.index.lookup(dpid=dpid_str)
>>> switch = switches.next()

>>> ports   = self.g.vertices.index.lookup(port_id=port_id_str)
>>> port = ports.next()

也可以使用list()generator转换为Python list

代码语言:javascript
运行
复制
>>> switches = self.g.vertices.index.lookup(dpid=dpid_str)
>>> list(switches)

>>> ports   = self.g.vertices.index.lookup(port_id=port_id_str)
>>> list(ports)

但是,如果索引项是唯一的,则可以使用get_unique()方法返回一个值或None

代码语言:javascript
运行
复制
# returns 1 vertex or None (errors if more than 1)
>>> vertex = g.vertices.index.get_unique( "dpid", dpid_str) 

你看..。

Rexter索引文档:

  • http://bulbflow.com/docs/api/bulbs/rexster/indices/

index.lookup() https://github.com/espeed/bulbs/blob/afa28ccbacd2fb92e0039800090b8aa8bf2c6813/bulbs/titan/index.py#L251

index.get_unique() https://github.com/espeed/bulbs/blob/afa28ccbacd2fb92e0039800090b8aa8bf2c6813/bulbs/titan/index.py#L274

注意到:迭代器和生成器是programming的基础--它们在任何地方都使用,并不是专门针对灯泡的--如果您是Python编程新手,请参阅我对我如何学会用Python编程?的回答,以获得一个用于学习使用我如何学会用Python编程?编程的好的在线资源列表。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24239327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档