Ajouter GrapheDictionnaire.py

This commit is contained in:
Francois Pelletier 2024-02-03 21:57:03 +00:00
commit 7d53b4d9bb

29
GrapheDictionnaire.py Normal file
View file

@ -0,0 +1,29 @@
# 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)