Ajouter GrapheDictionnaire.py
This commit is contained in:
commit
7d53b4d9bb
1 changed files with 29 additions and 0 deletions
29
GrapheDictionnaire.py
Normal file
29
GrapheDictionnaire.py
Normal 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)
|
Loading…
Reference in a new issue