Feat: turn Account to a single function
This commit is contained in:
parent
bf34d77bb0
commit
a4feb1c310
64
account.py
64
account.py
@ -1,64 +0,0 @@
|
|||||||
#!/usr/bin/env python
|
|
||||||
# encoding: utf-8
|
|
||||||
|
|
||||||
|
|
||||||
class Account(object):
|
|
||||||
"""@todo"""
|
|
||||||
|
|
||||||
def __init__(self, account):
|
|
||||||
"""Initiate the account as a list
|
|
||||||
|
|
||||||
:param account: list of tuples (length 2 with name and credit) [("bob", 4), ("hip",2)...]
|
|
||||||
|
|
||||||
"""
|
|
||||||
self._account = account
|
|
||||||
|
|
||||||
def tribut(self, seuil = 0):
|
|
||||||
"""Tells who owns what to who (recursive algorithm
|
|
||||||
|
|
||||||
:param seuil: seuil à partir duquel on ne doit plus rien.
|
|
||||||
:return: liste des valeurs associées aux débits des comptes après le remboursement
|
|
||||||
"""
|
|
||||||
self._account.sort(key = lambda s: s[1])
|
|
||||||
m = self._account[0]
|
|
||||||
M = self._account[-1]
|
|
||||||
|
|
||||||
res = ['doneur','receveur',0]
|
|
||||||
|
|
||||||
# Si celui qui doit le plus doit moins que le crédit que celui qui a le plus grand crédit
|
|
||||||
# Il va donc tout lui donner
|
|
||||||
if abs(m[1]) <= abs(M[1]):
|
|
||||||
M2 = M[1] + m[1]
|
|
||||||
# print("{em} donne {em_n} à {eM} donc {em} devient {eM2}".format(em = m[0], em_n = abs(m[1]), eM = M[0] , eM2 = M2))
|
|
||||||
res = [m[0], M[0], abs(m[1])]
|
|
||||||
# On enleve le minimum
|
|
||||||
self._account.remove(m)
|
|
||||||
# ON change la valeur du max
|
|
||||||
self._account[-1] = [M[0],M2]
|
|
||||||
|
|
||||||
# Sinon il donne juste de quoi compenser
|
|
||||||
else:
|
|
||||||
m2 = m[1] + M[1]
|
|
||||||
# print("{em} donne {eM_n} à {eM} et devient {em2}".format(em = m[0] , em2 = m2 , eM = M[0], eM_n = M[1]))
|
|
||||||
res = [m[0], M[0], abs(M[1])]
|
|
||||||
# On eleve le max
|
|
||||||
self._account.remove(M)
|
|
||||||
# On change la valeur du min
|
|
||||||
self._account[0] = [m[0], m2]
|
|
||||||
|
|
||||||
# Gestion du Seuil
|
|
||||||
self._account = [i for i in self._account if abs(i[1]) >= seuil]
|
|
||||||
|
|
||||||
if (len(self._account) > 1):
|
|
||||||
#print(self._account)
|
|
||||||
return [res] + self.tribut(seuil=seuil)
|
|
||||||
else:
|
|
||||||
return [res]
|
|
||||||
|
|
||||||
def __add__(self, account):
|
|
||||||
pass
|
|
||||||
|
|
||||||
# -----------------------------
|
|
||||||
# Reglages pour 'vim'
|
|
||||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
||||||
# cursor: 16 del
|
|
54
pralo.py
54
pralo.py
@ -17,8 +17,6 @@ import os
|
|||||||
import optparse
|
import optparse
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from account import Account
|
|
||||||
|
|
||||||
def extrait_from_file(file_name):
|
def extrait_from_file(file_name):
|
||||||
"""
|
"""
|
||||||
Extrait les informations à partir d'un fichier
|
Extrait les informations à partir d'un fichier
|
||||||
@ -105,6 +103,48 @@ def join_comptes(compte1, compte2):
|
|||||||
joined = {k: c_dict1.get(k, 0) + c_dict2.get(k, 0) for k in set(c_dict1) | set(c_dict2)}
|
joined = {k: c_dict1.get(k, 0) + c_dict2.get(k, 0) for k in set(c_dict1) | set(c_dict2)}
|
||||||
return [(k, v) for k,v in joined.items()]
|
return [(k, v) for k,v in joined.items()]
|
||||||
|
|
||||||
|
def tribut(account, seuil = 0):
|
||||||
|
"""Tells who owns what to who (recursive algorithm)
|
||||||
|
|
||||||
|
:param seuil: seuil à partir duquel on ne doit plus rien.
|
||||||
|
:return: liste des valeurs associées aux débits des comptes après le remboursement
|
||||||
|
"""
|
||||||
|
account.sort(key = lambda s: s[1])
|
||||||
|
m = account[0]
|
||||||
|
M = account[-1]
|
||||||
|
|
||||||
|
res = ['doneur','receveur',0]
|
||||||
|
|
||||||
|
# Si celui qui doit le plus doit moins que le crédit que celui qui a le plus grand crédit
|
||||||
|
# Il va donc tout lui donner
|
||||||
|
if abs(m[1]) <= abs(M[1]):
|
||||||
|
M2 = M[1] + m[1]
|
||||||
|
# print("{em} donne {em_n} à {eM} donc {em} devient {eM2}".format(em = m[0], em_n = abs(m[1]), eM = M[0] , eM2 = M2))
|
||||||
|
res = [m[0], M[0], abs(m[1])]
|
||||||
|
# On enleve le minimum
|
||||||
|
account.remove(m)
|
||||||
|
# ON change la valeur du max
|
||||||
|
account[-1] = [M[0],M2]
|
||||||
|
|
||||||
|
# Sinon il donne juste de quoi compenser
|
||||||
|
else:
|
||||||
|
m2 = m[1] + M[1]
|
||||||
|
# print("{em} donne {eM_n} à {eM} et devient {em2}".format(em = m[0] , em2 = m2 , eM = M[0], eM_n = M[1]))
|
||||||
|
res = [m[0], M[0], abs(M[1])]
|
||||||
|
# On eleve le max
|
||||||
|
account.remove(M)
|
||||||
|
# On change la valeur du min
|
||||||
|
account[0] = [m[0], m2]
|
||||||
|
|
||||||
|
# Gestion du Seuil
|
||||||
|
account = [i for i in account if abs(i[1]) >= seuil]
|
||||||
|
|
||||||
|
if (len(account) > 1):
|
||||||
|
#print(account)
|
||||||
|
return [res] + tribut(account, seuil=seuil)
|
||||||
|
else:
|
||||||
|
return [res]
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# Pour analyser les options qu'on lui demande
|
# Pour analyser les options qu'on lui demande
|
||||||
parser = optparse.OptionParser()
|
parser = optparse.OptionParser()
|
||||||
@ -119,22 +159,18 @@ if __name__ == '__main__':
|
|||||||
path = Path(options.file_name)
|
path = Path(options.file_name)
|
||||||
assert path.exists()
|
assert path.exists()
|
||||||
if path.is_dir():
|
if path.is_dir():
|
||||||
god = []
|
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 = extrait_from_file(f)
|
||||||
compte_normalise = forfait(compte)
|
compte_normalise = forfait(compte)
|
||||||
god = join_comptes(god, compte_normalise)
|
cred_dept = join_comptes(cred_dept, compte_normalise)
|
||||||
|
|
||||||
account = Account(god)
|
|
||||||
else:
|
else:
|
||||||
compte = extrait_from_file(options.file_name)
|
compte = extrait_from_file(options.file_name)
|
||||||
# compte_normalise = normalise(compte)
|
|
||||||
cred_dept = forfait(compte)
|
cred_dept = forfait(compte)
|
||||||
#final = echange(compte_normalise, options.seuil)
|
|
||||||
account = Account(cred_dept)
|
|
||||||
|
|
||||||
final = account.tribut(options.seuil)
|
final = tribut(cred_dept, options.seuil)
|
||||||
affiche_final(final)
|
affiche_final(final)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user