-
Notifications
You must be signed in to change notification settings - Fork 1
/
debt_snowball.py
297 lines (225 loc) · 9.47 KB
/
debt_snowball.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime
from decimal import Decimal
import re
import sys
from urllib.parse import urlparse
from dateutil.relativedelta import relativedelta
from jinja2 import Template
from werkzeug import Request, Response
from werkzeug.exceptions import HTTPException, BadRequest, NotFound
import debt_snowball_config as rc
__version__ = '2020-11-13-16-05'
class NoFormData(BadRequest):
pass
class MissingFields(Exception):
pass
class NegativeNumbers(Exception):
pass
class TooFewDebts(Exception):
pass
class RisingBalance(Exception):
pass
class InvalidData(Exception):
pass
class DuplicateNames(Exception):
pass
def money_fmt(value, places=2, curr='$', sep=',', dp='.',
pos='', neg='-', trailneg=''):
"""Convert Decimal to a money formatted string.
Gleaned from https://docs.python.org/3/library/decimal.html#recipes
places: required number of places after the decimal point
curr: optional currency symbol before the sign (may be blank)
sep: optional grouping separator (comma, period, space, or blank)
dp: decimal point indicator (comma or period)
only specify as blank when places is zero
pos: optional sign for positive numbers: '+', space or blank
neg: optional sign for negative numbers: '-', '(', space or blank
trailneg:optional trailing minus indicator: '-', ')', space or blank
>>> d = Decimal('-1234567.8901')
>>> moneyfmt(d, curr='$')
'-$1,234,567.89'
>>> moneyfmt(d, places=0, sep='.', dp='', neg='', trailneg='-')
'1.234.568-'
>>> moneyfmt(d, curr='$', neg='(', trailneg=')')
'($1,234,567.89)'
>>> moneyfmt(Decimal(123456789), sep=' ')
'123 456 789.00'
>>> moneyfmt(Decimal('-0.02'), neg='<', trailneg='>')
'<0.02>'
"""
if not isinstance(value, Decimal):
value = Decimal(value)
q = Decimal(10) ** -places # 2 places --> '0.01'
sign, digits, exp = value.quantize(q).as_tuple()
result = []
digits = list(map(str, digits))
build, next = result.append, digits.pop
if sign:
build(trailneg)
for i in range(places):
build(next() if digits else '0')
if places:
build(dp)
if not digits:
build('0')
i = 0
while digits:
build(next())
i += 1
if i == 3 and digits:
i = 0
build(sep)
build(curr)
build(neg if sign else pos)
return ''.join(reversed(result))
def do_amortization(debt_name, balance, payment, apr, additional_start=datetime.date(9999, 12,31), additional_payment=0):
apr = float(apr)/100.0
monthly_pr = apr/12.0
if rc.debug: #pragma: no cover
print("month, start_balance, new_balance, paid_balance, payment, interest_payment, principal_payment")
month_count = 0
start_date = datetime.date.today()
results = []
original_payment = float(payment)
balance = float(balance)
additional_payment = float(additional_payment)
while balance > 0:
start_balance = balance
new_balance = balance + balance * monthly_pr
this_month = start_date + relativedelta(months=month_count)
if additional_payment > 0 and this_month > additional_start:
payment = original_payment + additional_payment
else:
payment = original_payment
paid_balance = new_balance - payment
if paid_balance >= start_balance:
raise RisingBalance(debt_name)
if paid_balance < 0:
paid_balance = 0
payment = new_balance
interest_payment = new_balance - start_balance
principal_payment = payment - interest_payment
balance = paid_balance
if rc.debug: #pragma: no cover
print("%s-%s" % (this_month.year, this_month.month),
money_fmt(start_balance), money_fmt(new_balance),
money_fmt(paid_balance), money_fmt(payment),
money_fmt(interest_payment), money_fmt(principal_payment))
month_count += 1
results.append({'month': this_month,
'start_balance': money_fmt(start_balance),
'payment': money_fmt(payment),
'new_balance': money_fmt(new_balance),
'paid_balance': money_fmt(paid_balance),
'interest_payment': money_fmt(interest_payment),
'principal_payment': money_fmt(principal_payment)})
return results
def sort_by_payoff_time(fields):
debts = {}
for num in range(1, int(fields['row_count']) + 1):
debt_name = fields["debt_name_%s" % num]
if debt_name == '':
continue
balance = fields["balance_%s" % num]
payment = fields["payment_%s" % num]
apr = fields["apr_%s" % num].strip()
payments = len(do_amortization(debt_name, balance, payment, apr))
debts[debt_name] = {'debt_name': debt_name, 'payments': payments, 'balance': balance,
'payment': payment, 'apr': apr}
sorted_debts = sorted(debts.values(), key=lambda x: x['payments'])
return sorted_debts
def calculate_combined_payoff_tables(sorted_debts):
additional_start = datetime.date(9999, 12,31)
additional_payment = 0
results = []
for debt in sorted_debts:
results.append({'debt_name': debt['debt_name'],
'payoff_chart': do_amortization(debt['debt_name'],
debt['balance'], debt['payment'],
debt['apr'], additional_start,
additional_payment)})
additional_start = results[-1]['payoff_chart'][-1]['month']
additional_payment = additional_payment + float(debt['payment'])
return results
def process_form(fields):
##TODO: Do this checking client-side too
##TODO: Return names of fields to highlight in red
##TODO: Move a good chunk of this into validate_form()
debt_count = 0
debt_names = {}
# Sanitized fields
s_fields = fields.copy()
# Make sure all values are filled out if one value on a line is filled out
for num in range(1, int(fields['row_count']) + 1):
not_blank = [bool(fields["%s_%s" % (f, num)].strip()) for f in ['debt_name', 'balance', 'payment', 'apr']]
if any(not_blank) and not all(not_blank):
raise MissingFields
for f in ['balance', 'payment', 'apr']:
s_fields["%s_%s" % (f, num)] = re.sub('[$%,]', '', s_fields["%s_%s" % (f, num)].strip())
if any([s_fields["debt_name_%s" % (num)] != '' and float(s_fields["%s_%s" % (f, num)]) < 0 for f in ['balance', 'payment', 'apr']]):
raise NegativeNumbers
if s_fields["debt_name_%s" % (num)] == '':
continue
debt_names[s_fields["debt_name_%s" % num]] = 1
debt_count += 1
if debt_count < 2:
raise TooFewDebts
if len(debt_names) < debt_count:
raise DuplicateNames
sorted_debts = sort_by_payoff_time(s_fields)
payoff_tables = calculate_combined_payoff_tables(sorted_debts)
return render_page(fields, payoff_tables, '')
def render_page(fields={}, results=[], message=''):
template = Template(open(rc.template_file).read())
return template.render(fields=fields, results=results, message=message,
data_ad_client=rc.data_ad_client, version=__version__)
@Request.application
def application(request):
response = Response(mimetype='text/html')
try:
# Only accept requests for rc.base_path
if urlparse(request.url).path != rc.base_path:
raise NotFound
fields = request.form.to_dict()
if request.method == 'GET':
raise NoFormData
elif request.method == 'POST':
if not request.form:
raise NoFormData
else:
raise BadRequest
response.data = process_form(fields)
except NoFormData as e:
response.data = render_page()
except MissingFields as e:
response.data = render_page(fields=fields,
message='All fields on a line must be filled out.')
except TooFewDebts as e:
response.data = render_page(fields=fields,
message='Two or more debts must be provided.')
except NegativeNumbers as e:
response.data = render_page(fields=fields,
message='All numbers must be positive.')
except DuplicateNames as e:
response.data = render_page(fields=fields,
message='To avoid confusion, all debts must have unique names.')
except RisingBalance as e:
response.data = render_page(fields=fields,
message="Debt '%s' does not have a large enough payment to reduce the balance." % e.args[0])
except ValueError as e:
response.data = render_page(fields=fields,
message='Balance, payment, and APR must be numeric.')
except HTTPException as e:
return e
return response
if __name__ == '__main__': #pragma: no cover
from werkzeug.serving import run_simple
if sys.argv[1:]:
port = int(sys.argv[1])
else:
port = 8000
print("Serving on port %s" % port)
run_simple('127.0.0.1', port, application, use_debugger=False, use_reloader=False,
passthrough_errors=True, threaded=False)