From 7d53b4d9bb427c337ca257bdd5001aa81cb4a749 Mon Sep 17 00:00:00 2001 From: FRancois Pelletier Date: Sat, 3 Feb 2024 21:57:03 +0000 Subject: [PATCH] Ajouter GrapheDictionnaire.py --- GrapheDictionnaire.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 GrapheDictionnaire.py diff --git a/GrapheDictionnaire.py b/GrapheDictionnaire.py new file mode 100644 index 0000000..65dfd65 --- /dev/null +++ b/GrapheDictionnaire.py @@ -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)