Cypher 文件作为 py2neo python 中的参数

我正在尝试在py2neo中将文件作为参数传递,以使查询中的变量作为其他参数。而不是:cypher


from py2neo import Graph


graph = Graph(password = "*****")


def test(some_things):

    result = graph.run(

                "MATCH (movie:movies)"

                "where movie.name =~ $name "

                "RETURN movie",{"name":"(?i).*" + some_things+ ".*"})

    return result

我想知道是否有这样的东西:


from py2neo import Graph


graph = Graph(password = "*****")


def test(some_things):

    result = graph.run("some_cypher.cypher", some_things)

    return result

其中可能是:some_cypher.cypher


MATCH (movie:movies) where movie.name =~ $name RETURN movie, ,{"name":"(?i).*" + ?+ ".*"}

在 python 文件中是要替换为 的参数。?some_things


慕勒3428872
浏览 80回答 2
2回答

狐的传说

虽然在py2neo中没有直接从文件中读取的内置选项,但有一种机制可以根据需要获取参数序列。因此,剩下的只是使用一个函数从文件中读取查询并使用参数。这应该看起来像这样:from py2neo import Graphgraph = Graph(password = "*****")def run_query_from_file(cypher_file_path,  parameters=None, **kwparameters):    with open(cypher_file_path, 'r') as cypher_file:          cypher_query = cypher_file.read().strip()    graph.run(cypher_query, parameters)def test1(dict_of_parameters):    result = run_query_from_file("some_cypher.cypher", dict_of_parameters)    return resultdef test2(**kwparameters):    result = run_query_from_file("some_cypher.cypher", **kwparameters)    return result# Both should worktest1({'username': 'abc', 'password': '123'})test2('username'='abc', 'password'='123')其中包含:some_cypher.cypherMERGE (user:User {username:$username}) with user, user.password as user_password SET user.password = $password RETURN user_password

喵喔喔

py2neo中没有内置这样的功能。如果你想这样做,你必须滚动自己的函数。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python