From dd4a5a0ac424467f9ba2d74bef5e485fffa1079b Mon Sep 17 00:00:00 2001 From: Vincent Le Gallic Date: Sun, 28 Sep 2014 14:21:43 +0200 Subject: [PATCH] =?utf8?q?[bde/combiennegs]=20Pour=20savoir=20combien=20de?= =?utf8?q?=20fois=20les=20gens=20sont=20pass=C3=A9s=20en=20n=C3=A9gatif?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- bde/combiennegs.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100755 bde/combiennegs.py diff --git a/bde/combiennegs.py b/bde/combiennegs.py new file mode 100755 index 0000000..c6f43fb --- /dev/null +++ b/bde/combiennegs.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +""" Pour savoir combien de fois les gens sont passés en négatif """ + +import psyopg2 +import psyopg2.extras + +year = 2014 + +def get_data(con, year): + cur = con.getcursor(cursor_factory = psycopg2.DictCursor) + cur.execute("SELECT * FROM transactions ORDER BY date ASC;") + transactions = cur.fetchall() + cur.execute("SELECT adherent FROM inscriptions WHERE annee = %s;", (year,)) + numcbdes = cur.fetchall() + return (numcbdes, transactions) + +def combienneg(numcbdes, alltrans): + soldes = {n : 0 for n in numcbdes} + nbnegs = {n : 0 for n in numcbdes} + for trans in [t for t in alltrans if t["valide"]]: + for (num, signe) in [(t["emetteur"], -1), (t["destinataire"], 1)]: + newsolde = soldes[num] + t["montant"] * t["quantite"] * signe + if soldes[num] >= 0 and newsolde < 0: + nbnegs[num] += 1 + soldes[num] += newsolde + return nbnegs + +if __name__ == "__main__": + con = psycopg2.connect("bde") + numcbdes, transactions = get_data(con, year) + nbnegs = combienneg(numcbdes, transactions) -- 2.39.2