-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct.py
42 lines (33 loc) · 974 Bytes
/
product.py
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
import json
from dataclasses import dataclass, field
from datetime import datetime
from typing import Optional
from uuid import uuid1
@dataclass
class ProductBase:
name: str
type: str
price: float
id: Optional[str] = None
def __post_init__(self):
if not self.id:
self.id = str(uuid1())
def to_dict(self) -> dict:
d = {
"name": self.name,
"type": self.type,
"price": self.price,
"id": self.id,
}
return d
def to_json(self) -> str:
d = self.to_dict()
return json.dumps(d, indent=3)
def __repr__(self) -> str:
return f'Produkt: "{self.name}" ({self.type}, cena {self.price:.2f} zł, ID {self.id})'
@dataclass
class Product(ProductBase):
quantity: float = field(default=0.0)
timestamp: datetime = field(default_factory=datetime.utcnow)
def __post_init__(self):
self.value = self.quantity * self.price