-
Notifications
You must be signed in to change notification settings - Fork 3
/
stats.py
153 lines (126 loc) · 5.37 KB
/
stats.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
from bdb import effective
import json
from logging import error
import os
import re
import isodate
import json
lowest_rate = 100
mortgage_amount = 600000
def parse_duration(duration):
try:
interest_period = isodate.parse_duration(duration)
except:
return None
return interest_period
def get_product(brand, product):
for file in os.listdir('brands/product/'+brand+"/"):
file_name = os.path.splitext(file)[0]
if file_name == product:
raw_file = open('brands/product/' + brand + "/" + file, "rb")
contents = json.load(raw_file)
raw_file.close()
return contents
def check_product(product):
if "data" not in product:
return False
if product["data"]["productCategory"] != "RESIDENTIAL_MORTGAGES":
return False
if "lendingRates" not in product["data"]:
return False
if "brandName" not in product["data"] and "brand" not in product["data"]:
return False
if "name" not in product["data"]:
return False
if "description" not in product["data"]:
return False
if "productId" not in product["data"]:
return False
return True
def check_eligability(product):
if "constraints" in product["data"]:
for constraint in product["data"]["constraints"]:
if constraint["constraintType"] == "MAX_LIMIT" or constraint["constraintType"] == "MAX_BALANCE":
try:
if float(constraint["additionalValue"]) < mortgage_amount:
return False
except:
return False
return True
def check_offset(product):
if "features" in product["data"]:
for feature in product["data"]["features"]:
if feature["featureType"] == "OFFSET":
if "additionalValue" in feature and (feature["additionalValue"] == "No" or feature["additionalValue"] == "Not available"):
continue
return True
return False
def check_redraw(product):
if "features" in product["data"]:
for feature in product["data"]["features"]:
if feature["featureType"] == "REDRAW":
if "additionalValue" in feature and (feature["additionalValue"] == "No" or feature["additionalValue"] == "Not available"):
continue
return True
return False
def calculate_interest(lendingRates):
global lowest_rate
product_rates = []
for lendingRate in lendingRates:
if lendingRate["lendingRateType"] != "VARIABLE" and lendingRate["lendingRateType"] != "FIXED":
continue
try:
rate = round(float(lendingRate["rate"])*100,2)
except:
continue
formatted = {"rate": rate, "lendingRateType": lendingRate["lendingRateType"]}
if "loanPurpose" in lendingRate:
formatted["purpose"] = lendingRate["loanPurpose"]
if lendingRate["lendingRateType"] == "FIXED" and "additionalValue" in lendingRate and lendingRate["additionalValue"] != None:
try:
period = parse_duration(lendingRate["additionalValue"])
months = int(period.years)*12 + int(period.months)
except:
period = None
if period != None:
formatted["period"] = months
if "repaymentType" in lendingRate and lendingRate["repaymentType"] != None:
formatted["repaymentType"] = lendingRate["repaymentType"]
if "tiers" in lendingRate:
for tier in lendingRate["tiers"]:
if ("LVR" in tier["name"].upper() and tier["unitOfMeasure"] == "PERCENT" and "minimumValue" in tier and "maximumValue" in tier):
if (tier["minimumValue"] != None):
if (float(tier["minimumValue"]) <= 1):
formatted["minLVR"] = float(tier["minimumValue"])*100
else:
formatted["minLVR"] = float(tier["minimumValue"])
if (tier["maximumValue"] != None):
if (float(tier["maximumValue"]) <= 1):
formatted["maxLVR"] = float(tier["maximumValue"])*100
else:
formatted["maxLVR"] = float(tier["maximumValue"])
product_rates.append(formatted)
if len(product_rates) == 0:
return None
return product_rates
data = []
for root, dirs, files in os.walk("brands/product/"):
for file in files:
brand = root.split("/")[2]
id = os.path.splitext(file)[0]
product = get_product(brand, id)
if not check_product(product):
continue
if not check_eligability(product):
continue
rate = calculate_interest(product["data"]["lendingRates"])
if rate == None:
continue
processed = {"brandId": brand, "brandName": "placeholder", "productId": product["data"]["productId"], "productName": product["data"]["name"], "rate": rate, "offset": check_offset(product), "redraw": check_redraw(product)}
if "brandName" in product["data"]:
processed["brandName"] = product["data"]["brandName"]
else:
processed["brandName"] = product["data"]["brand"]
data.append(processed)
with open('aggregate/RESIDENTIAL_MORTGAGES/data.json', 'w') as outfile:
json.dump(data, outfile)