2024-06-11 20:33:35 +02:00
|
|
|
import datetime
|
2024-06-11 23:47:07 +02:00
|
|
|
from decimal import Decimal
|
|
|
|
|
from numbers import Number
|
|
|
|
|
from typing import Iterable, Set, Union
|
2024-06-11 20:33:35 +02:00
|
|
|
|
|
|
|
|
from money.currency import Currency
|
|
|
|
|
from money.money import Money
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ExchangeRate:
|
|
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
from_currency: Currency,
|
|
|
|
|
to_currency: Currency,
|
2024-06-11 23:47:07 +02:00
|
|
|
rate: Union[Money, Number],
|
2024-06-11 20:33:35 +02:00
|
|
|
rate_date: datetime.date,
|
|
|
|
|
) -> None:
|
|
|
|
|
self.from_currency = from_currency
|
|
|
|
|
self.to_currency = to_currency
|
2024-06-11 23:47:07 +02:00
|
|
|
if not isinstance(rate, Money):
|
|
|
|
|
rate = Money(rate, to_currency)
|
2024-06-11 20:33:35 +02:00
|
|
|
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
|
|
|
)
|
|
|
|
|
|
2024-06-11 23:47:07 +02:00
|
|
|
@property
|
|
|
|
|
def amount(self) -> Decimal:
|
|
|
|
|
return self.rate.amount
|
|
|
|
|
|
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
|
|
|
|
|
|
2024-06-11 23:47:07 +02:00
|
|
|
@property
|
|
|
|
|
def present_currencies(self) -> Set[Currency]:
|
|
|
|
|
present_currencies = set()
|
|
|
|
|
|
|
|
|
|
for rate in self:
|
|
|
|
|
present_currencies.add(rate.from_currency)
|
|
|
|
|
present_currencies.add(rate.to_currency)
|
|
|
|
|
|
|
|
|
|
return present_currencies
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def present_dates(self) -> Set[datetime.date]:
|
|
|
|
|
return {rate.rate_date for rate in self}
|
|
|
|
|
|
2024-06-11 20:33:35 +02:00
|
|
|
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())
|
2024-06-11 23:47:07 +02:00
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
|
return len(self._rate_index)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_equal_rates(rates: ExchangeRates) -> ExchangeRates:
|
|
|
|
|
|
|
|
|
|
present_currencies = rates.present_currencies
|
|
|
|
|
present_dates = rates.present_dates
|
|
|
|
|
|
|
|
|
|
for date in present_dates:
|
|
|
|
|
for currency in present_currencies:
|
|
|
|
|
rates.add_rate(
|
|
|
|
|
ExchangeRate(
|
|
|
|
|
from_currency=currency,
|
|
|
|
|
to_currency=currency,
|
|
|
|
|
rate=Money(1, currency),
|
|
|
|
|
rate_date=date,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return rates
|