-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexample_dataclass.py
More file actions
95 lines (73 loc) · 2.48 KB
/
Copy pathexample_dataclass.py
File metadata and controls
95 lines (73 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from dataclasses import dataclass, field
from datetime import date
from enum import StrEnum, auto
class OrderStatus(StrEnum):
OPEN = auto()
CLOSED = auto()
@dataclass
class Product:
name: str = field(compare=True)
category: str = field(compare=True)
shipping_weight: float = field(compare=False)
unit_price: int = field(compare=False)
tax_percent: float = field(compare=False)
def __post_init__(self) -> None:
if self.unit_price < 0:
raise ValueError("unit_price attribute must greater then zero.")
if self.shipping_weight < 0:
raise ValueError("shipping_weight attribute must greater then zero.")
if not 0 < self.tax_percent < 1:
raise ValueError("tax_percent attribute must be between zero and one.")
@dataclass
class Order:
status: OrderStatus
creation_date: date = date.today()
products: list[Product] = field(default_factory=list)
def add_product(self, product: Product) -> None:
self.products.append(product)
@property
def sub_total(self) -> int:
return sum((p.unit_price for p in self.products))
@property
def tax(self) -> float:
return sum(
(product.unit_price * product.tax_percent for product in self.products)
)
@property
def total_price(self) -> float:
return self.sub_total + self.tax
@property
def total_shipping_weight(self) -> float:
return sum((product.shipping_weight for product in self.products))
def main() -> None:
banana = Product(
name="banana",
category="fruit",
shipping_weight=0.5,
unit_price=215,
tax_percent=0.07,
)
mango = Product(
name="mango",
category="fruit",
shipping_weight=2,
unit_price=319,
tax_percent=0.11,
)
expensive_mango = Product(
name="Mango",
category="Fruit",
shipping_weight=4.0,
unit_price=800,
tax_percent=0.20,
)
order = Order(status=OrderStatus.OPEN)
for product in [banana, mango, expensive_mango]:
order.add_product(product)
print(f"Comparison between mango and expensive mango: {mango == expensive_mango}")
print(f"Total order price: ${order.total_price/100:.2f}")
print(f"Subtotal order price: ${order.sub_total/100:.2f}")
print(f"Value paid in taxes: ${order.tax/100:.2f}")
print(f"Total weight order: {order.total_shipping_weight} kg")
if __name__ == "__main__":
main()