Skip to content

Instantly share code, notes, and snippets.

@isidentical
Created December 25, 2020 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isidentical/56e05e0ac9a1796fa74743638f9e12b2 to your computer and use it in GitHub Desktop.
Save isidentical/56e05e0ac9a1796fa74743638f9e12b2 to your computer and use it in GitHub Desktop.
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