-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
160 lines (121 loc) · 5.18 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# -*- coding: utf-8 -*-
"""Loan Qualifier Application.
This is a command line application to match applicants with qualifying loans.
Example:
$ python app.py
"""
from os import close
from re import escape
import sys
import fire
import questionary
from pathlib import Path
from qualifier.utils.fileio import load_csv, save_csv
from qualifier.utils.calculators import (
calculate_monthly_debt_ratio,
calculate_loan_to_value_ratio,
)
from qualifier.filters.max_loan_size import filter_max_loan_size
from qualifier.filters.credit_score import filter_credit_score
from qualifier.filters.debt_to_income import filter_debt_to_income
from qualifier.filters.loan_to_value import filter_loan_to_value
def load_bank_data():
"""Ask for the file path to the latest banking data and load the CSV file.
Returns:
The bank data from the data rate sheet CSV file.
"""
csvpath = questionary.text("Enter a file path to a rate-sheet (.csv):").ask()
csvpath = Path(csvpath)
if not csvpath.exists():
sys.exit(f"Oops! Can't find this path: {csvpath}")
return load_csv(csvpath)
def get_applicant_info():
"""Prompt dialog to get the applicant's financial information.
Returns:
Returns the applicant's financial information.
"""
credit_score = questionary.text("What's your credit score?").ask()
try:
credit_score = int(credit_score)
except:
print("Invalid input, please restart Qualifier and use only numbers.")
debt = questionary.text("What's your current amount of monthly debt?").ask()
try:
debt = float(debt)
except:
print("Invalid input, please restart Qualifier and use only numbers.")
income = questionary.text("What's your total monthly income?").ask()
try:
income = float(income)
except:
print("Invalid input, please restart Qualifier and use only numbers.")
loan_amount = questionary.text("What's your desired loan amount?").ask()
try:
loan_amount = float(loan_amount)
except:
print("Invalid input, please restart Qualifier and use only numbers.")
home_value = questionary.text("What's your home value?").ask()
try:
home_value = float(home_value)
except:
print("Invalid input, please restart Qualifier and use only numbers.")
return credit_score, debt, income, loan_amount, home_value
def find_qualifying_loans(bank_data, credit_score, debt, income, loan, home_value):
"""Determine which loans the user qualifies for.
Loan qualification criteria is based on:
- Credit Score
- Loan Size
- Debit to Income ratio (calculated)
- Loan to Value ratio (calculated)
Args:
bank_data (list): A list of bank data.
credit_score (int): The applicant's current credit score.
debt (float): The applicant's total monthly debt payments.
income (float): The applicant's total monthly income.
loan (float): The total loan amount applied for.
home_value (float): The estimated home value.
Returns:
A list of the banks willing to underwrite the loan.
"""
# Calculate the monthly debt ratio.
monthly_debt_ratio = calculate_monthly_debt_ratio(debt, income)
print(f"The monthly debt to income ratio is {monthly_debt_ratio:.02f}.")
# Calculate loan to value ratio.
loan_to_value_ratio = calculate_loan_to_value_ratio(loan, home_value)
print(f"The loan to value ratio is {loan_to_value_ratio:.02f}.")
# Run qualification filters.
bank_data_filtered = filter_max_loan_size(loan, bank_data)
bank_data_filtered = filter_credit_score(credit_score, bank_data_filtered)
bank_data_filtered = filter_debt_to_income(monthly_debt_ratio, bank_data_filtered)
bank_data_filtered = filter_loan_to_value(loan_to_value_ratio, bank_data_filtered)
if len(bank_data_filtered) > 0:
print(f"Great! Qualifier Found {len(bank_data_filtered)} Qualifying Loans!")
return bank_data_filtered
def save_qualifying_loans(qualifying_loans):
"""Saves the qualifying loans to a CSV file.
Args:
qualifying_loans (list of lists): The qualifying bank loans.
"""
if len(qualifying_loans) == 0:
sys.exit(f"Sorry, you currently do not qualify.")
ask_to_save = questionary.confirm("Would you like to save your Qualifying Bank Loans?").ask()
if ask_to_save == False:
sys.exit(f"Loans not saved. Thank you for using Qualifier!")
rate_sheet = questionary.text("Enter a file path to a rate-sheet (.csv):").ask()
rate_sheet = Path(rate_sheet)
save_csv(rate_sheet, qualifying_loans)
sys.exit(f"Loans saved to output file: ({rate_sheet}). Thank you for using Qualifier!")
def run():
"""The main function for running the script."""
# Load the latest Bank data.
bank_data = load_bank_data()
# Get the applicant's information.
credit_score, debt, income, loan_amount, home_value = get_applicant_info()
# Find all qualifying loans.
qualifying_loans = find_qualifying_loans(
bank_data, credit_score, debt, income, loan_amount, home_value
)
# Save all qualifying loans.
save_qualifying_loans(qualifying_loans)
if __name__ == "__main__":
fire.Fire(run)