繁体   English   中英

Cypher文件作为py2neo python中的参数

[英]Cypher file as parameter in py2neo python

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

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替换的参数。

虽然在 py2neo 中没有直接从文件读取的内置选项,但有一种机制可以根据需要获取参数序列。 所以,剩下的就是使用一个函数从文件中读取查询并使用参数。 这应该看起来像:

from py2neo import Graph

graph = 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 result

def test2(**kwparameters):
    result = run_query_from_file("some_cypher.cypher", **kwparameters)
    return result

# Both should work
test1({'username': 'abc', 'password': '123'})
test2('username'='abc', 'password'='123')

其中some_cypher.cypher包含:

MERGE (user:User {username:$username}) WITH user, user.password as user_password SET user.password = $password RETURN user_password

py2neo 中没有内置这样的功能。 如果你想这样做,你必须推出自己的功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM