|
7 | 7 | Enables selecting an algorithm at runtime.
|
8 | 8 | """
|
9 | 9 |
|
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +from typing import Callable, Type |
| 13 | + |
| 14 | + |
| 15 | +class DiscountStrategyValidator: # Descriptor class for check perform |
| 16 | + def validate(self, obj: Order, value: Callable) -> bool: |
| 17 | + try: |
| 18 | + if obj.price - value(obj) < 0: |
| 19 | + raise ValueError(f"Discount cannot be applied due to negative price resulting. {value.__name__}") |
| 20 | + return True |
| 21 | + except ValueError as ex: |
| 22 | + print(str(ex)) |
| 23 | + return False |
| 24 | + |
| 25 | + def __set_name__(self, owner, name: str) -> None: |
| 26 | + self.private_name = '_' + name |
| 27 | + |
| 28 | + def __set__(self, obj: Order, value: Callable = None) -> None: |
| 29 | + if value and self.validate(obj, value): |
| 30 | + setattr(obj, self.private_name, value) |
| 31 | + else: |
| 32 | + setattr(obj, self.private_name, None) |
| 33 | + |
| 34 | + def __get__(self, obj: object, objtype: Type = None): |
| 35 | + return getattr(obj, self.private_name) |
| 36 | + |
10 | 37 |
|
11 | 38 | class Order:
|
12 |
| - def __init__(self, price, discount_strategy=None): |
13 |
| - self.price = price |
| 39 | + discount_strategy = DiscountStrategyValidator() |
| 40 | + |
| 41 | + def __init__(self, price: float, discount_strategy: Callable = None) -> None: |
| 42 | + self.price: float = price |
14 | 43 | self.discount_strategy = discount_strategy
|
15 | 44 |
|
16 |
| - def price_after_discount(self): |
| 45 | + def apply_discount(self) -> float: |
17 | 46 | if self.discount_strategy:
|
18 | 47 | discount = self.discount_strategy(self)
|
19 | 48 | else:
|
20 | 49 | discount = 0
|
| 50 | + |
21 | 51 | return self.price - discount
|
22 | 52 |
|
23 |
| - def __repr__(self): |
24 |
| - fmt = "<Price: {}, price after discount: {}>" |
25 |
| - return fmt.format(self.price, self.price_after_discount()) |
| 53 | + def __repr__(self) -> str: |
| 54 | + return f"<Order price: {self.price} with discount strategy: {getattr(self.discount_strategy,'__name__',None)}>" |
26 | 55 |
|
27 | 56 |
|
28 |
| -def ten_percent_discount(order): |
| 57 | +def ten_percent_discount(order: Order) -> float: |
29 | 58 | return order.price * 0.10
|
30 | 59 |
|
31 | 60 |
|
32 |
| -def on_sale_discount(order): |
| 61 | +def on_sale_discount(order: Order) -> float: |
33 | 62 | return order.price * 0.25 + 20
|
34 | 63 |
|
35 | 64 |
|
36 | 65 | def main():
|
37 | 66 | """
|
38 |
| - >>> Order(100) |
39 |
| - <Price: 100, price after discount: 100> |
40 |
| -
|
41 |
| - >>> Order(100, discount_strategy=ten_percent_discount) |
42 |
| - <Price: 100, price after discount: 90.0> |
43 |
| -
|
44 |
| - >>> Order(1000, discount_strategy=on_sale_discount) |
45 |
| - <Price: 1000, price after discount: 730.0> |
| 67 | + >>> order = Order(100, discount_strategy=ten_percent_discount) |
| 68 | + >>> print(order) |
| 69 | + <Order price: 100 with discount strategy: ten_percent_discount> |
| 70 | + >>> print(order.apply_discount()) |
| 71 | + 90.0 |
| 72 | + >>> order = Order(100, discount_strategy=on_sale_discount) |
| 73 | + >>> print(order) |
| 74 | + <Order price: 100 with discount strategy: on_sale_discount> |
| 75 | + >>> print(order.apply_discount()) |
| 76 | + 55.0 |
| 77 | + >>> order = Order(10, discount_strategy=on_sale_discount) |
| 78 | + Discount cannot be applied due to negative price resulting. on_sale_discount |
| 79 | + >>> print(order) |
| 80 | + <Order price: 10 with discount strategy: None> |
46 | 81 | """
|
47 | 82 |
|
48 | 83 |
|
|
0 commit comments