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

Gremlin python:如何使用用户定义的步骤重用遍历逻辑,gremlin,tinkerpop,gremlinpython,Gremlin,Tinkerpop,Gremlinpython,我试图简化以下遍历: 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() 因为我想重用投影遍历,

我试图简化以下遍历:

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','label','attribute_id', 'attribute_name', '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\u attribute()
函数中生成一个。重用遍历逻辑的另一个更高级的方法是构建一个