2024-06-11 20:33:35 +02:00
|
|
|
import datetime
|
|
|
|
|
from typing import Iterable, Union
|
|
|
|
|
|
|
|
|
|
from money.currency import Currency
|
|
|
|
|
from money.money import Money
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ExchangeRate:
|
|
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
from_currency: Currency,
|
|
|
|
|
to_currency: Currency,
|
|
|
|
|
rate: Money,
|
|
|
|
|
rate_date: datetime.date,
|
|
|
|
|
) -> None:
|
|
|
|
|
self.from_currency = from_currency
|
|
|
|
|
self.to_currency = to_currency
|
|
|
|
|
self.rate = rate
|
|
|
|
|
self.rate_date = rate_date
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def descriptor(self) -> str:
|
|
|
|
|
return (
|
|
|
|
|
str(self.from_currency.value)
|
|
|
|
|
+ str(self.to_currency.value)
|
2024-06-11 20:44:03 +02:00
|
|
|
+ str(self.rate_date.strftime("%Y-%m-%d"))
|
2024-06-11 20:33:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ExchangeRates:
|
|
|
|
|
|
|
|
|
|
def __init__(self, rates: Union[Iterable[ExchangeRate], None] = None):
|
|
|
|
|
|
|
|
|
|
self._rate_index = {}
|
|
|
|
|
|
|
|
|
|
if rates is not None:
|
|
|
|
|
for rate in rates:
|
|
|
|
|
if not isinstance(rate, ExchangeRate):
|
|
|
|
|
raise TypeError("ExchangeRates can only hold Rates.")
|
|
|
|
|
self._rate_index[rate.descriptor] = rate
|
|
|
|
|
|
|
|
|
|
def add_rate(self, new_rate: ExchangeRate) -> None:
|
|
|
|
|
self._rate_index[new_rate.descriptor] = new_rate
|
|
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
|
return iter(self._rate_index.values())
|