-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacc.py
157 lines (130 loc) · 4.7 KB
/
acc.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import csv
from datetime import datetime
import matplotlib
import matplotlib.pyplot as plt
from categories import CATEGORIES, UNCATEGORIZED_KEY
def convert_date(date):
return datetime.strptime(date, '%d/%m/%Y')
def _transaction(row):
transaction = {
'date': convert_date(row[1]),
'amount': row[3],
'type': row[4],
'description': row[5]
}
return transaction
def _transactions (filename='data.csv'):
transactions = []
with open(filename, 'r') as input_file:
reader = csv.reader(input_file)
next(reader, None)
for row in reader:
transaction = _transaction(row)
transactions.append(transaction)
return transactions
def _transactions_per_month(transactions, month=None):
transactions = _transactions()
if not month:
return transactions
result = []
for transaction in transactions:
if transaction['date'].month == month:
result.append(transaction)
return result
def _transactions_per_categories(transactions, categories=CATEGORIES):
keys = CATEGORIES.keys()
result = {UNCATEGORIZED_KEY: []}
for transaction in transactions:
match = False
for key in keys:
for substring in CATEGORIES[key]:
if substring.lower() in transaction['description'].lower():
if key in result:
result[key].append(transaction)
else:
result[key] = [transaction]
match = True
if match == False:
otherArray = result[UNCATEGORIZED_KEY]
otherArray.append(transaction)
return result
def _net_for_transactions(transactions):
result = 0.0
for transaction in transactions:
amount = transaction['amount']
amount = float(amount)
result += amount
print("Net is %s" % result)
return result
def _expenses_for_transactions(transactions):
result = 0.0
for transaction in transactions:
amount = transaction['amount']
amount = float(amount)
if amount < 0:
result += amount
return abs(result)
def _income_for_transactions(transactions):
result = 0.0
for transaction in transactions:
amount = transaction['amount']
amount = float(amount)
if amount > 0:
result += amount
return abs(result)
def _print_summary():
print('===========')
transactions = _transactions()
for month in range(0, 13):
transactions = _transactions_per_month(transactions, month)
income = _income_for_transactions(transactions)
expenses = _expenses_for_transactions(transactions)
if len(transactions) < 1:
continue
print("Income for month %s: %s" % (month, income))
print("Expenses for month %s: %s" % (month, expenses))
print("Net for month: %s" % (income - expenses))
print('===========')
print("Parsing %s transactions for month %s" % (len(transactions), month))
transactions = _transactions_per_categories(transactions)
for key, category_transactions in transactions.items():
print("Expenses for category %s with %s transactions: %s" % (
key,
len(category_transactions),
_expenses_for_transactions(category_transactions)))
print('===========')
def _graph_summary(start_month=0, num_months=12):
transactions = _transactions()
months = ["Tot",
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"]
expenses = [0]*13
incomes = [0]*13
nets = [0]*13
for index, month in enumerate(months):
transactions_for_month = _transactions_per_month(transactions, index)
expenses[index] = int(_expenses_for_transactions(transactions_for_month))
incomes[index] = int(_income_for_transactions(transactions_for_month))
if (index == 5):
# End of months salary + projecting expenses
incomes[index] += 3100
expenses[index] += 1000
nets[index] = int(incomes[index] - expenses[index])
plt.plot(months[start_month:num_months], expenses[start_month:num_months], label='Expenses')
plt.plot(months[start_month:num_months], incomes[start_month:num_months], label='Income')
plt.plot(months[start_month:num_months], nets[start_month:num_months], label='Net')
plt.legend()
plt.show()
if __name__ == "__main__":
_print_summary()
_graph_summary(start_month=1, num_months=6)