-
Notifications
You must be signed in to change notification settings - Fork 0
/
rent_calculator.py
127 lines (103 loc) · 4.61 KB
/
rent_calculator.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
import csv
from helpers import Home, Options, Rent
floor_plan = """
[Floor B] [Floor C]
|------------------------ |------------------------
| | R1 | | R1 | R2 |
|----- ------ |-----------------
| R2 | | R3 | |
|----- ----- |
|BRoom |
|BRoom |BRoom -----
| |BRoom | R3 |
|Kitchen |
|---- ---- |Kitchen
| R4 | | R5 | |----- -----
------------------- | R4 | | R5 |
-------------------
"""
# Putting up here for cleanliness reasons. Makes it easier to add more later.
def apply_fees_or_discounts(deets, home):
bonus_fees_or_discounts = home.good_lighting_fee if deets.has_window \
else home.bad_lighting_deduction
return bonus_fees_or_discounts
def calculate_rent(room_deets, home, round_dollar=True):
occupants = len(room_deets.occupied_by)
base_cost = home.common_share_cost * occupants
room_cost = room_deets.sqft * home.room_cost_per_sqft * room_deets.percent_usable
bonus_fees_or_discounts = apply_fees_or_discounts(room_deets, home)
rent = (base_cost + room_cost) + (bonus_fees_or_discounts * occupants)
if round_dollar:
return int(rent)
return rent
# Since there's lots of division happening, it makes more sense for us to drop
# all of the extra pennies on people paying the least.
def cheapest_room_picks_up_the_cents(rents, home):
missing_money = home.rent_sum - sum([rent.price for rent in rents.values()])
num_people_with_lowest_price = sum([len(rent.deets.occupied_by) for rent in home.cheapest_rooms])
cent_diff = missing_money / num_people_with_lowest_price
for rent in home.cheapest_rooms:
new_price = rent.price + (cent_diff * len(rent.deets.occupied_by))
rents[rent.room] = Rent(new_price, rent.room, rent.deets)
return rents
# Takes in a list of Rent
def print_rents_per_room(rents):
row_divider = "-" * 96
row_format = "{:^15}|{:^15}|{:^15}|{:^15}|{:^15}|{:^15}"
print
print row_format.format("Room", "Rent", "SqFt", "% Usable",
"Good Lighting", "Occupants")
print row_divider
for rent in sorted(rents):
print row_format.format(rent.room, ("$%.2f" % rent[0]),
rent.deets.sqft,
"{}%".format(int(rent.deets.percent_usable * 100)),
str(rent.deets.has_window),
", ".join(rent.deets.occupied_by))
print row_divider
print "{:^15}|{:^15}|".format(
"Total", "$%.2f" % sum([rent.price for rent in rents]))
print
# Takes in a list of Rent objects and uses it to print out individual rents.
def print_cost_per_person(rents):
cost_person = []
for rent in rents:
cost = rent.price / len(rent.deets.occupied_by)
for person in rent.deets.occupied_by:
cost_person.append([cost, person, rent.room])
row_divider = "-" * 50
row_format = "{:^15}|{:^15}|{:^15}"
print
print row_format.format("Name", "Room", "Rent")
print row_divider
for rent in sorted(cost_person):
print row_format.format(rent[1], rent[2], "$%.2f" % rent[0])
print row_divider
print
if __name__ == '__main__':
options, args = Options.parse_args()
home = Home(options)
home.bad_lighting_deduction, home.good_lighting_fee = home.calculate_opposing_fee(
'has_window', float(options.bad_lighting_deduct) * -1)
rents = {}
for room, deets in home.rooms.iteritems():
rent = calculate_rent(deets, home, home.round_dollar)
room_rent = Rent(rent, room, deets)
home.set_cheapest_room(room_rent)
rents[room] = room_rent
if home.round_dollar:
rents = cheapest_room_picks_up_the_cents(rents, home)
rents = sorted(rents.values())
print floor_plan
print "People in House:", home.peeps_count
print "Total SqFt:", home.house_size
print "Common Space:", home.common_space
print "Cost per Common SqFt: $%.2f (Weighted by %.2f)" % (
home.common_cost_per_sqft, home.common_weight)
print "Common Cost: $%.2f" % home.common_share_cost
print "Cost per Room SqFt: $%.2f" % home.common_cost_per_sqft
print "Bad Lighting Rebate: $%.2f" % home.bad_lighting_deduction
print "Good Lighting Upcharge: $%.2f" % home.good_lighting_fee
print_rents_per_room(rents)
if not home.calculate_as_singles:
print_cost_per_person(rents)