From b0c9b76ed9703018c2abebcd9577db650fb01911 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ga=C3=A9tan=20Lerisson?= Date: Tue, 14 Aug 2012 03:09:28 +0200 Subject: [PATCH] =?utf8?q?ce=20qui=20permet=20de=20faire=20des=20graphes?= =?utf8?q?=20(noms=20seuls=20et=20liens,=20pas=20d'infos=20suppl=C3=A9ment?= =?utf8?q?aires).?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + graphe.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100755 graphe.py 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') -- 2.39.2