forked from zabatakram/Py_Expense_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expense.py
94 lines (77 loc) · 2.32 KB
/
expense.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
from PyInquirer import prompt
import csv
# Return a list with all the users
def get_users(answer):
with open('users.csv', newline='') as f:
reader = csv.reader(f)
data = list(reader)
flat_list = [item for sublist in data for item in sublist]
return flat_list
def list_to_object_list(answer):
l = get_users("e")
new_l = []
for i in l:
new_l.append({"name": i})
return new_l
expense_questions = [
{
"type": "input",
"name": "amount",
"message": "New Expense - Amount: ",
},
{
"type": "input",
"name": "label",
"message": "New Expense - Label: ",
},
{
"type": "list",
"name": "spender",
"message": "New Expense - Spender: ",
"choices": get_users
},
{
"type": "checkbox",
"qmark": "X",
"message": "Select participants",
"name": "participants",
"choices": list_to_object_list
}
]
# Return the row of the spender
def get_spender(spender):
with open('users.csv', 'r') as o:
myData = csv.reader(o)
i = 0
for row in myData:
i = i + 1
if row[0] == spender:
return i
# Modify users' balances
def add_default_expense(num, spenderId):
i = 1
with open('expenses.csv', 'r') as o:
myData = csv.reader(o)
l = list(myData)
i = 1
for elm in l:
if spenderId == i:
elm[0] = int(float(elm[0])) + (int(float(num)) / len(l))
else:
elm[0] = int(float(elm[0])) - (int(float(num)) / len(l))
i = i + 1
with open('expenses.csv', 'w') as file:
writer = csv.writer(file, delimiter='|',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
writer.writerows(l)
def new_expense(*args):
infos = prompt(expense_questions)
with open('expense_report.csv', 'a', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter='|',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
writer.writerow([infos["spender"]] +
[infos["amount"]] + [infos["label"]])
spender = get_spender(infos["spender"])
add_default_expense(infos["amount"], spender)
print("Expense Added !")
return True