Advertisement
Guest User

build.py

a guest
Dec 14th, 2022
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. import redis
  2. from redis.commands.graph import Graph, Edge, Node
  3. import random
  4. import string
  5.  
  6. r = redis.Redis(unix_socket_path='/test_socket.sock')
  7. g: Graph = r.graph("tg")
  8.  
  9. try:
  10. g.delete()
  11. except:
  12. pass
  13.  
  14. N1_root = Node(label="N1", properties={"path": "/"})
  15. N1_A = Node(label="N1", properties={"path": "/A"})
  16. N1_B = Node(label="N1", properties={"path": "/A/B"})
  17. N1_C = Node(label="N1", properties={"path": "/A/C"})
  18. N1_X = Node(label="N1", properties={"path": "/X"})
  19.  
  20. g.add_node(N1_root)
  21. g.add_node(N1_A)
  22. g.add_node(N1_B)
  23. g.add_node(N1_C)
  24. g.add_node(N1_X)
  25.  
  26. g.add_edge(Edge(src_node=N1_root, relation="N1PARENT", dest_node=N1_A))
  27. g.add_edge(Edge(src_node=N1_A, relation="N1PARENT", dest_node=N1_B))
  28. g.add_edge(Edge(src_node=N1_A, relation="N1PARENT", dest_node=N1_C))
  29. g.add_edge(Edge(src_node=N1_root, relation="N1PARENT", dest_node=N1_X))
  30.  
  31. def gen():
  32. return ''.join(random.choices(string.ascii_uppercase, k=5))
  33.  
  34. nodes = [N1_X]
  35. for i in range(100):
  36. parent = random.choice(nodes)
  37.  
  38. child_path = parent.properties['path'] + '/' + gen()
  39. child = Node(label="N1", properties={"path": child_path})
  40.  
  41. g.add_node(child)
  42. g.add_edge(Edge(src_node=parent, relation="N1PARENT", dest_node=child))
  43. nodes.append(child)
  44.  
  45. N2_X = Node(label="N2", properties={"uid": "XXXXX-XXX-XXXX"})
  46. N3_test = Node(label="N3", properties={"name": "test"})
  47.  
  48. g.add_node(N2_X)
  49. g.add_node(N3_test)
  50.  
  51. g.add_edge(Edge(src_node=N2_X, relation="N2_TO_N1", dest_node=N1_root))
  52. g.add_edge(Edge(src_node=N3_test, relation="N3_TO_N2", dest_node=N2_X))
  53.  
  54. g.commit()
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement