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