Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pydot
- menu = {'dinner':
- {'chicken':'good',
- 'beef':'average',
- 'vegetarian':{
- 'tofu':'good',
- 'salad':{
- 'caeser':'bad',
- 'italian':'average'}
- },
- 'pork':'bad'}
- }
- def draw(parent_name, child_name):
- edge = pydot.Edge(parent_name, child_name)
- graph.add_edge(edge)
- def visit(node, parent=None):
- for k,v in node.items():
- if isinstance(v, dict):
- # We start with the root node whose parent is None
- # we don't want to graph the None node
- if parent:
- draw(parent, k)
- visit(v, k)
- else:
- draw(parent, k)
- # drawing the label using a distinct name
- draw(k, k+'_'+v)
- if __name__ == '__main__':
- graph = pydot.Dot(graph_type='graph')
- visit(menu)
- graph.write_png('example1_graph.png')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement