如何使用Python为neo4j定义遍历?

如何使用Python为neo4j定义遍历?,python,neo4j,neo4j.py,Python,Neo4j,Neo4j.py,我需要使用Python27遍历一个neo4j图 从手册中我看到我可以定义一个遍历,包括一个求值器,我得到了类似的东西 def my_evaluator(path): if (path.end['type'] == 'STOP') or (path.end['type'] == 'BUS_STOP'): return Evaluation.EXCLUDE_AND_CONTINUE else: return Evaluation.INCLUDE_AND

我需要使用Python27遍历一个neo4j图

从手册中我看到我可以定义一个遍历,包括一个求值器,我得到了类似的东西

def my_evaluator(path):
    if (path.end['type'] == 'STOP') or (path.end['type'] == 'BUS_STOP'):
        return Evaluation.EXCLUDE_AND_CONTINUE
    else:
        return Evaluation.INCLUDE_AND_CONTINUE

def get_passengers_with_bus_stop(db, start_node):
    nodes = []

    traverser = db.traversal()\
        .relationships('STOPS_AT', INCOMING)\
        .relationships('HAS_BEEN', OUTGOING)\
        .evaluator(my_evaluator)\
        .traverse(start_node)

    # Name
    for n in traverser.nodes.iterator():
        nodes.append(n['name'])

    return nodes
但是从返回的节点列表中,我想排除第一个节点,有时会在某个深度处停止

如何使用定义de neo4j手册的遍历来实现这一点

有时,我会发现一个演示文稿解释了定义遍历的另一种方法,但它运行不正常,我从Python收到并出错。我需要安装其他东西吗

class Users(neo4j.Traversal): # Traversals ! queries in Neo4j
    types = [ neo4j.INCOMING.WORKS_AS,neo4j.OUTGOING.BELONGS_TO]
    order = neo4j.BREADTH_FIRST
    stop = neo4j.STOP_AT_END_OF_GRAPH
    returnable = neo4j.RETURN_ALL_BUT_START_NODE
如何使用Python为neo4j定义遍历以:

  • 排除第一个节点
  • 在某个深处停下来
也许我应该使用子行程?我如何定义它们

更具体地说,我想在这个图上定义一个遍历,从节点“我在这里”到节点“-1”或节点“+1”。该图定义了关系的de方向及其名称


您可以链接多个评估者来实现这一点。要跳过“开始”节点,请添加类似以下内容的计算器:

def exclude_start_node(path):
    if len(path) == 0:
        return Evaluation.EXCLUDE_AND_CONTINUE

    return Evaluation.INCLUDE_AND_CONTINUE
def stop_at_depth(depth):
    def evaluator(path):
        if len(path) >= depth:
            return Evaluation.INCLUDE_AND_PRUNE

        return Evaluation.INCLUDE_AND_CONTINUE
    return evaluator
t = db.traverse().evaluator(stop_at_depth(5)).evaluator(exclude_start_node)
在某个给定的深度停下来,比如:

def exclude_start_node(path):
    if len(path) == 0:
        return Evaluation.EXCLUDE_AND_CONTINUE

    return Evaluation.INCLUDE_AND_CONTINUE
def stop_at_depth(depth):
    def evaluator(path):
        if len(path) >= depth:
            return Evaluation.INCLUDE_AND_PRUNE

        return Evaluation.INCLUDE_AND_CONTINUE
    return evaluator
t = db.traverse().evaluator(stop_at_depth(5)).evaluator(exclude_start_node)
您可以这样使用它:

def exclude_start_node(path):
    if len(path) == 0:
        return Evaluation.EXCLUDE_AND_CONTINUE

    return Evaluation.INCLUDE_AND_CONTINUE
def stop_at_depth(depth):
    def evaluator(path):
        if len(path) >= depth:
            return Evaluation.INCLUDE_AND_PRUNE

        return Evaluation.INCLUDE_AND_CONTINUE
    return evaluator
t = db.traverse().evaluator(stop_at_depth(5)).evaluator(exclude_start_node)
注意这两个遍历器如何在它们不“应用”的地方使用INCLUDE_和_CONTINUE。当链接评估者时,最严格的评估者将获胜。因此,如果任何计算器说“排除”,当前节点将被排除,如果任何计算器说“修剪”,遍历将停止当前路径

另外,截至上周,最新版本的neo4j嵌入式绑定允许您执行密码查询。您可能希望对此进行研究,因为这可能是编写您正在查看的查询的更简单的方法


您可以链接多个评估者来实现这一点。要跳过“开始”节点,请添加类似以下内容的计算器:

def exclude_start_node(path):
    if len(path) == 0:
        return Evaluation.EXCLUDE_AND_CONTINUE

    return Evaluation.INCLUDE_AND_CONTINUE
def stop_at_depth(depth):
    def evaluator(path):
        if len(path) >= depth:
            return Evaluation.INCLUDE_AND_PRUNE

        return Evaluation.INCLUDE_AND_CONTINUE
    return evaluator
t = db.traverse().evaluator(stop_at_depth(5)).evaluator(exclude_start_node)
在某个给定的深度停下来,比如:

def exclude_start_node(path):
    if len(path) == 0:
        return Evaluation.EXCLUDE_AND_CONTINUE

    return Evaluation.INCLUDE_AND_CONTINUE
def stop_at_depth(depth):
    def evaluator(path):
        if len(path) >= depth:
            return Evaluation.INCLUDE_AND_PRUNE

        return Evaluation.INCLUDE_AND_CONTINUE
    return evaluator
t = db.traverse().evaluator(stop_at_depth(5)).evaluator(exclude_start_node)
您可以这样使用它:

def exclude_start_node(path):
    if len(path) == 0:
        return Evaluation.EXCLUDE_AND_CONTINUE

    return Evaluation.INCLUDE_AND_CONTINUE
def stop_at_depth(depth):
    def evaluator(path):
        if len(path) >= depth:
            return Evaluation.INCLUDE_AND_PRUNE

        return Evaluation.INCLUDE_AND_CONTINUE
    return evaluator
t = db.traverse().evaluator(stop_at_depth(5)).evaluator(exclude_start_node)
注意这两个遍历器如何在它们不“应用”的地方使用INCLUDE_和_CONTINUE。当链接评估者时,最严格的评估者将获胜。因此,如果任何计算器说“排除”,当前节点将被排除,如果任何计算器说“修剪”,遍历将停止当前路径

另外,截至上周,最新版本的neo4j嵌入式绑定允许您执行密码查询。您可能希望对此进行研究,因为这可能是编写您正在查看的查询的更简单的方法