snippets/GrapheDictionnaire.py

29 lines
980 B
Python

# GrapheDictionnaire
# Développé par François Pelletier
# MIT License
import pydot
class GrapheDictionnaire(object):
def __init__(self, mon_dict, **kwargs):
self.graph = pydot.Dot(graph_type='graph', **kwargs)
self.mon_dict = mon_dict
def dessiner_arete(self, noeud_parent, noeud_enfant):
edge = pydot.Edge(noeud_parent, noeud_enfant)
self.graph.add_edge(edge)
def visiter_noeud(self, mon_dict, noeud_parent="head", feuilles=False):
for cle, valeur in mon_dict.items():
if isinstance(valeur, dict):
if noeud_parent:
self.dessiner_arete(noeud_parent, cle)
self.visiter_noeud(valeur, cle)
else:
self.dessiner_arete(noeud_parent, cle)
if feuilles:
self.dessiner_arete(cle, str(cle)+'_'+str(valeur))
def generer_graph(self, feuilles=False):
self.visiter_noeud(self.mon_dict)