-
Notifications
You must be signed in to change notification settings - Fork 3
/
product.py
30 lines (21 loc) · 793 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
from typing import Tuple
from pydantic import BaseModel, validator
from src.project.errors import CurrencyError
supported_currencies = ["dollar", "euro"]
class Product(BaseModel):
"""This class represent a product entry."""
name: str
currency: str
price: float
@validator("currency")
@classmethod
def check_currency_is_supported(cls, value: str, values: dict) -> str:
if value not in supported_currencies:
msg = f"Currency '{value}' isn't supported in product '{values['name']}'."
raise CurrencyError(value, msg)
return value
def to_tuple(self) -> Tuple:
"""Convert Product object to a tuple of the type:
(name, currency, price).
"""
return (self.name, self.currency, self.price)