Feat: cleaning
This commit is contained in:
parent
a4feb1c310
commit
5c3a7f9b4f
33
pralo.py
33
pralo.py
@ -17,16 +17,15 @@ import os
|
|||||||
import optparse
|
import optparse
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
def extrait_from_file(file_name):
|
def account_from_file(file_name):
|
||||||
"""
|
"""
|
||||||
Extrait les informations à partir d'un fichier
|
Extract account from csv file
|
||||||
|
|
||||||
:param file_name: nom du fichier avec les comptes
|
:param file_name: csv file name
|
||||||
:return: liste [[nom,montant,parts],...]
|
:return: list of dictionnaries [{nom:... ,montant:... ,parts:... },...]
|
||||||
"""
|
"""
|
||||||
file = open(file_name, "r")
|
with open(file_name, "r") as f:
|
||||||
try:
|
reader = csv.reader(f, delimiter = ",")
|
||||||
reader = csv.reader(file, delimiter = ",")
|
|
||||||
r_comptes = list(reader)
|
r_comptes = list(reader)
|
||||||
header = r_comptes.pop(0) # On récupère le premier élément (les headers)
|
header = r_comptes.pop(0) # On récupère le premier élément (les headers)
|
||||||
|
|
||||||
@ -36,7 +35,7 @@ def extrait_from_file(file_name):
|
|||||||
compte = {}
|
compte = {}
|
||||||
for (i,h) in enumerate(header):
|
for (i,h) in enumerate(header):
|
||||||
if "nom" in h.lower():
|
if "nom" in h.lower():
|
||||||
compte["nom"] = c[i]
|
compte["nom"] = c[i].capitalize()
|
||||||
if "montant" in h.lower():
|
if "montant" in h.lower():
|
||||||
compte["montant"] = eval(c[i])
|
compte["montant"] = eval(c[i])
|
||||||
if "part" in h.lower():
|
if "part" in h.lower():
|
||||||
@ -44,15 +43,13 @@ def extrait_from_file(file_name):
|
|||||||
comptes += [compte]
|
comptes += [compte]
|
||||||
|
|
||||||
return comptes
|
return comptes
|
||||||
finally:
|
|
||||||
file.close()
|
|
||||||
|
|
||||||
def forfait(compte, output = print):
|
def forfait(compte, output = print):
|
||||||
"""
|
"""
|
||||||
Gère le nombre de parts et affiche quelques stats
|
Affiche quelques stats et calcule les crédits et dettes
|
||||||
|
|
||||||
:param compte: comptes de ce que chacun a payé [[nom, montant,parts],...]
|
:param compte: list of dictionnaries [{nom:... ,montant:... ,parts:... },...]
|
||||||
:return: Comptes des crédits de chacun envers la communauté
|
:return: list of tuples [(name, cred_dept), ...]
|
||||||
"""
|
"""
|
||||||
cout_total = sum([c["montant"] for c in compte])
|
cout_total = sum([c["montant"] for c in compte])
|
||||||
output("Cout total: {cout_tot}".format(cout_tot = cout_total))
|
output("Cout total: {cout_tot}".format(cout_tot = cout_total))
|
||||||
@ -65,11 +62,11 @@ def forfait(compte, output = print):
|
|||||||
|
|
||||||
output("\n")
|
output("\n")
|
||||||
|
|
||||||
new_compte = []
|
cred_dept = []
|
||||||
for pers in compte:
|
for pers in compte:
|
||||||
new_compte += [[pers["nom"], (pers["montant"] - pers["parts"] * cout_jour)]]
|
cred_dept += [[pers["nom"], (pers["montant"] - pers["parts"] * cout_jour)]]
|
||||||
|
|
||||||
return new_compte
|
return cred_dept
|
||||||
|
|
||||||
def normalise(compte):
|
def normalise(compte):
|
||||||
"""Centre en 0 les comptes"""
|
"""Centre en 0 les comptes"""
|
||||||
@ -162,12 +159,12 @@ if __name__ == '__main__':
|
|||||||
cred_dept = []
|
cred_dept = []
|
||||||
for f in path.glob('*.csv'):
|
for f in path.glob('*.csv'):
|
||||||
print(f"Compte: {f.name}")
|
print(f"Compte: {f.name}")
|
||||||
compte = extrait_from_file(f)
|
compte = account_from_file(f)
|
||||||
compte_normalise = forfait(compte)
|
compte_normalise = forfait(compte)
|
||||||
cred_dept = join_comptes(cred_dept, compte_normalise)
|
cred_dept = join_comptes(cred_dept, compte_normalise)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
compte = extrait_from_file(options.file_name)
|
compte = account_from_file(options.file_name)
|
||||||
cred_dept = forfait(compte)
|
cred_dept = forfait(compte)
|
||||||
|
|
||||||
final = tribut(cred_dept, options.seuil)
|
final = tribut(cred_dept, options.seuil)
|
||||||
|
Loading…
Reference in New Issue
Block a user