From 2c12ef8241190048fa21543666021af9b11e3d37 Mon Sep 17 00:00:00 2001 From: Vincent Le Gallic Date: Sun, 28 Sep 2014 15:15:43 +0200 Subject: [PATCH] =?utf8?q?[combiennegs]=20Des=20trucs=20cod=C3=A9s=20avec?= =?utf8?q?=20les=20pieds=20+=20compatibilit=C3=A9=20python2.=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Parce que les dico en compréhension on les a pas sur le serveur note --- bde/combiennegs.py | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/bde/combiennegs.py b/bde/combiennegs.py index c6f43fb..1e62cd1 100755 --- a/bde/combiennegs.py +++ b/bde/combiennegs.py @@ -3,31 +3,28 @@ """ Pour savoir combien de fois les gens sont passés en négatif """ -import psyopg2 -import psyopg2.extras +import psycopg2 +import psycopg2.extras -year = 2014 - -def get_data(con, year): - cur = con.getcursor(cursor_factory = psycopg2.DictCursor) +def get_data(con): + cur = con.cursor(cursor_factory = psycopg2.extras.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) + return transactions -def combienneg(numcbdes, alltrans): - soldes = {n : 0 for n in numcbdes} - nbnegs = {n : 0 for n in numcbdes} +def combienneg(alltrans): + nums = list(set([t["emetteur"] for t in alltrans] + [t["destinataire"] for t in alltrans])) + soldes = dict([(n, 0) for n in nums]) + negs = dict([(n, []) for n in nums]) 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 + for (num, signe) in [(trans["emetteur"], -1), (trans["destinataire"], 1)]: + newsolde = soldes[num] + trans["montant"] * trans["quantite"] * signe if soldes[num] >= 0 and newsolde < 0: - nbnegs[num] += 1 - soldes[num] += newsolde - return nbnegs + negs[num].append(t["date"]) + soldes[num] = newsolde + return (negs, soldes) if __name__ == "__main__": - con = psycopg2.connect("bde") - numcbdes, transactions = get_data(con, year) - nbnegs = combienneg(numcbdes, transactions) + con = psycopg2.connect(database="bde") + transactions = get_data(con) + (negs, soldes) = combienneg(transactions) -- 2.39.2