""" 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)