-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtirelire.py
executable file
·126 lines (114 loc) · 4.03 KB
/
tirelire.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Piggy bank application """
from __future__ import print_function
import shelve
from datetime import datetime
from datetime import date as dtdate
class Tirelire():
""" Piggy bank """
def __init__(self):
#: Total amount
self._total = 0.0
#: List of pigs
self._pig_names = []
#: List of pig's amounts
self._pig_amounts = []
#: Remaining amount
self._remaining = 0.0
#: History of the total amount
self._tirelire_history = []
#: Pigs' history
self._pigs_history = []
#: Name of the backup file
self._backup_filename = None
def provision(self, amount, comment, date=None):
""" Povide money to the Tirelire """
date, msg = gereDate(date)
self._total += amount
self._remaining += amount
self._tirelire_history.append((date, amount, "input",
comment))
self._pigs_history.append((date, amount, "input",
comment))
if msg:
print(msg)
return msg
def createPig(self, name, date=None):
""" Pig creation """
date, msg = gereDate(date)
self._pig_names.append(name)
self._pig_amounts.append(0.0)
self._pigs_history.append((date, None, "Pig creation",
name))
if msg:
print(msg)
return msg
def feed(self, name, amount, date=None):
""" Transfert money to the indicated pig """
date, msg = gereDate(date)
indice = self._pig_names.index(name)
self._pig_amounts[indice] += amount
self._remaining -= amount
self._pigs_history.append((date, amount,
"versement Cochon", name))
if msg:
print(msg)
return msg
def spend(self, name, amount, comment="", date=None):
""" Spend money from a pig """
date, msg = gereDate(date)
indice = self._pig_names.index(name)
self._pig_amounts[indice] -= amount
self._total -= amount
self._tirelire_history.append((date, amount, "outgo", comment))
self._pigs_history.append((date, amount, "spend Cochon", name))
if msg:
print(msg)
return msg
def __repr__(self):
""" Show pigs' state """
msg = "Remaining: %f\n" % self._remaining
msg += "---------\n"
for elem in self._pig_names:
msg += "%s: %f\n" % (elem, self._pig_amounts[self._pig_names.index(elem)])
return msg[:-1]
def showHistory(self):
""" Show total amount history """
for elem in self._tirelire_history:
print("%s %s | %s | %s" % elem)
def save(self, name_fichier=None):
""" Save Tirelire """
if name_fichier:
self._backup_filename = name_fichier
fic = shelve.open(self._backup_filename)
fic['Total'] = self._total
fic['Pigs'] = self._pig_names
fic['Amounts'] = self._pig_amounts
fic['Remaining'] = self._remaining
fic['GlobalHistory'] = self._tirelire_history
fic['PigsHistory'] = self._pigs_history
fic.close()
def load(self, name_fichier):
""" Load file """
self._backup_filename = name_fichier
fic = shelve.open(self._backup_filename)
self._total = fic['Total']
self._pig_names = fic['Pigs']
self._pig_amounts = fic['Amounts']
self._remaining = fic['Remaining']
self._tirelire_history = fic['GlobalHistory']
self._pigs_history = fic['PigsHistory']
fic.close()
def gereDate(date):
""" Return the date in Datetime format """
msg = None
if not date:
date = dtdate.today()
else:
try:
date = datetime.strptime(date, "%Y-%m-%d").date()
except ValueError:
msg = "Date incomprehensible, on prend la date actuelle"
date = dtdate.today()
return date, msg