Skip to content

Commit

Permalink
add extra_principle_payments for monthly_payment_schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
zochao committed Feb 23, 2017
1 parent e4ecef1 commit cfa0fd0
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions mortgage.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,37 @@ def total_value(self, m_payment):
def annual_payment(self):
return self.monthly_payment() * MONTHS_IN_YEAR

def total_payout(self):
return self.monthly_payment() * self.loan_months()

def monthly_payment_schedule(self):
def total_payout(self, extra_principle_payments=[]):
if extra_principle_payments == []:
# should use sum([sum(m) for m in monthly_payments]) but i m too lazy
return self.monthly_payment() * self.loan_months()
else:
monthly_payments = list(self.monthly_payment_schedule(extra_principle_payments=extra_principle_payments))
return sum([sum(m) for m in monthly_payments])

def monthly_payment_schedule(self, extra_principle_payments=[]):
monthly = self.monthly_payment()
balance = dollar(self.amount())
rate = decimal.Decimal(str(self.rate())).quantize(decimal.Decimal('.000001'))
epps = [dollar(e) for e in extra_principle_payments]
while True:
try:
extra_principle = epps.pop(0)
except IndexError as e:
extra_principle = dollar(0)

interest_unrounded = balance * rate * decimal.Decimal(1)/MONTHS_IN_YEAR
interest = dollar(interest_unrounded, round=decimal.ROUND_HALF_UP)

if monthly >= balance + interest:
yield balance, interest
# should add warning
# reset the extra_principle = 0 since there is no point to pay
yield balance, interest, extra_principle
break

principle = monthly - interest
yield principle, interest
balance -= principle
yield principle, interest, extra_principle
balance = balance - principle - extra_principle

def print_summary(m):
print('{0:>25s}: {1:>12.6f}'.format('Rate', m.rate()))
Expand Down

0 comments on commit cfa0fd0

Please sign in to comment.