fixed error when invoked without extra-payments, relocated stuff relating to main block

This commit is contained in:
scuti 2024-05-11 19:32:12 -07:00
parent 5cc813a346
commit b9d1382cd0
2 changed files with 28 additions and 24 deletions

View File

@ -3,5 +3,5 @@ reference:
amortize -P 100000 -r 0.05 -f monthly -n 36 -s > ref.txt amortize -P 100000 -r 0.05 -f monthly -n 36 -s > ref.txt
test: 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}"

View File

@ -1,6 +1,4 @@
import argparse, json
def generate_amortization_schedule(principal, interest_rate, loan_term, extra_payments=[]): def generate_amortization_schedule(principal, interest_rate, loan_term, extra_payments=[]):
# Convert interest rate to decimal and calculate periodic interest rate # Convert interest rate to decimal and calculate periodic interest rate
monthly_interest_rate = interest_rate / 12 / 100 monthly_interest_rate = interest_rate / 12 / 100
@ -73,7 +71,30 @@ def get_totals(amortization_schedule, func=None):
func(messages) func(messages)
return total_paid, total_interest_paid, total_principal_paid 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__": if __name__ == "__main__":
import argparse, json
def get_arguments(): def get_arguments():
p = argparse.ArgumentParser() p = argparse.ArgumentParser()
p. add_argument("--principal", "-p", type=float, \ p. add_argument("--principal", "-p", type=float, \
@ -94,29 +115,12 @@ if __name__ == "__main__":
# print(extra["extra-payments"]) # print(extra["extra-payments"])
if args.one_time is not None: if args.one_time is not None:
l.append(json.loads(args.one_time)) l.append(json.loads(args.one_time))
extra = l["extra-payments"] extra = []
extra.sort(key=lambda k: k["payment_number"]) 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 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() principal, interest_rate, loan_term, extra_payments = get_arguments()
schedule = generate_amortization_schedule(principal, interest_rate, loan_term, extra_payments) schedule = generate_amortization_schedule(principal, interest_rate, loan_term, extra_payments)