Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import ast
tree = ast.parse("""
1
""")
import redis
from redisgraph import Node, Edge, Graph, Path
instance = redis.Redis(host='localhost', port=9000)
redis_graph = Graph('ast', instance)
name_pool, node_pool = [], []
def insert_ast(node):
name = type(node).__name__
graph_node = Node(alias=f"{name}_{name_pool.count(name)}", label=name, properties={})
name_pool.append(name)
node_pool.append(graph_node)
redis_graph.add_node(graph_node)
def add_edge(field, value):
child_node = insert_ast(value)
graph_edge = Edge(graph_node, field, child_node)
redis_graph.add_edge(graph_edge)
for field, value in ast.iter_fields(node):
if isinstance(value, ast.AST):
add_edge(field, value)
elif isinstance(value, list):
for item in value:
add_edge(field, item)
else:
graph_node.properties[field] = repr(value)
return graph_node
insert_ast(tree)
redis_graph.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment