""" Author : flo date : 2017.03.10 Purpose : example of finding out the amount of principle paid to mortgage lender per year. : This version is uniquely special. Escrow was ignored, since it doesnt pay down the overall : amount of the total. Comments: Simulate the number of mortgage payments based on the loan amount. : : : run the example command to first see how it needs to run. : Getopt is interesting to use, short and long parameters in function do worked. : Improvements: The next version will need to have a feature to allow use to enter in additional amount of : principle. It will be some sort of switch to indicate this feature along with a second switch to : notify this program as far as timeframe -ep extraPrincipleAmount -dw DelayWindowWhenInFuture example : python mortgage-0.0.6.py -h example : python mortgage-0.0.6.py -v """ import getopt import sys import datetime import calendar def computeMortgage(tot, mon, rate, intr, pal): now = datetime.datetime.now() currentMonth = now.month currentYear = now.year mtsum = 0.00 for i in range(1, mon+1): daysInMonth = calendar.monthrange(currentYear, currentMonth)[1] for x in range(1, daysInMonth+1): tsum = tot * (rate/100.00) * (1.00/365.00) mtsum += tsum tot = tsum + tot if x < 10: dvalue = "0" + str(x) else: dvalue = str(x) if currentMonth < 10: mvalue = "0" + str(currentMonth) else: mvalue = str(currentMonth) print " Year " + str(currentYear) + " Month " + str(mvalue) + " Day " + str(dvalue) + " " + \ format(tot, '.4f') + " " + format(tsum, '.4f') if currentMonth == 12: currentYear += 1 currentMonth = 1 else: currentMonth += 1 tot = tot - (pal + intr) print " Actual Uptodate Interests Paid: " + format(mtsum, '.4f') def version(): print "version 0.0.6" def usage(): usage = """ usage: python mortgage.py [-l,--la= FLOAT_VALUE] [-t,--lt= Integer_VALUE] [-r,--lr= FLOAT_VALUE] [-i,--li= FLOAT_VALUE] [-p,--lp= FLOAT_VALUE] [-h] [-v] examples: python mortgage.py python mortgage.py -l 250000.99 -t 24 -r 4.95 -i 515.69 -p 600 python mortgage.py -l 250000.99 -t 24 -r 4.95 -i 0 -p 0 python mortgage.py --la 250000.99 --lt 24 --lr 4.95 --li 515.69 --lp 600 python mortgage.py --la=250000.99 --lt=24 --lr=4.95 --li=515.69 --lp=600 python mortgage.py -v python mortgage.py -h options : -l, --la=value, --la value take a loan Value amount -t, --lt=value, --lt value take an actual or a hypothetical value term -r, --lr=value, --lr value take the monthly interests value rate -i, --li=value, --li value take the monthly interests value amount -p, --lp=value, --lp value take the monthly principle value amount -v show program's version number and exit -h show this help message and exit """ print usage def main(): # default values lamt, lterm, lrate, lint, lprin = 200000.99, 10, 3.89, 450, 600.99 # try the getopt function for kicks and giggles try: opts, args = getopt.getopt(sys.argv[1:], 'l:t:r:i:p:vh', ['la=', 'lt=', 'lr=', 'li=', 'lp=']) except getopt.GetoptError as err: # print something and exit print str(err) usage() sys.exit(2) # pull data from getopt # may need to catch exception when o[1] is empty for o, a in opts: if o in ('-l', '--la'): if a.replace('.','',1).isdigit(): lamt = float(a) else: print "Please enter your loan amount in digit form. " sys.exit(2) elif o in ('-t', '--lt'): if a.isdigit(): lterm = int(a) else: print "Please enter your loan duration in digit form. " sys.exit(2) elif o in ('-r', '--lr'): if a.replace('.','',1).isdigit(): lrate = float(a) else: print "Please enter your loan rate in digit form. " sys.exit(2) elif o in ('-i', '--li'): if a.replace('.','',1).isdigit(): lint = float(a) else: print "Please enter your loan accrual monthly interests in digit form. " sys.exit(2) elif o in ('-p', '--lp'): if a.replace('.','',1).isdigit(): lprin = float(a) else: print "Please enter your loan accrual monthly principle in digit form. " sys.exit(2) elif o in ('-v', '--version'): version() sys.exit() elif o in ('-h', '--help'): usage() sys.exit() else: assert False, "unhandled option" # compute and forecast it all computeMortgage(lamt, lterm, lrate, lint, lprin) # calling main if __name__ == "__main__": main()