]> gitweb.pimeys.fr Git - scripts-20-100.git/blob - bde/combiennegs.py
[solde_date] proprification, importable
[scripts-20-100.git] / bde / combiennegs.py
1 #!/usr/bin/env python
2 # -*- encoding: utf-8 -*-
3
4 """ Pour savoir combien de fois les gens sont passés en négatif """
5
6 import psycopg2
7 import psycopg2.extras
8
9 def get_data(con):
10 cur = con.cursor(cursor_factory = psycopg2.extras.DictCursor)
11 cur.execute("SELECT * FROM transactions ORDER BY date ASC;")
12 transactions = cur.fetchall()
13 return transactions
14
15 def combienneg(alltrans):
16 nums = list(set([t["emetteur"] for t in alltrans] + [t["destinataire"] for t in alltrans]))
17 soldes = dict([(n, 0) for n in nums])
18 negs = dict([(n, []) for n in nums])
19 for trans in [t for t in alltrans if t["valide"]]:
20 for (num, signe) in [(trans["emetteur"], -1), (trans["destinataire"], 1)]:
21 newsolde = soldes[num] + trans["montant"] * trans["quantite"] * signe
22 if soldes[num] >= 0 and newsolde < 0:
23 negs[num].append(t["date"])
24 soldes[num] = newsolde
25 return (negs, soldes)
26
27 if __name__ == "__main__":
28 con = psycopg2.connect(database="bde")
29 transactions = get_data(con)
30 (negs, soldes) = combienneg(transactions)