0

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

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 功能,但到目前为止没有成功。

4

1 回答 1

1

有几种方法可以做到这一点,但这里有一种与您尝试做的方式一致的方法:

>>> 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()重用遍历逻辑的另一种更高级的方法是构建自定义 Gremlin DSL

于 2020-11-06T10:46:29.440 回答