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
|
||||
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
|
||||
:return: liste [[nom,montant,parts],...]
|
||||
:param file_name: csv file name
|
||||
:return: list of dictionnaries [{nom:... ,montant:... ,parts:... },...]
|
||||
"""
|
||||
file = open(file_name, "r")
|
||||
try:
|
||||
reader = csv.reader(file, delimiter = ",")
|
||||
with open(file_name, "r") as f:
|
||||
reader = csv.reader(f, delimiter = ",")
|
||||
r_comptes = list(reader)
|
||||
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 = {}
|
||||
for (i,h) in enumerate(header):
|
||||
if "nom" in h.lower():
|
||||
compte["nom"] = c[i]
|
||||
compte["nom"] = c[i].capitalize()
|
||||
if "montant" in h.lower():
|
||||
compte["montant"] = eval(c[i])
|
||||
if "part" in h.lower():
|
||||
@ -44,15 +43,13 @@ def extrait_from_file(file_name):
|
||||
comptes += [compte]
|
||||
|
||||
return comptes
|
||||
finally:
|
||||
file.close()
|
||||
|
||||
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],...]
|
||||
:return: Comptes des crédits de chacun envers la communauté
|
||||
:param compte: list of dictionnaries [{nom:... ,montant:... ,parts:... },...]
|
||||
:return: list of tuples [(name, cred_dept), ...]
|
||||
"""
|
||||
cout_total = sum([c["montant"] for c in compte])
|
||||
output("Cout total: {cout_tot}".format(cout_tot = cout_total))
|
||||
@ -65,11 +62,11 @@ def forfait(compte, output = print):
|
||||
|
||||
output("\n")
|
||||
|
||||
new_compte = []
|
||||
cred_dept = []
|
||||
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):
|
||||
"""Centre en 0 les comptes"""
|
||||
@ -162,12 +159,12 @@ if __name__ == '__main__':
|
||||
cred_dept = []
|
||||
for f in path.glob('*.csv'):
|
||||
print(f"Compte: {f.name}")
|
||||
compte = extrait_from_file(f)
|
||||
compte = account_from_file(f)
|
||||
compte_normalise = forfait(compte)
|
||||
cred_dept = join_comptes(cred_dept, compte_normalise)
|
||||
|
||||
else:
|
||||
compte = extrait_from_file(options.file_name)
|
||||
compte = account_from_file(options.file_name)
|
||||
cred_dept = forfait(compte)
|
||||
|
||||
final = tribut(cred_dept, options.seuil)
|
||||
|
Loading…
Reference in New Issue
Block a user