Search code examples
pythonflaskjinja2py2neo

py2neo run().data() randomly return tuples instead of list


im using py2neo 5.0b1 in combination with flask 1.1.2 and jinja2 2.11.2 to build a small webapp.

My simple function which return the values of my cypher: Model:

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()

View:

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

My function get_threats() in the beginning returns a tuple instead of a list of dict. After a some time and several server restarts the return is a list. How can this be?

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 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'>

List:

[{'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'>

Solution

  • This cannot be. The method always returns a list, regardless of the size and shape of the result.

    The most likely expansion for what you saw is suggested by the comment above: that you had a trailing comma at some point, which got removed.