Gremlin-python:如何使用用户定义的步骤重用遍历逻辑

问题描述

我正在尝试简化以下遍历:

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...