diff --git a/Makefile b/Makefile index 2d851c9..d54cd5b 100644 --- a/Makefile +++ b/Makefile @@ -3,5 +3,5 @@ reference: amortize -P 100000 -r 0.05 -f monthly -n 36 -s > ref.txt test: - python amort.py -p 100000 -i 5.0 -t 3 -ot "{\"payment_number\":13,\"amount\":5000}" + python src/amort.py -p 100000 -i 5.0 -t 3 -ot "{\"payment_number\":13,\"amount\":5000}" diff --git a/amort.py b/src/amort.py similarity index 84% rename from amort.py rename to src/amort.py index df39cef..b06a824 100644 --- a/amort.py +++ b/src/amort.py @@ -1,6 +1,4 @@ -import argparse, json - def generate_amortization_schedule(principal, interest_rate, loan_term, extra_payments=[]): # Convert interest rate to decimal and calculate periodic interest rate monthly_interest_rate = interest_rate / 12 / 100 @@ -73,7 +71,30 @@ def get_totals(amortization_schedule, func=None): func(messages) return total_paid, total_interest_paid, total_principal_paid +def compare(with_extra_payments, without): + x,y,z = get_totals(with_extra_payments) + a,b,c = get_totals(without) + term_length = len(without) - len(with_extra_payments) + print(chr(916), "paid: ", round(x-a,2)) + print(chr(916), "interest paid: ", round(y-b,2)) + print(chr(916), "principal paid: ", round(abs(z-c),2)) + print(chr(916), "term (months): ", term_length) + +def display(table): + print("id, paid, interest payment, principal payment, remaining") + for row in table: + print(row) + +def export(table, filename="schedule.csv"): + with open(filename, 'w') as f: + print("id, paid, interest payment, principal payment, remaining", file=f) + for row in table: + print(row, file=f) + print("wrote to file", filename) + if __name__ == "__main__": + import argparse, json + def get_arguments(): p = argparse.ArgumentParser() p. add_argument("--principal", "-p", type=float, \ @@ -94,29 +115,12 @@ if __name__ == "__main__": # print(extra["extra-payments"]) if args.one_time is not None: l.append(json.loads(args.one_time)) - extra = l["extra-payments"] - extra.sort(key=lambda k: k["payment_number"]) + extra = [] + if "extra-payments" in l: + extra = l["extra-payments"] + extra.sort(key=lambda k: k["payment_number"]) return args.principal, args.interest_rate, args.term, extra - def compare(with_extra_payments, without): - x,y,z = get_totals(with_extra_payments) - a,b,c = get_totals(without) - print(chr(916), "paid: ", round(x-a,2)) - print(chr(916), "interest paid: ", round(y-b,2)) - print(chr(916), "principal paid: ", round(abs(z-c),2)) - - def display(table): - print("id, paid, interest payment, principal payment, remaining") - for row in table: - print(row) - - def export(table, filename="schedule.csv"): - with open(filename, 'w') as f: - print("id, paid, interest payment, principal payment, remaining", file=f) - for row in table: - print(row, file=f) - print("wrote to file", filename) - principal, interest_rate, loan_term, extra_payments = get_arguments() schedule = generate_amortization_schedule(principal, interest_rate, loan_term, extra_payments)