Slide 67
Slide 67 text
everyday
superpowers
async def compute_price(
kind: str,
age: int = AGE_MISSING,
date: datetime.date = DATE_MISSING,
):
base_price = await get_base_price(kind)
if kind == 'night':
if age < 6:
return 0
if 64 < age:
return math.ceil(base_price * .4)
return base_price
else:
if age < 6:
return 0
if age < 15:
return math.ceil(base_price * .7)
price_with_date_discount = (
base_price * await _calculate_date_discount(date)
)
if 64 < age:
return math.ceil(
price_with_date_discount * 0.75
)
else:
return math.ceil(price_with_date_discount)
async def compute_price(
type: str, age: int = AGE_MISSING, date: datetime.date = DATE_MISSING
) -> int:
if type == 'night':
return await NightTicket(age).price
return await NormalTicket(age, date).price
class Ticket:
ticket_kind = ''
async def get_base_price(self):
return await get_base_price_for(self.ticket_kind)
class NightTicket(Ticket):
ticket_kind = 'night'
def __init__(self, age: int = AGE_MISSING, date: datetime = DATE_MISSING):
self.age = age
@property
async def price(self):
if self.age <= 6:
return 0
if self.age > 64:
return math.ceil(await self.get_base_price() * .4)
return await self.get_base_price()
class NormalTicket(Ticket):
ticket_kind = '1day'
def __init__(self, age: int = AGE_MISSING, date: datetime.date = DATE_MISSING):
self.age = age
self.date = date
@property
async def price(self):
if self.age <= 6:
return 0
if self.age < 15:
return math.ceil(await self.get_base_price() * .7)
reduction = 1 - 35 / 100 if await self.is_holiday() else 1
if self.age > 64:
return math.ceil(await self.get_base_price() * .75 * reduction)
return math.ceil(await self.get_base_price() * reduction)
async def is_holiday(self):
holidays = {row.holiday for row in
await database.fetch_all(select(holidays_table))}
return self.date in holidays