-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbudget.py
More file actions
121 lines (103 loc) · 4.11 KB
/
budget.py
File metadata and controls
121 lines (103 loc) · 4.11 KB
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
class Category:
def __init__(self, category):
self.ledger = []
self.category = category.capitalize()
def __str__(self):
string = """"""
# Add title
while len(string) < 30:
if len(string) == int((30-len(self.category))/2):
string += self.category.capitalize()
else:
string += '*'
string += '\n'
# Add items
for item in self.ledger:
desc = item['description'][:23]
while len(desc) < 23:
desc += ' '
# Create amount string (right side of line)
# right aligned, max 7 char, 2 decimal places
amount = list("{:.2f}".format(item['amount'])[:7])
while len(amount) < 7:
amount.insert(0, ' ')
string += desc + ''.join(amount) + '\n'
# Add total
string += 'Total: {:.2f}'.format(self.get_balance())
return string
def deposit(self, amount, description=''):
self.ledger.append({'amount': amount, 'description': description})
def get_balance(self):
return sum([x['amount'] for x in self.ledger])
def check_funds(self, amount):
enough_funds = False
if amount <= self.get_balance():
enough_funds = True
return enough_funds
def withdraw(self, amount, description=''):
withdrawn = False
if self.check_funds(amount):
self.ledger.append({'amount': -amount, 'description': description})
withdrawn = True
return withdrawn
def transfer(self, amount, recipient):
transferred = False
if self.check_funds(amount):
self.withdraw(amount, f'Transfer to {recipient.category}')
recipient.deposit(amount, f'Transfer from {self.category}')
transferred = True
return transferred
def create_spend_chart(categories):
string = """Percentage spent by category\n"""
# Create list with percentages from 100 to 0 at 10 intervals
graph_pcts = [str(x) for x in range(100, -1, -10)]
# Create list with amounts of withdrawals for category
categ_names = [] # list of the category names passed into the function
expenses = []
for i, category in enumerate(categories):
# Add an integer to next index in expenses list
expenses.append(0)
categ_names.append(category.category)
for dict in category.ledger:
if dict['amount'] < 0:
# Add expense amount to new integer in list
expenses[i] += dict['amount']
# Create list with the percentages to draw in the graph
expenses_pct = [str(int(expense/sum(expenses)*10)*10) for expense in expenses]
# Have we started drawing the bar for each of the categories?
bar_started = [False for i in range(4)]
# If so, where have we started?
first_started_at = 0
# For each line in the graph (string)
for pct in graph_pcts:
# Add leading space(s), percentage, and pipe to graph
if len(pct) == 2:
string += ' '
elif len(pct) == 1:
string += ' '
string += pct + '| '
# Should we start drawing the bar?
if pct in expenses_pct:
bar_started[expenses_pct.index(pct)] = True
# Draw the bars
for i in range(len(categ_names)):
if bar_started[i]:
string += 'o '
else:
string += ' '*3
string += '\n'
# Add horizontal bar of '-'
string += ' '*4 + '-' + '---'*bar_started.count(True) + '\n'
# Add categories names arranged vertically
no_of_lines = max([len(categ) for categ in categ_names]) # the length of longest name
for n in range(no_of_lines):
# Add line leading spaces
string += ' '*5
for categ in categ_names:
if n < len(categ):
string += categ[n] + ' '*2
else:
string += ' '*3
if categ == categ_names[-1]:
string += '\n'
return string[:-1]