28 lines
667 B
Python
28 lines
667 B
Python
from dataclasses import dataclass, field
|
|
from uuid import UUID, uuid4
|
|
|
|
|
|
@dataclass
|
|
class Account:
|
|
id: UUID = field(default_factory=uuid4)
|
|
name: str = ""
|
|
currency: str = "USD"
|
|
balance: float = 0.0
|
|
|
|
def to_dict(self) -> dict:
|
|
return {
|
|
"id": str(self.id),
|
|
"name": self.name,
|
|
"currency": self.currency,
|
|
"balance": self.balance,
|
|
}
|
|
|
|
@classmethod
|
|
def from_dict(cls, data: dict) -> "Account":
|
|
return cls(
|
|
id=UUID(data["id"]),
|
|
name=data["name"],
|
|
currency=data.get("currency", "USD"),
|
|
balance=data.get("balance", 0.0),
|
|
)
|