Advertisement
Guest User

Github Issue pydot

a guest
Mar 2nd, 2021
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. import pydot
  2.  
  3. menu = {'dinner':
  4.             {'chicken':'good',
  5.              'beef':'average',
  6.              'vegetarian':{
  7.                    'tofu':'good',
  8.                    'salad':{
  9.                             'caeser':'bad',
  10.                             'italian':'average'}
  11.                    },
  12.              'pork':'bad'}
  13.         }
  14.  
  15. def draw(parent_name, child_name):
  16.     edge = pydot.Edge(parent_name, child_name)
  17.     graph.add_edge(edge)
  18.  
  19. def visit(node, parent=None):
  20.     for k,v in node.items():
  21.         if isinstance(v, dict):
  22.             # We start with the root node whose parent is None
  23.             # we don't want to graph the None node
  24.             if parent:
  25.                 draw(parent, k)
  26.             visit(v, k)
  27.         else:
  28.             draw(parent, k)
  29.             # drawing the label using a distinct name
  30.             draw(k, k+'_'+v)
  31. if __name__ == '__main__':
  32.     graph = pydot.Dot(graph_type='graph')
  33.     visit(menu)
  34.     graph.write_png('example1_graph.png')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement