From 209b9bae939c744017dfa0192daf855d16db3902 Mon Sep 17 00:00:00 2001 From: Vincent Le Gallic Date: Sun, 15 Feb 2015 18:43:35 +0100 Subject: [PATCH] =?utf8?q?[all=5Fpast]=20R=C3=A9cup=C3=A9rer=20les=20gains?= =?utf8?q?/d=C3=A9penses=20de=20tous=20les=20comptes=20dans=20le=20pass?= =?utf8?q?=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- bde/stats/all_past.py | 47 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 bde/stats/all_past.py diff --git a/bde/stats/all_past.py b/bde/stats/all_past.py new file mode 100755 index 0000000..75f1b1d --- /dev/null +++ b/bde/stats/all_past.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +"""Pour récupérer les soldes des comptes à plusieurs moments du passé.""" + +import depenses + +def get_idbdes(): + """Récupère les idbdes de tous les comptes.""" + con, cur = depenses.get_cursor() + cur.execute("SELECT idbde FROM comptes WHERE idbde >= 0;") + l = cur.fetchall() + l = [i[0] for i in l] + return l + +def line_csv(idbde, li): + """Renvoie la liste ``li`` comme une ligne de fichier csv""" + return "%s;%s;%s;%s;%s\n" % (idbde, int(li[0]), int(li[1]), '"%s"' % (li[2],), '"%s"' % (li[3],)) + +def to_csv(data, path): + """Écrit le csv.""" + f = open(path, "w") + f.write('''N° de compte;Année;Mois;Dépenses;Gains\n''') + idbdes = data.keys() + idbdes.sort() + for i in idbdes: + datai = data[i] + for l in datai: + f.write(line_csv(i, l)) + f.close() + +def get_all(idbdes, verbose=False): + """Récupère toutes les données.""" + data = {} + if verbose: + print "Dernier idbde : %s" % (max(idbdes),) + for i in idbdes: + data[i] = depenses.get_depenses(i, bymonth=True)[0] + if i % 100 == 0: + if verbose: + print i + return data + +if __name__ == "__main__": + idbdes = get_idbdes() + data = get_all(idbdes, verbose=True) + to_csv(data, "all_past.csv") -- 2.39.2