From: Gaétan Lerisson Date: Tue, 14 Aug 2012 01:09:28 +0000 (+0200) Subject: ce qui permet de faire des graphes (noms seuls et liens, pas d'infos supplémentaires). X-Git-Url: http://gitweb.pimeys.fr/?a=commitdiff_plain;h=b0c9b76ed9703018c2abebcd9577db650fb01911;p=chopes.git ce qui permet de faire des graphes (noms seuls et liens, pas d'infos supplémentaires). --- diff --git a/.gitignore b/.gitignore index a57fd2e..62633f6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Custom # ################### *~ +*.png # Compiled source # ################### diff --git a/graphe.py b/graphe.py new file mode 100755 index 0000000..499abb2 --- /dev/null +++ b/graphe.py @@ -0,0 +1,51 @@ +#!/usr/bin/python +# -*- encoding: utf-8 -*- + + +# Import graphviz +import gv + +import psycopg2 +import psycopg2.extras + +def getcursor(): + con = psycopg2.connect(database = "chopes") + cur = con.cursor(cursor_factory=psycopg2.extras.DictCursor) + return con, cur + +# Import pygraph +from pygraph.classes.graph import graph +from pygraph.classes.digraph import digraph +from pygraph.algorithms.searching import breadth_first_search +from pygraph.readwrite.dot import write + + +def getgraph(): + con, cur = getcursor() + cur.execute("SELECT * FROM gens;") + l = cur.fetchall() + gens = {n: nom.decode("utf8") for n,nom in l} + cur.execute("SELECT id1, id2 FROM chopes;") + chopes = cur.fetchall() + return gens, chopes + + +if __name__ == "__main__": + gens, chopes = getgraph() + #gr = graph() + textgraph = u"graph chope {\n" + for noeud in gens.values(): + textgraph += u'"%s";\n' % noeud + #gr.add_nodes(gens.values()) + + for chopeur, victime in chopes: + textgraph += u'"%s" -- "%s";\n' % (gens[chopeur], gens[victime]) + #gr.add_edge((gens[chopeur], gens[victime])) + textgraph += "}\n" + print textgraph + # Draw as PNG + #dot = write(gr) + dot = textgraph.encode("utf8") + gvv = gv.readstring(dot) + gv.layout(gvv, 'dot') + gv.render(gvv, 'png', 'chopes.png')