【问题标题】:py2neo run().data() randomly return tuples instead of listpy2neo run().data() 随机返回元组而不是列表
【发布时间】:2020-07-25 17:37:14
【问题描述】:

我使用 py2neo 5.0b1 与 flask 1.1.2 和 jinja2 2.11.2 结合来构建一个小型 webapp。

我的简单函数返回我的密码值: 型号:

def get_threats():
    query = '''Match (n:Threat) return n.threat_name as threat_name , n.threat_id as threat_id'''
    return graph.run(query).data()

查看:

def add_mitigation():
    if request.method=='GET':
        threats=get_threats()
        print(type(threats))
        return render_template('mitigation.html', threats=threats)
....

我的函数 get_threats() 在开头返回一个元组而不是一个字典列表。经过一段时间和几个服务器重新启动后,返回的是一个列表。这怎么可能?

元组:

([{'threat_name': 'test', 'threat_id': '93b7c91e-67de-4b52-892c-3b589c00042f'}, {'threat_name': 'test-endge', 'threat_id': '00fec93b-60b7-4ffc-b96e-ef57c0c967a1'}, {'threat_name': 'test-endge', 'threat_id': 'd6b62895-68be-4d96-94c1-d05596550465'}, {'threat_name': 'test-endge', 'threat_id': '5b66ed31-9043-4d2b-8e88-a563297f38a2'}, {'threat_name': 'test-endge', 'threat_id': 'f799dd39-abaa-4d42-850f-e5181e5abd83'}, {'threat_name': 'test-endge', 'threat_id': 'b2602c18-1dfb-4065-82dd-67653e9589b3'}, {'threat_name': 'test-endge', 'threat_id': 'b39f0322-b5a7-4d98-912e-ac1dfe3f9b2e'}, {'threat_name': 'test-endge', 'threat_id': '0d6a498d-d89f-485f-a5b2-e53aeb2cdde2'}, {'threat_name': 'test-endge', 'threat_id': 'b3bac0d6-92c9-491f-9e1e-9de7b5f554c5'}, {'threat_name': 'test-endge', 'threat_id': 'fa1c845e-53af-4c6d-b31c-9870d36d90bb'}, {'threat_name': 'test-endge', 'threat_id': '03c42053-5e6e-4c78-bf93-b7432087e89c'}, {'threat_name': 'test-endge', 'threat_id': '7822ed14-1af4-4c2f-9b74-21ec17ab78fb'}, {'threat_name': 'test-endge', 'threat_id': 'ed84f785-00e1-4e7f-804c-cf7cf7034ea9'}, {'threat_name': 'test-endge', 'threat_id': '2e61a6c0-e987-4c1d-bd02-faf34ffc38e8'}, {'threat_name': 'test-endge', 'threat_id': '70a949e4-2d74-4d98-900e-900e1d72fb6b'}, {'threat_name': 'blub', 'threat_id': '0d769c85-1908-4bd7-bbfa-b7d9320f76e2'}, {'threat_name': 'test', 'threat_id': 'bf83aefc-3b9b-412f-a85b-5845d1700da2'}, {'threat_name': 'a', 'threat_id': '0c20b0ff-69a7-4134-b865-4a5fb7813984'}],)
127.0.0.1 - - [25/Jul/2020 18:54:10] "GET /mitigation HTTP/1.1" 200 -
 * Detected change in '/home/macgio/neo4j-flask/blog/views.py', reloading
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 287-155-723
<class 'tuple'>

列表:

[{'threat_name': 'test', 'threat_id': '93b7c91e-67de-4b52-892c-3b589c00042f'}, {'threat_name': 'test-endge', 'threat_id': '00fec93b-60b7-4ffc-b96e-ef57c0c967a1'}, {'threat_name': 'test-endge', 'threat_id': 'd6b62895-68be-4d96-94c1-d05596550465'}, {'threat_name': 'test-endge', 'threat_id': '5b66ed31-9043-4d2b-8e88-a563297f38a2'}, {'threat_name': 'test-endge', 'threat_id': 'f799dd39-abaa-4d42-850f-e5181e5abd83'}, {'threat_name': 'test-endge', 'threat_id': 'b2602c18-1dfb-4065-82dd-67653e9589b3'}, {'threat_name': 'test-endge', 'threat_id': 'b39f0322-b5a7-4d98-912e-ac1dfe3f9b2e'}, {'threat_name': 'test-endge', 'threat_id': '0d6a498d-d89f-485f-a5b2-e53aeb2cdde2'}, {'threat_name': 'test-endge', 'threat_id': 'b3bac0d6-92c9-491f-9e1e-9de7b5f554c5'}, {'threat_name': 'test-endge', 'threat_id': 'fa1c845e-53af-4c6d-b31c-9870d36d90bb'}, {'threat_name': 'test-endge', 'threat_id': '03c42053-5e6e-4c78-bf93-b7432087e89c'}, {'threat_name': 'test-endge', 'threat_id': '7822ed14-1af4-4c2f-9b74-21ec17ab78fb'}, {'threat_name': 'test-endge', 'threat_id': 'ed84f785-00e1-4e7f-804c-cf7cf7034ea9'}, {'threat_name': 'test-endge', 'threat_id': '2e61a6c0-e987-4c1d-bd02-faf34ffc38e8'}, {'threat_name': 'test-endge', 'threat_id': '70a949e4-2d74-4d98-900e-900e1d72fb6b'}, {'threat_name': 'blub', 'threat_id': '0d769c85-1908-4bd7-bbfa-b7d9320f76e2'}, {'threat_name': 'test', 'threat_id': 'bf83aefc-3b9b-412f-a85b-5845d1700da2'}, {'threat_name': 'a', 'threat_id': '0c20b0ff-69a7-4134-b865-4a5fb7813984'}]
127.0.0.1 - - [25/Jul/2020 19:22:22] "GET /mitigation HTTP/1.1" 200 -
 * Detected change in '/home/macgio/neo4j-flask/blog/views.py', reloading
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 287-155-723
<class 'list'>

【问题讨论】:

  • 也许你在return graph.run(query).data() 后面漏掉了一个逗号,后来你更正了。您是否能够再次重现此错误?
  • 嗨,是的,如果我在函数后面加上逗号,我可以重现错误。谢谢

标签: python flask jinja2 py2neo


【解决方案1】:

这不可能。该方法总是返回一个列表,无论结果的大小和形状如何。

上面的评论建议你看到的最有可能的扩展:你在某个时候有一个尾随逗号,它被删除了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-08
    • 2016-03-22
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多