''' Created on May 20, 2014 @author: Brett Paufler 5-20-14 Copyright Brett Paufler Loan calculator outputs two different csv files Not reviewed recently, so could use a bit of cleaning up Better than starting from scratch, maybe... 3pm - 4pm firstShift 1hr, prettyPrint took most of the time, that and creating an object 6pm - 8pm end 3hrs I think it's done???? BELOW ARE THE STARTING INPUT VALUES loanStartAmount = total Loan daysInTerm are how many days between payments 30.42 to a month 365 to a year numTerms is how many iterations to do monthly = True, prints output monthly, False prints yearly iPList is a List of tuples (i,p) i = interest p = points ''' import math print "Starting loanMatrix" #30.42 days in a month loanStartAmount = 1000 daysInTerm = 30.42 numTerms = 360 amountOfPayment = 5 paymentInCents = amountOfPayment * 100 iPList = [(1,1), (2,2), (3,3), (4,4) ] def ppC(n): '''transforms a cent integer into a dollar string 24 becomes $0.24 2401 becomes $24.01 rounds up 2401.1 becomes $24.02 ''' #print "Pretty Print Called" pos = True n = math.ceil(n) if n < 0: pos = False n = int(abs(n)) #print "abs n" #print n s = str(n) l = len(s) #print l if l == 1: s = "$0.0%s" % s elif l == 2: s = "$0.%s" % s elif l <= 5: s = "$%s.%s" % (s[0:(l-2)], s[(l-2):]) elif l <= 8: s = "$%s,%s.%s" % (s[0:(l-5)],s[(l-5):(l-2)], s[(l-2):]) elif l <= 11: s = "$%s,%s,%s.%s" % (s[0:(l-8)], s[(l-8):(l-5)],s[(l-5):(l-2)], s[(l-2):]) elif l <= 14: s = "$%s,%s,%s,%s.%s" % (s[0:(l-11)] ,s[(l-11):(l-8)], s[(l-8):(l-5)],s[(l-5):(l-2)], s[(l-2):]) else: print "Too Big, need to extend ppC" if not pos: s = "-" + s return s class Loan(): def __init__(self): self.startingLoanCents = 0 self.interest = 0 self.points = 0 self.centsOwed = 0 self.rList = [] self.termRate = 0 def printStatus(self): print "Current Loan Case Summary" print "Starting Loan Amount = %s" % ppC(self.startingLoanCents) print "Interest Rate = %d" % self.interest print "Points Paid = %s" % self.points print "Outstanding Balance (now owed) = %s" % ppC(self.centsOwed) print "Running List rList = " + str(self.rList) print "TermRate = %f" % self.termRate #interest Point Pair List #interest First, then Points cases = [] #initializes the cases list with the appropriate Loan cases for iP in iPList: #a = '' a = Loan() a.startingLoanCents = int(math.ceil(loanStartAmount * 100)) print "raw sLC" print a.startingLoanCents a.interest, a.points = iP a.centsOwed = loanStartAmount * 100 + math.ceil(loanStartAmount * a.points) a.termRate = (1.0000000000 + (a.interest / 36500.000)) ** daysInTerm a.printStatus() cases.append(a) #print a.termRate for case in cases: case.rList.append(ppC(case.centsOwed)) for term in range(1, numTerms + 1, 1): if case.termRate: case.centsOwed = case.centsOwed*case.termRate - paymentInCents else: case.centsOwed -= paymentInCents case.rList.append(ppC(case.centsOwed)) if case.centsOwed <= 0: case.termRate = 0.00 #print case.centsOwed #print ppC(case.centsOwed) case.printStatus() def pS(s): ''' returns a string in quotes for csv ''' s = str(s) s = '"' + s + '"' return s #LMO = loanMatrixOutput #This is the Montly Output Chart LMO_Path = "loanMatrixMonthly.csv" with open(LMO_Path, 'w') as LMO: tS = "" for i in range(0,len(cases[0].rList),1): tS += ", M%d " % i #print tS header = "Interest, Points, Loan Amount, Payment Amount, Payment Term, --- , --- %s \n" % tS LMO.write(header) ''' If you have to start modifying this, pull out as a seperate function used in the yearly below ''' for case in cases: LMO.write(pS(case.interest)) LMO.write(',') LMO.write(pS(case.points)) LMO.write(',') LMO.write(pS(ppC(case.startingLoanCents))) LMO.write(',') LMO.write(pS(ppC(paymentInCents))) LMO.write(',') LMO.write(pS(daysInTerm)) LMO.write(',') LMO.write(',') for item in case.rList: LMO.write(',') LMO.write(pS(item)) LMO.write('\n') #This is the Yearly Output Chart LMO_Path = "loanMatrixYearly.csv" with open(LMO_Path, 'w') as LMO: tS = "" for i in range(0 , (int((len(cases[0].rList))/12) +1) ,1): tS += ", Y%d " % i tS += ",FINAL" tS = "" for i in range(0,len(cases[0].rList),1): if i%12 == 0: tS += ", Y%d " % int(i/12) #print tS header = "Interest, Points, Loan Amount, Payment Amount, Payment Term, --- , --- %s \n" % tS LMO.write(header) ''' If you have to start modifying this, pull out as a separate function used in the monthly above ''' for case in cases: LMO.write(pS(case.interest)) LMO.write(',') LMO.write(pS(case.points)) LMO.write(',') LMO.write(pS(ppC(case.startingLoanCents))) LMO.write(',') LMO.write(pS(ppC(paymentInCents))) LMO.write(',') LMO.write(pS(daysInTerm)) LMO.write(',') LMO.write(',') #This part is different from the monthly to yearly #THis is logic for pulling out every twelfth data point for num in range(0,len(cases[0].rList),1): if num%12 == 0: LMO.write(',') LMO.write(pS(case.rList[num])) LMO.write('\n') print "Ending LoanMatrix"