apache-age-python icon indicating copy to clipboard operation
apache-age-python copied to clipboard

Excution ERR['int' object does not support indexing]

Open afidegnum opened this issue 4 years ago • 5 comments

I'm trying to run a recursive query that will enable me to process the nodes which can be used for additional operations, and came across the error above: when I tested individual queries, they works perfectly

def graph_nodes(tag_id):
    top = ag.execCypher("MATCH (n:node) WHERE id(n) = %s RETURN n", params=(tag_id,))
    x = [r[0] for r in top]
    print(x[0]["tag"])
    children = ag.execCypher("MATCH (v:node)-[R:connect]-(V2) WHERE id(v) = %s RETURN V2", params=(x[0]).id,)
    for c in children:
        print(c)
        graph_nodes(c[0].id)

cursor = ag.execCypher("MATCH (n:node {tag: 'html'}) RETURN n")    
t = [x[0].id for x in cursor]
print(t[0])
graph_nodes(t[0])
TypeError                                 Traceback (most recent call last)
~/.virtualenvs/orgs/lib/python3.9/site-packages/age/age.py in execCypher(conn, graphName, cypherStmt, cols, params)
    106     try:
--> 107         cursor.execute(stmt, params)
    108         return cursor

TypeError: 'int' object does not support indexing

During handling of the above exception, another exception occurred:

SqlExcutionError                          Traceback (most recent call last)
/tmp/ipykernel_117955/2082054997.py in <module>
     11 t = [x[0].id for x in cursor]
     12 print(t[0])
---> 13 graph_nodes(t[0])

/tmp/ipykernel_117955/2082054997.py in graph_nodes(tags)
      3     x = [r[0] for r in top]
      4     print(x[0]["tag"])
----> 5     children = ag.execCypher("MATCH (v:node)-[R:connect]-(V2) WHERE id(v) = %s RETURN V2", params=(x[0].id,)
      6     for c in children:
      7         print(c)

~/.virtualenvs/orgs/lib/python3.9/site-packages/age/age.py in execCypher(self, cypherStmt, cols, params)
    156 
    157     def execCypher(self, cypherStmt:str, cols:list=None, params:tuple=None) -> ext.cursor :
--> 158         return execCypher(self.connection, self.graphName, cypherStmt, cols=cols, params=params)
    159 
    160     def cypher(self, cursor:ext.cursor, cypherStmt:str, cols:list=None, params:tuple=None) -> ext.cursor :

~/.virtualenvs/orgs/lib/python3.9/site-packages/age/age.py in execCypher(conn, graphName, cypherStmt, cols, params)
    112     except Exception as cause:
    113         conn.rollback()
--> 114         raise SqlExcutionError("Excution ERR[" + str(cause) +"](" + stmt +")", cause)
    115 
    116 

SqlExcutionError: ("Excution ERR['int' object does not support indexing](SELECT * from cypher('text_test', $$ MATCH (v:node)-[R:connect]-(V2) WHERE id(v) = %s RETURN V2 $$) as (v agtype);)", TypeError("'int' object does not support indexing"))

afidegnum avatar Dec 02 '21 17:12 afidegnum

Tracing log says "TypeError: 'int' object does not support indexing" And, the code line where error occurs is

children = ag.execCypher("MATCH (v:node)-[R:connect]-(V2) WHERE id(v) = %s RETURN V2", params=(x[0].id,)

So, I think variable 'x' is int value, and 'x[0]' is not supported. Please check the variable 'x'.

rhizome-ai avatar Dec 03 '21 00:12 rhizome-ai

@rhizome-ai I posted a question about traversing recursively a graph, can you please have a look? https://stackoverflow.com/q/70213240/5713751

afidegnum avatar Dec 03 '21 11:12 afidegnum

I think you should call graph_dom(did) recursively in the children loop .

cursor = ag.execCypher("MATCH (n:node {tag: 'html'}) RETURN n")    
t = [x[0].id for x in cursor]
print(t[0])

def graph_dom(t_id):
    parent = ag.execCypher("MATCH (n:node) WHERE id(n) = %s RETURN n", params=(t_id,))
    p = [x[0] for x in parent]
    pt = p[0]["tag"]       
    pid = p[0].id
    print(f"|> parent Id: {pid} --- parent tag {pt}")
    parent_tag = soup.new_tag(name=p[0]["tag"])
    soup.append(parent_tag)
    children = ag.execCypher("MATCH (v:node)-[R:connect]->(V2) WHERE id(v) = %s RETURN V2", params=(p[0].id,))
    for d in children:
        children_tag = soup.new_tag(name=d[0]["tag"])
        parent_tag.append(children_tag)
        dt = d[0]["tag"]
        did = d[0].id
        print(f"child id {did} child tag: {dt}")        
        graph_dom(did)   # Call graph_dom recursively.   
        
graph_dom(t[0])

file_soup = soup.prettify()
with open("helloworld.html", "w") as file:
    file.write(str(file_soup))

rhizome-ai avatar Dec 07 '21 07:12 rhizome-ai

I tried that approach and it didn't work. here is my notebook

afidegnum avatar Dec 07 '21 07:12 afidegnum

Fix Agtype parser #13

rhizome-ai avatar Dec 09 '21 08:12 rhizome-ai