property.holding-deposit-cap@1.0.0
impl/python.py
1,066 bytes · the Python implementation · view raw
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
from .money_amount import Money, money ← from money.amount ^1.0.0 · built alongside by fune
from .property_deposit_cap import RentPeriod ← from property.deposit-cap ^1.0.0 · built alongside by fune
# Keeps annual rent exact as a JavaScript number; the same limit here.
_MAX_MINOR = 1_000_000_000_000
def holding_deposit_cap(rent: Money, rent_period: RentPeriod) -> Money:
"""One week's rent, the statutory maximum holding deposit: the annual rent
divided by 52, rounded down once so the deposit can never exceed the cap."""
if rent.currency != "GBP":
raise ValueError("rent must be in GBP, received %s" % (rent.currency,))
if rent.minor < 0:
raise ValueError("rent must not be negative, received %r" % (rent.minor,))
if rent_period == "weekly":
annual = rent.minor * 52
elif rent_period == "monthly":
annual = rent.minor * 12
elif rent_period == "annual":
annual = rent.minor
else:
raise ValueError('unknown rent period "%s"' % (rent_period,))
if annual > _MAX_MINOR:
raise ValueError("annual rent must be at most %d minor units" % (_MAX_MINOR,))
return money(annual // 52, "GBP")