--- /dev/null
+#!/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)