-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptax.py
152 lines (123 loc) · 5.63 KB
/
ptax.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
from bottle import run, get, request, response
import re
@get("/ptax")
def ptax():
""" Single endpoint to get annual property tax information for a particular address
Request URL parameters:
address (string, optional): Address of property. Optionally can be just a state.
value (integer, required): Property value
Response:
200 (json):
property_tax_amount (string): Calculated annual amount of property tax
property_tax_effective_rate (float): Effective annual rate of property tax
information (string): Brief description of calculation
400 (json):
error (string): Description of error
"""
# Confirm value is a number that is between 1e3 and 1e12
# In the future, make this handle dollar signs and commas and anything else MLS throws our way.
value = request.query.value
if not value:
response.status = 400
return {"error": "No value provided"}
try:
value = float(value)
except ValueError:
response.status = 400
return {"error": "Bad value provided"}
if value < 1e3 or value > 1e12:
response.status = 400
return {"error": "Value out of range"}
# Parse address to get state
# Simple, brittle regex parser here to be replaced with something more robust.
address = request.query.address
state = None
m = re.search("[A-Z]{2}", address)
if m:
state = m.group(0)
# Identify property tax calculation function to be called based on the state_map dict.
if address and not state:
# If no state is included in an address, that's a malformed address.
response.status = 400
return {"error": "Please include two-character state (e.g., CA) in address."}
elif not address:
# If no address passed, use national average.
fn = default
elif state not in state_map:
# If address passed, but state does not exist, use national average but log warning
# since the front-end should not be passing states that are outside Compass markets.
fn = default
# TODO: LOG WARNING HERE #
else:
# This is the normal branch.
fn = state_map[state]
# Call appropriate property tax function
result = fn(address, value)
return result
def california(address, value):
"""California Property Tax - Last Updated March, 2019
- Calculation is a simple statewide average percentage times the value.
- As of the 2017 annual report, the statewide average is 1.143%.
- Other reports of average effective rates (~0.74%) are misleading due to Proposition 13.
- Property tax in all areas of California is at least 1%.
- Precise property tax rates is very difficult to calculate in a scalable way because of "tax rate area" polygons.
- Tax rate areas can pass bonds to fund additional local services.
- This may increase property tax beyond 1%, but it always stays pretty close to 1%.
"""
property_tax_effective_rate = 0.01143
property_tax_amount = f"{value * property_tax_effective_rate:.2f}"
information = "Based on the California statewide average property tax rate"
return {
"property_tax_amount": property_tax_amount,
"property_tax_effective_rate": property_tax_effective_rate,
"information": information,
}
def colorado(address, value):
"""Colorado Property Tax - Last Updated March, 2019
- Calculation is a simple statewide average percentage times the value.
- As of 2018, the statewide average is 0.57%
- CO has similar issues to CA where there are different tax rate areas.
"""
property_tax_effective_rate = 0.0057
property_tax_amount = f"{value * property_tax_effective_rate:.2f}"
information = "Based on the Colorado statewide average property tax rate"
return {
"property_tax_amount": property_tax_amount,
"property_tax_effective_rate": property_tax_effective_rate,
"information": information,
}
def washington_dc(amount, value):
"""Washington DC Property Tax - Last Updated March, 2019
- Assumes buyer will be living there, so deduct the homestead exemption of $73,350
- Then apply the standing property tax rate of 0.85%
"""
assessed_value = max(value - 73350, 0)
property_tax_amount = assessed_value * 0.0085
property_tax_effective_rate = property_tax_amount / value
property_tax_amount = f"{property_tax_amount:.2f}"
property_tax_effective_rate = f"{property_tax_effective_rate:.5f}"
information = (
"Based on DC-wide property tax rate after application of homestead exemption"
)
return {
"property_tax_amount": property_tax_amount,
"property_tax_effective_rate": property_tax_effective_rate,
"information": information,
}
def default(address, value):
"""Default Property Tax Calculation -- Last Updated March, 2019
- Calculation is a simple nationwide average percentage times the value.
- As of April 3, 2018, the nationwide average is 1.17%.
- Understates the national average due to the way California is handled.
- In CA, this number treats the effective rate is based on the FMV of the properties rather than the assessed value.
"""
property_tax_effective_rate = 0.0117
property_tax_amount = f"{value * property_tax_effective_rate:.2f}"
information = "Based on the nationwide average property tax rate"
return {
"property_tax_amount": property_tax_amount,
"property_tax_effective_rate": property_tax_effective_rate,
"information": information,
}
state_map = {"CA": california, "CO": colorado, "DC": washington_dc}
run(port=8080)