-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBudgeter.py
101 lines (90 loc) · 3.23 KB
/
Budgeter.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
import getopt,sys
from Account import Account
from Meridian import Meridian
from Cibc import Cibc
from datetime import datetime
# iterate over years
def month_year_iter( start_month, start_year, end_month, end_year ):
ym_start= 12*start_year + start_month - 1
ym_end= 12*end_year + end_month - 1
for ym in range( ym_start, ym_end+1 ):
y, m = divmod( ym, 12 )
yield y, m+1
def analyze(accounts):
print "Analyzing"
#add transactions from every account and sort
t = []
for a in accounts:
t.extend(a.d)
t.sort(key=lambda k: k['date'])
#calc for each month, start at first trans
sy = t[0]['date'].year
sm = t[0]['date'].month
ey = t[-1]['date'].year
em = t[-1]['date'].month
mos = []
i = 0
for y,m in month_year_iter(sm,sy,em,ey):
#cycle through transaction
spending , income = 0,0
print
while i < len(t) and t[i]['date'].month == m and t[i]['date'].year == y:
#process
if not specialCondition(t[i]['name']):
spending += t[i]['credit']
income += t[i]['debit']
i += 1
month = {'date':datetime(y,m,1),'spending':spending,'income':income}
print "%d %d : Spent $ %.2f , Made $ %.2f" % (y,m,spending,income)
mos.append(month)
print
#print highest spending, income months
highestS = max(mos,key=lambda k: k['spending'])
lowestS = min(mos,key=lambda k: k['spending'])
highestI = max(mos,key=lambda k: k['income'])
AvInc = sum(map(lambda x: x['income'],mos))/len(mos)
print "Highest spending on %d %d with $ %.2f" % (highestS['date'].year, highestS['date'].month, highestS['spending'])
print "Lowest spending on %d %d with $ %.2f" % (lowestS['date'].year, lowestS['date'].month, lowestS['spending'])
print "Highest income on %d %d with $ %.2f" % (highestI['date'].year, highestI['date'].month, highestI['income'])
print "Average income: $ %.2f" % AvInc
print
spending = 0
income = 0
for l in t:
if not specialCondition(l['name']):
spending += l['credit']
income += l['debit']
print 'Total Spending: ' + str(spending)
print 'Total Income: ' + str(income)
print '-----------------------------'
print 'Total Savings: ' + str(income - spending)
def specialCondition(n):
# do not include itrade transfers, CIBC is included
codeWords = ["ITrade","In from advntge","Cheque 50","INSTANT TELLER DEPOSIT","PAYMENT THANK YOU","INTERNET TRANSFER"]
for c in codeWords:
if n.find(c) > -1:
return True
def main(argv):
try:
opts,args = getopt.getopt(argv, "m:c:v:", ["meridian=", "cibc=", 'visa='])
except getopt.GetoptError:
sys.exit(2)
accounts = []
#process files
print opts
for opt, arg in opts:
if opt in ("-m", "--meridian"):
#open file
m = Meridian(arg)
accounts.append(m)
elif opt in ("-c", "--cibc"):
#open file
c = Cibc(arg)
accounts.append(c)
elif opt in ("-v", "--visa"):
#open file
v = Cibc(arg)
accounts.append(v)
analyze(accounts)
if __name__ == '__main__':
main(sys.argv[1:])