问题描述
我正在尝试简化以下遍历:
operation_dict['output_schema'] = g.V(operation_id).outE('uses').inV()\
.project('id','label','attribute_id','attribute_name','dataType')\
.by(T.id).by(T.label).by('attribute_id').by('attribute_name').by('dataType').toList()
由于我想重用投影遍历,所以我想从遍历中提取它,如以下代码片段所示:
def extract_attribute(x):
return g.V(x).project('id','dataType')\
.by(T.id).by(T.label).by('attribute_id').by('attribute_name').by('dataType')
operation_dict['input_schema'] = g.V(operation_id).inE('follows').outV().outE('uses').inV()\
.map(lambda x: extract_attribute(x)).toList()
如何在Gremlin for Python中做到这一点?我尝试了Lambda功能,但到目前为止没有成功。
解决方法
您可以通过几种方法来执行此操作,但这与您尝试执行的操作一致:
>>> def c(x):
... return __.project('x').by(x)
...
>>> g.V().map(c('name')).toList()
[{'x': 'marko'},{'x': 'vadas'},{'x': 'lop'},{'x': 'josh'},{'x': 'ripple'},{'x': 'peter'}]
您只需要在extract_attribute()
函数中生成一个anonymous child traversal。重用遍历逻辑的另一种更高级的方法是构建custom Gremlin DSL。