example-code-2e/10-dp-1class-func/strategy_best4.py

91 lines
2.6 KiB
Python
Raw Normal View History

2020-06-11 19:58:15 +02:00
# strategy_best4.py
# Strategy pattern -- function-based implementation
# selecting best promotion from list of functions
# registered by a decorator
"""
2021-08-07 05:44:01 +02:00
>>> from decimal import Decimal
>>> from strategy import Customer, LineItem, Order
>>> from promotions import *
2020-06-11 19:58:15 +02:00
>>> joe = Customer('John Doe', 0)
>>> ann = Customer('Ann Smith', 1100)
2021-08-07 05:44:01 +02:00
>>> cart = [LineItem('banana', 4, Decimal('.5')),
... LineItem('apple', 10, Decimal('1.5')),
... LineItem('watermelon', 5, Decimal(5))]
>>> Order(joe, cart, fidelity_promo)
2020-06-11 19:58:15 +02:00
<Order total: 42.00 due: 42.00>
2021-08-07 05:44:01 +02:00
>>> Order(ann, cart, fidelity_promo)
2020-06-11 19:58:15 +02:00
<Order total: 42.00 due: 39.90>
2021-08-07 05:44:01 +02:00
>>> banana_cart = [LineItem('banana', 30, Decimal('.5')),
... LineItem('apple', 10, Decimal('1.5'))]
>>> Order(joe, banana_cart, bulk_item_promo)
2020-06-11 19:58:15 +02:00
<Order total: 30.00 due: 28.50>
2021-08-07 05:44:01 +02:00
>>> long_cart = [LineItem(str(item_code), 1, Decimal(1))
2020-06-11 19:58:15 +02:00
... for item_code in range(10)]
2021-08-07 05:44:01 +02:00
>>> Order(joe, long_cart, large_order_promo)
2020-06-11 19:58:15 +02:00
<Order total: 10.00 due: 9.30>
2021-08-07 05:44:01 +02:00
>>> Order(joe, cart, large_order_promo)
2020-06-11 19:58:15 +02:00
<Order total: 42.00 due: 42.00>
# tag::STRATEGY_BEST_TESTS[]
2021-08-07 05:44:01 +02:00
>>> Order(joe, long_cart, best_promo)
2020-06-11 19:58:15 +02:00
<Order total: 10.00 due: 9.30>
>>> Order(joe, banana_cart, best_promo)
<Order total: 30.00 due: 28.50>
>>> Order(ann, cart, best_promo)
<Order total: 42.00 due: 39.90>
# end::STRATEGY_BEST_TESTS[]
"""
2021-08-07 05:44:01 +02:00
from decimal import Decimal
from typing import Callable
2020-06-11 19:58:15 +02:00
2021-08-07 05:44:01 +02:00
from strategy import Order
2020-06-11 19:58:15 +02:00
# tag::STRATEGY_BEST4[]
2021-08-07 05:44:01 +02:00
Promotion = Callable[[Order], Decimal]
promos: list[Promotion] = [] # <1>
2020-06-11 19:58:15 +02:00
def promotion(promo: Promotion) -> Promotion: # <2>
promos.append(promo)
return promo
2021-08-07 05:44:01 +02:00
def best_promo(order: Order) -> Decimal:
"""Compute the best discount available"""
return max(promo(order) for promo in promos) # <3>
2020-06-11 19:58:15 +02:00
2021-08-07 05:44:01 +02:00
@promotion # <4>
def fidelity(order: Order) -> Decimal:
2020-06-11 19:58:15 +02:00
"""5% discount for customers with 1000 or more fidelity points"""
2021-08-07 05:44:01 +02:00
if order.customer.fidelity >= 1000:
return order.total() * Decimal('0.05')
return Decimal(0)
2020-06-11 19:58:15 +02:00
@promotion
2021-08-07 05:44:01 +02:00
def bulk_item(order: Order) -> Decimal:
2020-06-11 19:58:15 +02:00
"""10% discount for each LineItem with 20 or more units"""
2021-08-07 05:44:01 +02:00
discount = Decimal(0)
2020-06-11 19:58:15 +02:00
for item in order.cart:
if item.quantity >= 20:
2021-08-07 05:44:01 +02:00
discount += item.total() * Decimal('0.1')
2020-06-11 19:58:15 +02:00
return discount
@promotion
2021-08-07 05:44:01 +02:00
def large_order(order: Order) -> Decimal:
2020-06-11 19:58:15 +02:00
"""7% discount for orders with 10 or more distinct items"""
distinct_items = {item.product for item in order.cart}
if len(distinct_items) >= 10:
2021-08-07 05:44:01 +02:00
return order.total() * Decimal('0.07')
return Decimal(0)
2020-06-11 19:58:15 +02:00
# end::STRATEGY_BEST4[]