Handling a RecordListList from a py2neo transaction process() call

101 views Asked by At

I am processing Cypher statements using

result = my_cypher.my_transaction.process()

My issue is that I don't know how to elegantly get the result. I have stumbled onto the fact that process() returns a RecordListList but I don't know what to do with it except iterate over it. This annoys me since in this part of my code, I am processing a single statement at a time and don't really need to iterate over anything. (I am still in a single transaction, however.)

Here's why I am doing now and it is burning my soul:

result = cypher.tx.process()
for r in result:
    for x in r:
        node_id = x['node_id']

but r and x will only ever have one row each. If I could just get the result directly I'd be happier:

node_id = result.one().one()['node_id']

EDIT 1

I used ipython to display the list of methods available on process(). One of these was pop(). Now I have this abomination:

 result = cypher.process()
 one_row = result.pop()
 tmp = one_row[0]['node_id']

Better, but still fugly.

EDIT 2

Apparently there's a piece of magic in RecordList that pulls the first row from said recordset. It's an attribute called one. Very odd that it is not a method.

 result = cypher.process()
 one_row = result.pop()
 node_id = one_row.one['node_id']
1

There are 1 answers

4
Martin Preusse On

Why not:

try:
    node_id = result[0][0]['node_id']
except IndexError:
    # result is empty
    pass